public double[] nextResult(double[] previous) { // cr = x_n; // pr = x_(n-1); // nr = x_(n+1); var cr = previous [1]; var pr = previous [0]; var nr = cr - (Equation.F(cr) * (cr - pr)) / (Equation.F(cr) - Equation.F(pr)); return(new double[] { cr, nr }); }
public override double[] NextResult(double[] currentResult) { double x1 = currentResult [0]; double x2 = currentResult [1]; Console.WriteLine("Result #" + ++iterations + ": [" + x1 + ", " + x2 + "]"); double middle = (x1 + x2) / 2.0; if (Equation.F(x1) * Equation.F(middle) < 0) { temp = new double[] { x1, middle }; return(temp); } temp = new double[] { middle, x2 }; return(temp); }
public static void Main(string[] args) { Console.WriteLine("Nonlinear Equation Solving Methods, v1.0"); Console.WriteLine("by Paul Pukach a.k.a. Icebreaker"); Console.WriteLine(""); /* * Console.WriteLine ("Enter the desired precision (default = 1E-3)"); * double eps; * try * { * eps = double.Parse (Console.ReadLine ()); * } * catch * { * eps = 1E-3; * } * Console.WriteLine ("Enter the X range [a, b]:"); * var a = double.Parse (Console.ReadLine ()); * var b = double.Parse (Console.ReadLine ()); * * NonlinearSolving method = new Dychotomy (new double[]{a, b}, eps); * var baseResult = (method as Dychotomy).Result (); * Console.WriteLine ("Dychotomy:"); * Console.WriteLine ("Result: " + baseResult); * Console.WriteLine ("Iterations: " + method.iterations); * Console.WriteLine ("F(result) = " + Equation.F (baseResult)); * * Console.WriteLine ("Enter the desired precision for Iteration method (default = 1E-5)"); * try * { * eps = double.Parse (Console.ReadLine ()); * } * catch * { * eps = 1E-5; * } * method = new IterationMethod (baseResult, eps); * var result = (method as IterationMethod).Result (); * * Console.WriteLine (""); * Console.WriteLine ("Iteration Method:"); * Console.WriteLine ("Result: " + result); * Console.WriteLine ("Iterations: " + method.iterations); * * Console.WriteLine ("F(result) = " + Equation.F (result)); */ var eps = 1E-3; Console.WriteLine("Enter the desired precision for Dychotomy method (default = 1E-3)"); try { eps = double.Parse(Console.ReadLine()); } catch { eps = 1E-3; } Console.WriteLine("Enter the X range [a, b]:"); var a = double.Parse(Console.ReadLine()); var b = double.Parse(Console.ReadLine()); NonlinearSolving method = new Dychotomy(new double[] { a, b }, eps); var baseResult = (method as Dychotomy).Result(); Console.WriteLine("Dychotomy:"); Console.WriteLine("Result: " + baseResult); Console.WriteLine("Iterations: " + method.iterations); Console.WriteLine("F(result) = " + Equation.F(baseResult)); Console.WriteLine("Enter the desired precision for Cord method (default = 1E-7)"); try { eps = double.Parse(Console.ReadLine()); } catch { eps = 1E-7; } CordMethod m = new CordMethod(eps); double res = m.Result((method as Dychotomy).temp); Console.WriteLine(""); Console.WriteLine("Cord Method:"); Console.WriteLine("Result: " + res); Console.WriteLine("Iterations: " + m.iterations); Console.WriteLine("F(result) = " + Equation.F(res)); }