Пример #1
0
        //returns the point which axproximates the zero inside the interval
        public Vector2D Solve(double inf, double sup)
        {
            double a = inf, b = sup, x = 0;
            double fa = function(a), fb = function(b), fx = 0;

            bool success = false;

            //check if the interval is valid
            if (fa * fb > 0)
            {
                throw new CalculationException("Not valid interval", function);
            }

            //first iteration is used for providing a reference estimation
            x  = (a + b) / 2;
            fx = function(x);
            if (fx * fa < 0)
            {
                b  = x;
                fb = fx;
            }
            else if (fx * fb < 0)
            {
                a  = x;
                fa = fx;
            }
            stoppingCriteria.SetCriteria(new Vector2D(x, fx));

            for (int k = 0; k < maxIterations; k++)
            {
                x  = (a + b) / 2;
                fx = function(x);

                if (stoppingCriteria.FullfilCriteria(new Vector2D(x, fx)))
                {
                    success = true;
                    break;
                }

                if (fx * fa < 0)
                {
                    b  = x;
                    fb = fx;
                }
                else if (fx * fb < 0)
                {
                    a  = x;
                    fa = fx;
                }
            }

            if (!success)
            {
                throw new CalculationException("No convergence to the solution (exceded the maximum number of iterations)", function);
            }

            return(new Vector2D(x, fx));
        }
Пример #2
0
        public Vector2D Solve(double initialEstimation, int multiplicity = 1)
        {
            if (multiplicity < 1)
            {
                throw new ArgumentOutOfRangeException("multiplicity", "the multiplicity of the root must be equal or greater than 1");
            }

            double xn      = initialEstimation;
            double fn      = 0;
            double dfn     = 0;
            bool   success = false;

            stoppingCriteria.SetCriteria(new Vector2D(xn, function(xn)));

            for (int k = 0; k < maxIterations; k++)
            {
                fn  = function(xn);
                dfn = derivative(xn);

                if (dfn == 0 && fn != 0)
                {
                    throw new CalculationException("Cannot continue calculation, the function derivative is 0 in " + xn, function);
                }

                if (stoppingCriteria.FullfilCriteria(new Vector2D(xn, fn)))
                {
                    success = true;
                    break;
                }

                xn = xn - multiplicity * fn / dfn;
            }

            if (!success)
            {
                throw new CalculationException("No convergence to the solution (exceded the maximum number of iterations)", function);
            }

            return(new Vector2D(xn, fn));
        }
Пример #3
0
        public Vector2D Solve(double x0, double x1)
        {
            double xn1 = x1, xn0 = x0, x = 0;
            double fn1 = function(xn1), fn0 = function(xn0);
            bool   success = false;

            stoppingCriteria.SetCriteria(new Vector2D(xn1, function(fn1)));

            for (int k = 0; k < maxIterations; k++)
            {
                double d = (fn1 - fn0) / (xn1 - xn0);
                x = xn1 - fn1 / d;
                if (!x.IsFinite())
                {
                    throw new CalculationException("Found a non finite point in the succession to the root", function);
                }

                fn0 = fn1;
                xn0 = xn1;
                fn1 = function(x);
                xn1 = x;

                if (stoppingCriteria.FullfilCriteria(new Vector2D(xn1, fn1)))
                {
                    success = true;
                    break;
                }
            }

            if (!success)
            {
                throw new CalculationException("No convergence to the solution (exceded the maximum number of iterations)", function);
            }

            return(new Vector2D(xn1, fn1));
        }