public double FindRoot(Func <double, double> function, Func <double, double> derivative, double a, double b, double eps1, double eps2, ref int numOfIterations) { var bisectA = a; var bisectB = b; var bisectionresult = new Bisection().FindRoot(ref bisectA, ref bisectB, eps1, function, ref numOfIterations); Console.WriteLine("Interval after bisection: [{0};{1}]", bisectA, bisectB); double xPrev = (bisectA + bisectB) / 2; Console.Write("{0} ", xPrev); double x = 0; int i; for (i = 0; i < 100; i++) //prevent infinite loop { x = xPrev - function(xPrev) / derivative(xPrev); Console.Write("{0} ", x); if (Math.Abs(x - xPrev) < eps2) { break; } xPrev = x; } numOfIterations += i + 1; Console.WriteLine("Finished in {0} iterations", i + 1); return(x); }
static void Main(string[] args) { Func <double, double> fn1 = x => (x * Math.Pow(Math.E, 0.8 * x)) - 12; //Andris x*e^(0.8*x) - 12 Func <double, double> fn2 = x => (x * x * Math.Sin(x)) - 0.12; //Alina (x^2)*sinx - 0.12 Func <double, double> fn3 = x => (Math.Pow(Math.Log(x), 2) / x) - 12; //Moritz ((lnx)^2)/x - 12 Func <double, double> fn1d = x => 0.8 * Math.Pow(Math.E, (0.8 * x)) * (x + 1.25); //Andris Func <double, double> fn2d = x => x * (x * Math.Cos(x) + 2 * Math.Sin(x)); //Alina Func <double, double> fn3d = x => - ((-2 + Math.Log(x)) * Math.Log(x)) / Math.Pow(x, 2); //Moritz var bisection = new Algorithms.Bisection(); Console.WriteLine("Bisection"); double a = 2; double b = 3; int numOfIterations = 0; Console.WriteLine(bisection.FindRoot(ref a, ref b, 0.0001, fn1, ref numOfIterations)); Console.WriteLine(numOfIterations); a = 3; b = 4; numOfIterations = 0; Console.WriteLine(bisection.FindRoot(ref a, ref b, 0.0001, fn2, ref numOfIterations)); Console.WriteLine(numOfIterations); a = 0; b = 1; numOfIterations = 0; Console.WriteLine(bisection.FindRoot(ref a, ref b, 0.0001, fn3, ref numOfIterations)); Console.WriteLine(numOfIterations); numOfIterations = 0; Console.WriteLine(); var newtons = new Algorithms.NewtonsCombined(); Console.WriteLine("Andris x*e^(0.8*x) - 12 - Newtons Method - eps = 0.01, eps for bisection = 0.1"); newtons.FindRoot(fn1, fn1d, 2, 3, 0.1, 0.0001, ref numOfIterations); Console.WriteLine(numOfIterations); numOfIterations = 0; Console.WriteLine("Alina (x^2)*sinx - 0.12 - Newtons Method - eps = 0.01, eps for bisection = 0.1"); newtons.FindRoot(fn2, fn2d, 3, 4, 0.1, 0.0001, ref numOfIterations); Console.WriteLine(numOfIterations); numOfIterations = 0; Console.WriteLine("Moritz ((lnx)^2)/x - 12 - Newtons Method - eps = 0.01, eps for bisection = 0.1"); newtons.FindRoot(fn3, fn3d, 0, 1, 0.1, 0.0001, ref numOfIterations); Console.WriteLine(numOfIterations); Console.Read(); }