private void Form1_Load(object sender, EventArgs e) { double[] inp = new double[] { -36, 33, -10, 1, 17, -32, 1, -3, 5 }; mainChart.Series.Clear(); mainChart.Series.Add("Frequencies"); Func <double, double> foo = PolynomialFactory.getPolynomialFunction(inp); Func <double, double> fooD = PolynomialFactory.getPolynomialDerivative(inp); ImprovedNewtonRaphsonMethodD rootFinder = new ImprovedNewtonRaphsonMethodD(foo, fooD); double x1 = float.MinValue, x2 = 1.84; double equalsTo = 0; rootFinder.ExpandBracket(ref x1, ref x2, equalsTo); double root = rootFinder.FindRoot(x1, x2, equalsTo); MessageBox.Show("Root is: " + root + " x1: " + x1 + "; x2: " + x2); }
public void FindRootTest1() { Func <double, double> polynomial = x => (x - 1) * (x - 10) * (x - 18); Func <double, double> computeDerivative = x => 3 * x * x - 58 * x + 208; ImprovedNewtonRaphsonMethodD rootFinder = new ImprovedNewtonRaphsonMethodD(polynomial, computeDerivative); rootFinder.EpsilonX = Numeric.EpsilonD / 100; double xRoot = rootFinder.FindRoot(0, 2); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(4, 10); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(10, 4); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(10, 12); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(2, 0); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(0, 3); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(3, 0); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(-6, 2); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(-1, 1); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(-1, 1); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(0.9, 9.9); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(2, 3); Assert.IsTrue(double.IsNaN(xRoot)); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(3, 2); Assert.IsTrue(double.IsNaN(xRoot)); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); rootFinder.MaxNumberOfIterations = 1; xRoot = rootFinder.FindRoot(0, 1000); Assert.IsTrue(double.IsNaN(xRoot)); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); }
public void FindRootTest1() { Func<double, double> polynomial = x => (x - 1)*(x - 10)*(x - 18); Func<double, double> computeDerivative = x => 3 * x * x - 58 * x + 208; ImprovedNewtonRaphsonMethodD rootFinder = new ImprovedNewtonRaphsonMethodD(polynomial, computeDerivative); rootFinder.EpsilonX = Numeric.EpsilonD / 100; double xRoot = rootFinder.FindRoot(0, 2); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(4, 10); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(10, 4); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(10, 12); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(2, 0); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(0, 3); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(3, 0); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(-6, 2); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(-1, 1); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(-1, 1); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(0.9, 9.9); Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot))); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(2, 3); Assert.IsTrue(double.IsNaN(xRoot)); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); xRoot = rootFinder.FindRoot(3, 2); Assert.IsTrue(double.IsNaN(xRoot)); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); rootFinder.MaxNumberOfIterations = 1; xRoot = rootFinder.FindRoot(0, 1000); Assert.IsTrue(double.IsNaN(xRoot)); Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations); }