public static void applyMethod(Func <double, double> f, double a, double b, double eps) { int iteration = 1; double lambda = 0, q = 0; Func <double, double> phi = (double x) => x - lambda * f(x); Func <double, double> df = Auxiliary.getDerivative(f); if (Auxiliary.isMonotonous(df, a, b)) { double alpha = Math.Min(df(a), df(b)); double gamma = Math.Max(df(a), df(b)); lambda = 2 / (alpha + gamma); q = (gamma - alpha) / (gamma + alpha); do { b = phi(a); if (stopCriterion(a, b, eps, q)) { break; } a = b; Console.WriteLine(iteration + ": x = " + a); iteration++; } while (true); Console.WriteLine("\nRoot is " + (a + b) / 2 + "."); } else { Console.WriteLine("Function derivative is not monotonous in the interval."); } }
public static void applyMethod(Func <double, double> f, double a, double b, double eps) { double x = 0, prevX = 0; if ((f(a) * Auxiliary.getDerivativeValue(f, 2, a)) > 0) { x = a; } else { x = b; } int iteration = 1; if (Math.Abs(f(x)) < 5 * eps) { SteffensenMethod.applyMethod(f, x, prevX, eps, iteration); return; } Console.WriteLine("<------Newton Method------>"); do { prevX = x; x = x - f(x) / Auxiliary.getDerivativeValue(f, 1, x); Console.WriteLine(iteration + ": x_k = " + x); iteration++; if (Math.Abs(f(x)) < 5 * eps) { SteffensenMethod.applyMethod(f, x, prevX, eps, iteration); return; } } while (Math.Abs(x - prevX) > eps); Console.WriteLine("\nRoot is " + x + "."); }
static void Main(string[] args) { int userInput; Console.WriteLine("Choose what you want:\n" + "0. Exit.\n" + "1. First equation in Horde Method.\n" + "2. Second equation in Steffensen Method.\n" + "3. Second equation in Simple Iterations Method.\n" + "4. Output the roots of the algebraic equation by the Lobachevsky method (clarifying - SIM)"); int.TryParse(Console.ReadLine(), out userInput); Console.WriteLine("Enter epsilon or leave empty [ by default eps = 10^(-7) ]:"); string epsInput = Console.ReadLine(); double eps = string.IsNullOrEmpty(epsInput) ? Math.Pow(10, -7) : Convert.ToDouble(epsInput); double a, b; while (userInput != 0) { switch (userInput) { case 0: Console.Clear(); Console.WriteLine("Have a nice day! :)"); Environment.Exit(0); break; case 1: Console.Clear(); Console.WriteLine("Enter interval start:"); a = Convert.ToDouble(Console.ReadLine(), System.Globalization.CultureInfo.GetCultureInfo("en-US")); Console.WriteLine("Enter interval end:"); b = Convert.ToDouble(Console.ReadLine(), System.Globalization.CultureInfo.GetCultureInfo("en-US")); if (!Auxiliary.isInputForFuncValid(Variant.f1, a, b)) { break; } HordeMethod.applyMethod(Variant.f1, eps, a, b); break; case 2: Console.Clear(); Console.WriteLine("Enter interval start:"); a = Convert.ToDouble(Console.ReadLine(), System.Globalization.CultureInfo.GetCultureInfo("en-US")); Console.WriteLine("Enter interval end:"); b = Convert.ToDouble(Console.ReadLine(), System.Globalization.CultureInfo.GetCultureInfo("en-US")); if (!Auxiliary.isInputForFuncValid(Variant.f2, a, b)) { break; } NewtonMethod.applyMethod(Variant.f2, a, b, eps); break; case 3: Console.Clear(); Console.WriteLine("Enter interval start:"); a = Convert.ToDouble(Console.ReadLine(), System.Globalization.CultureInfo.GetCultureInfo("en-US")); Console.WriteLine("Enter interval end:"); b = Convert.ToDouble(Console.ReadLine(), System.Globalization.CultureInfo.GetCultureInfo("en-US")); if (!Auxiliary.isInputForFuncValid(Variant.f2, a, b)) { break; } SimpleIterationMethod.applyMethod(Variant.f2, a, b, eps); break; case 4: Console.Clear(); LobachevskyMethod.applyMethod(eps); break; default: Console.Clear(); Console.WriteLine("Enter correct number of action!"); int.TryParse(Console.ReadLine(), out userInput); break; } Console.WriteLine("\n---------------------------------------------------->>>>>\n" + "Choose what you want:\n" + "0. Exit.\n" + "1. First equation in Horde Method.\n" + "2. Second equation in Steffensen's Method.\n" + "3. Second equation in Simple Iterations Method.\n" + "4. Output the roots of the algebraic equation by the Lobachevsky method."); int.TryParse(Console.ReadLine(), out userInput); } }