Exemplo n.º 1
0
 public void FindNthRoot_Positive_Test()
 {
     Assert.AreEqual(FindRoot.FindNthRoot(1, 5, 0.0001), 1);
     Assert.AreEqual(FindRoot.FindNthRoot(0.04100625, 4, 0.0001), 0.45);
     Assert.AreEqual(FindRoot.FindNthRoot(0.0279936, 7, 0.0001), 0.6);
     Assert.AreEqual(FindRoot.FindNthRoot(-0.008, 3, 0.1), -0.24);
 }
Exemplo n.º 2
0
        public void T02_Multidimensional()
        {
            IDifferentiableVVFunction function = new TwoDimensionalFunction();

            Test(FindRoot.GlobalNewton, function, 1, 1, 0, 4, 1e-15);
            Test(FindRoot.Broyden, function, 1, 1, 0, 4, 1e-15);

            // now try computing the Jacobian via finite differences rather than directly
            Test(FindRoot.GlobalNewton, (IVectorValuedFunction)function, 1, 1, 0, 4, 1.5e-8);
            Test(FindRoot.Broyden, (IVectorValuedFunction)function, 1, 1, 0, 4, 4e-9);

            // now try a function that has multiple roots, x^2 + y^2 = 10 and x+y = 0
            function = new MultiRootTwoDimensionalFunction();
            // the initial point has to be in a correct quadrant (x positive and y negative, or vice versa) or it will fail to find a root
            double[] point = new double[2] {
                0.1, -1
            };
            Assert.IsTrue(FindRoot.GlobalNewton(function, point));
            Assert.AreEqual(Math.Sqrt(5), point[0], 1e-15);
            Assert.AreEqual(-Math.Sqrt(5), point[1], 1e-15);

            point = new double[2] {
                0.1, -1
            };
            Assert.IsTrue(FindRoot.Broyden(function, point));
            Assert.AreEqual(Math.Sqrt(5), point[0], 1.5e-11);
            Assert.AreEqual(-Math.Sqrt(5), point[1], 1.5e-11);
        }
 public void FindRoots_1_5_00001_Expect1(double element, int pow, double eps) => Assert.AreEqual(1, FindRoot.FindNthRoot(element, pow, eps), 0.0001);
Exemplo n.º 4
0
 public void FindRoot_FindNthRoot_ArgumentOutOfRangeException(double number, int degree, double delta, double expected)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => FindRoot.FindNthRoot(number, degree, delta));
 }
Exemplo n.º 5
0
 public void FindNthRoot_Negative_Test()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => FindRoot.FindNthRoot(15, 5, 12));
 }
Exemplo n.º 6
0
 public void NewTonFormula_ArgumentOutOfRangeException(double number, int n, double eps)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => FindRoot.NewtonFormula(number, n, eps));
 }
Exemplo n.º 7
0
 public void FindRoot_FindNthRoot(double a, int n, double delta, double expected)
 {
     Assert.AreEqual(expected, FindRoot.FindNthRoot(a, n, delta), delta);
 }
Exemplo n.º 8
0
        public void T01_OneDimensional()
        {
            // this is pretty close to the minimum that i can make it while still passing, given the original implementation. if an
            // implementation degrades substantially (with these functions, anyway), this should catch it
            const double Accuracy = 1.77636e-15;

            // this is a simple parabola with a double root at x=1. because of the double root, which means the function never crosses zero, only
            // touches it, many methods have more trouble with it. in particular, only unbounded newton raphson is able to find it without having
            // the root at one of the interval boundaries
            DifferentiableFunction function = new DifferentiableFunction(x => (x - 1) * (x - 1), x => 2 * x - 2); // f(x) = (x-1)^2

            // test unbounded newton raphson with a wide interval
            Assert.AreEqual(1, FindRoot.UnboundedNewtonRaphson(function, new RootBracket(-10, 10)), Accuracy);
            // the others need the root to be at one of the boundaries, although this is a trivial case for any method. make sure it works from
            // both edges for all methods
            Assert.AreEqual(1, FindRoot.BoundedNewtonRaphson(function, new RootBracket(1, 10)), Accuracy);
            Assert.AreEqual(1, FindRoot.BoundedNewtonRaphson(function, new RootBracket(-10, 1)), Accuracy);
            Assert.AreEqual(1, FindRoot.Brent(function, new RootBracket(1, 10)), Accuracy);
            Assert.AreEqual(1, FindRoot.Brent(function, new RootBracket(-10, 1)), Accuracy);
            Assert.AreEqual(1, FindRoot.Subdivide(function, new RootBracket(1, 10)), Accuracy);
            Assert.AreEqual(1, FindRoot.Subdivide(function, new RootBracket(-10, 1)), Accuracy);

            // this is a parabola with roots at x=0 and x=2. since it crosses zero, it should be amenable to many different methods
            function = new DifferentiableFunction(x => (x - 1) * (x - 1) - 1, x => 2 * x - 2); // f(x) = (x-1)^2 - 1

            // first, let's try some root bracketing
            RootBracket interval = new RootBracket(0.5, 1.5);

            // bracket outwards
            Assert.IsTrue(FindRoot.BracketOutward(function, ref interval));
            Assert.IsTrue(interval.Min <= 0 && interval.Max >= 0 || interval.Min <= 2 && interval.Max >= 2); // make sure it brackets a root
            // bracket inwards. since interval, when divided into 20 pieces, will have the roots exactly on the boundaries, the sub intervals
            // should also (although that's not something we need to verify)
            interval = new RootBracket(-10, 10);
            bool foundZero = false, foundTwo = false;

            foreach (RootBracket sub in FindRoot.BracketInward(function, interval, 20))
            {
                if (sub.Min <= 0 && sub.Max >= 0)
                {
                    foundZero = true;
                }
                if (sub.Min <= 2 && sub.Max >= 2)
                {
                    foundTwo = true;
                }
                Assert.IsTrue(sub.Min <= 0 && sub.Max >= 0 || sub.Min <= 2 && sub.Max >= 2);
            }
            Assert.IsTrue(foundZero && foundTwo);

            // try again, using an interval that doesn't divide evenly (and therefore won't provide cases that are trivial to solve)
            interval  = new RootBracket(-8, 9);
            foundZero = foundTwo = false;
            foreach (RootBracket sub in FindRoot.BracketInward(function, interval, 20))
            {
                double root = -1;
                if (sub.Min <= 0 && sub.Max >= 0)
                {
                    foundZero = true;
                    root      = 0;
                }
                else if (sub.Min <= 2 && sub.Max >= 2)
                {
                    foundTwo = true;
                    root     = 2;
                }
                else
                {
                    Assert.Fail();
                }

                // ensure that all methods find the root
                Assert.AreEqual(root, FindRoot.BoundedNewtonRaphson(function, sub), Accuracy);
                Assert.AreEqual(root, FindRoot.Brent(function, sub), Accuracy);
                Assert.AreEqual(root, FindRoot.Subdivide(function, sub), Accuracy);
                Assert.AreEqual(root, FindRoot.UnboundedNewtonRaphson(function, sub), Accuracy);
            }
            Assert.IsTrue(foundZero && foundTwo);

            // ensure that unbounded newton-raphson fails properly when there's no root
            function = new DifferentiableFunction(x => x * x + 1, x => 2 * x); // f(x) = x^2+1, a parabola with no root
            interval = new RootBracket(-1, 1);
            TestHelpers.TestException <RootNotFoundException>(delegate { FindRoot.UnboundedNewtonRaphson(function, interval); });
            // ensure that the others complain about the root not being bracketed
            TestHelpers.TestException <ArgumentException>(delegate { FindRoot.BoundedNewtonRaphson(function, interval); });
            TestHelpers.TestException <ArgumentException>(delegate { FindRoot.Brent(function, interval); });
            TestHelpers.TestException <ArgumentException>(delegate { FindRoot.Subdivide(function, interval); });
            // ensure that bracketing fails as it should
            Assert.IsFalse(FindRoot.BracketOutward(function, ref interval));
            Assert.AreEqual(0, FindRoot.BracketInward(function, new RootBracket(-10, 10), 20).Count());
        }
Exemplo n.º 9
0
 public double NewtonFormula_PositiveTests(double number, int n, double eps, int round)
 {
     return(Math.Round(FindRoot.NewtonFormula(number, n, eps), round));
 }
Exemplo n.º 10
0
Arquivo: Form4.cs Projeto: a-27m/vssdb
        private void buttonSolve_Click(object sender, EventArgs e)
        {
            if (checkAutoSearch.Checked)
            {
                AutoFindRoots(); return;
            }

            double p0, p1, p2;
            double eps;

            errorProvider.Clear();

            try
            { eps = float.Parse(textE.Text); }
            catch (FormatException)
            { errorProvider.SetError(textE, "Wrong float number"); return; }
            if (eps < 0)
            {
                errorProvider.SetError(textE, "Has to be > 0");
                return;
            }

            double          res   = double.NaN;
            DekartForm      dForm = null;
            List <PointF[]> lines = null;

            switch (selectedSolMeth)
            {
                #region Method Still Point
            case SolutionMethod.StillPoint:

                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }

                dForm      = new DekartForm(100, 100, 320, 300);
                dForm.Size = new Size(700, 500);

                dForm.AddGraphic(g, -5f, 5f, DrawModes.DrawLines, Color.Green);
                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Gray);
                dForm.AddGraphic(bisectress, -5f, 5f, DrawModes.DrawLines, Color.Blue);

                res = FindRoot.StillPointMethod(f, p0, eps, out lines, false);
                break;
                #endregion

                #region Method Bisection
            case SolutionMethod.Bisection:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }
                try
                { p1 = float.Parse(textP1.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP1, "Wrong float number"); return; }

                dForm = new DekartForm(100, 100, 175, 300);
                dForm.Use_IsVisible = false;
                dForm.Size          = new Size(350, 600);

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.Bisection(f, p0, p1, eps, out lines, false);
                break;

                #endregion

                #region Method Newtone-Rafson
            case SolutionMethod.NewtoneRafson:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }

                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.Size          = new Size(750, 600);
                dForm.Use_IsVisible = false;

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.NewtoneRafson(f, df, p0, eps, out lines, false);
                break;

                #endregion

                #region Method Cuttings
            case SolutionMethod.Cuttings:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }
                try
                { p1 = float.Parse(textP1.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP1, "Wrong float number"); return; }

                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.Size          = new Size(750, 600);
                dForm.Use_IsVisible = false;

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.Cuttings(f, p0, p1, eps, out lines, false);
                break;

                #endregion

                #region Method Hord
            case SolutionMethod.Hord:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }
                try
                { p1 = float.Parse(textP1.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP1, "Wrong float number"); return; }

                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.Size          = new Size(750, 600);
                dForm.Use_IsVisible = false;

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.Hord(f, d2f, p0, p1, eps, out lines, false);

                break;

                #endregion

                #region Method Muller
            case SolutionMethod.Muller:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }
                try
                { p1 = float.Parse(textP1.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP1, "Wrong float number"); return; }
                try
                { p2 = float.Parse(textP2.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP2, "Wrong float number"); return; }

                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.Size          = new Size(750, 600);
                dForm.Use_IsVisible = false;

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.Muller(f, p0, p1, p2, eps, ref dForm, false);

                lines = null;
                break;
                #endregion

            default:
                return;
            }

            #region Print results

            if (lines != null)
            {
                foreach (PointF[] pts in lines)
                {
                    dForm.AddPolygon(Color.Red, DrawModes.DrawLines, pts);
                }
            }
            dForm.Show();
            dForm.Update2();

            listRoots.Items.Clear();
            listY.Items.Clear();

            if (double.IsNaN(res))
            {
                MessageBox.Show("Корни не найдены.");
                return;
            }

            listRoots.Items.Add("x" + (listRoots.Items.Count + 1) + " = "
                                + res.ToString("F16"));
            listY.Items.Add("y" + (listY.Items.Count + 1) + " = "
                            + f(res).ToString("F16"));
            #endregion
        }
Exemplo n.º 11
0
 public double FindNthRootTest_Number_Degree_Precision_ReturnsResult(double number, int degree, double precision)
 {
     return(FindRoot.FindNthRoot(number, degree, precision));
 }
 public void FindRoots_0004241979_9_000000001_Expect0545(double element, int pow, double eps)
 {
     Assert.AreEqual(0.545, FindRoot.FindNthRoot(element, pow, eps), 0.00000001);
 }
 public void FindRoots_8_15_M06_ArgumentException(double element, int pow, double eps)
 {
     Assert.Throws <ArgumentException>(() => FindRoot.FindNthRoot(element, pow, eps));
 }
 public void FindRoots_M0008_3_01_ExpectM02(double element, int pow, double eps)
 {
     Assert.AreEqual(-0.2, FindRoot.FindNthRoot(element, pow, eps), 0.1);
 }
 public void FindRoots_00081_4_01_Expect03(double element, int pow, double eps)
 {
     Assert.AreEqual(0.3, FindRoot.FindNthRoot(element, pow, eps), 0.1);
 }
 public void FindRoots_00279936_7_00001_Expect06(double element, int pow, double eps)
 {
     Assert.AreEqual(0.6, FindRoot.FindNthRoot(element, pow, eps), 0.0001);
 }
 public void FindRoots_004100625_4_00001_Expect045(double element, int pow, double eps)
 {
     Assert.AreEqual(0.45, FindRoot.FindNthRoot(element, pow, eps), 0.0001);
 }
 public void FindRoots_0001_3_00001_Expect01(double element, int pow, double eps)
 {
     Assert.AreEqual(0.1, FindRoot.FindNthRoot(element, pow, eps), 0.0001);
 }
Exemplo n.º 19
0
Arquivo: Form4.cs Projeto: a-27m/vssdb
        private void AutoFindRoots()
        {
            float  x1, x2;
            double eps;

            errorProvider.Clear();

            try
            { eps = float.Parse(textE.Text); }
            catch (FormatException)
            { errorProvider.SetError(textE, "Wrong float number"); return; }
            if (eps < 0)
            {
                errorProvider.SetError(textE, "Has to be > 0"); return;
            }

            try
            { x1 = float.Parse(textX1.Text); }
            catch (FormatException)
            { errorProvider.SetError(textX1, "Wrong float number"); return; }

            try
            { x2 = float.Parse(textX2.Text); }
            catch (FormatException)
            { errorProvider.SetError(textX2, "Wrong float number"); return; }

            float step = (x2 - x1) / 100f;

            DekartForm dForm;

            dForm = new DekartForm(100, 100, 175, 300);
            dForm.Use_IsVisible = false;
            dForm.Size          = new Size(350, 600);
            dForm.CenterView();

            listRoots.Items.Clear();
            listY.Items.Clear();

            roots = new List <double>();

            for (float x = x1; x < x2; x += step)
            {
                double          res   = double.NaN;
                List <PointF[]> lines = null;

                switch (selectedSolMeth)
                {
                    #region Method Still Point
                case SolutionMethod.StillPoint:
                    if (dForm.CountGraphics == 0)
                    {
                        dForm.AddGraphic(g, -5f, 5f,
                                         DrawModes.DrawLines, Color.Green);
                        dForm.AddGraphic(f, -5f, 5f,
                                         DrawModes.DrawLines, Color.Gray);
                        dForm.AddGraphic(bisectress, -5f, 5f,
                                         DrawModes.DrawLines, Color.Blue);
                    }

                    res = FindRoot.StillPointMethod(h, x, eps, out lines, true);
                    break;
                    #endregion

                    #region Method Bisection
                case SolutionMethod.Bisection:
                    if (dForm.CountGraphics == 0)
                    {
                        dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);
                    }

                    res = FindRoot.Bisection(h, x, x + step, eps, out lines, true);
                    break;
                    #endregion

                    //#region Method Newtone-Rafson
                    //case SolutionMethod.NewtoneRafson:
                    //    MessageBox.Show("How to evaluate dh/dx?");
                    //    return;
                    //    if ( dForm.CountGraphics == 0 )

                    //        dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                    //    res = FindRoot.NewtoneRafson(f, df, x, eps, out lines);
                    //    break;

                    //#endregion

                    #region Method Cuttings
                case SolutionMethod.Cuttings:
                    if (dForm.CountGraphics == 0)
                    {
                        dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);
                    }
                    res = FindRoot.Cuttings(h, x, x + step, eps, out lines, true);
                    break;
                    #endregion

                    //#region Method Hord
                    //case SolutionMethod.Hord:
                    //    if ( dForm.CountGraphics == 0 )
                    //        dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);
                    //    res = FindRoot.Hord(f, d2f, x, x + step, eps, out lines);
                    //    break;
                    //#endregion

                    #region Method Muller
                case SolutionMethod.Muller:
                    if (dForm.CountGraphics == 0)
                    {
                        dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);
                    }

                    res   = FindRoot.Muller(h, x - step, x, x + step, eps, ref dForm, true);
                    lines = null;
                    break;

                    #endregion
                default:
                    return;
                }

                if (lines != null)
                {
                    foreach (PointF[] pts in lines)
                    {
                        dForm.AddPolygon(Color.Red, DrawModes.DrawLines, pts);
                    }
                }

                if (double.IsNaN(res))
                {
                    continue;
                }
                if ((res > x2) || (res < x1))
                {
                    continue;
                }

                roots.Add(res);
            }
            roots.Sort();
            foreach (double p in roots)
            {
                listRoots.Items.Add("x" + (listRoots.Items.Count + 1) + " = "
                                    + p.ToString("F16"));
                listY.Items.Add("y" + (listY.Items.Count + 1) + " = "
                                + f(p).ToString("F16"));
            }

            dForm.Show();
            dForm.Update2();
        }
Exemplo n.º 20
0
 public void FindNthRoot_Number_Degree_Precision_ArgumentOutOfRangeException(double number, int degree, double precision)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => FindRoot.FindNthRoot(number, degree, precision));
 }