コード例 #1
0
        public double FindSolution(Equation eq, double eps)
        {
            Console.WriteLine("\nNewton's Modified Method:\n");
            Console.WriteLine($"Eps: {eps}");
            Console.WriteLine($"Start: {eq.start}\nEnd: {eq.end}\n");

            double x      = (eq.start + eq.end) / 2;
            double fvalue = eq.function(x);

            Console.WriteLine("Initial approximation: " + String.Format("{0:f8}", x) +
                              " Function value: " + String.Format("{0:f8}", fvalue));
            int i = 1;

            while (Math.Abs(fvalue) - eps > 0)
            {
                // fvalue == eq.function(x) - це зроблено для уникання багатьох перераховувань функції
                x     -= (fvalue / eq.derivative(x));
                fvalue = eq.function(x);
                Console.WriteLine($"Iteration {i}: " + String.Format("{0:f8}", x) +
                                  " Function value: " + String.Format("{0:f8}", fvalue));
                i++;
            }
            Console.WriteLine("Result: " + String.Format("{0:f8}", x) +
                              " Function value: " + String.Format("{0:f8}", fvalue));
            return(x);
        }
コード例 #2
0
        public double FindSolution(Equation eq, double eps)
        {
            Console.WriteLine("\nSecant Method\n");
            Console.WriteLine($"Eps: {eps}");
            Console.WriteLine($"Start: {eq.start}\nEnd: {eq.end}\n");

            double x      = (eq.start + eq.end) / 2;
            double fvalue = eq.function(x);

            Console.WriteLine("Initial approximation: x0 = " + String.Format("{0:f7}", x) +
                              " Function value: " + String.Format("{0:f7}", fvalue));

            double x_next = x - (fvalue / eq.derivative(x));

            fvalue = eq.function(x_next);
            Console.WriteLine("Second approximation: x1 = " + String.Format("{0:f7}", x_next) +
                              " Function value: " + String.Format("{0:f7}", fvalue));
            int i = 1;

            while (Math.Abs(fvalue) - eps > 0)
            {
                double t = x_next;
                // fvalue == eq.function(x_next) - це зроблено для уникання багатьох перераховувань функції
                x_next -= ((x_next - x) * fvalue) / (fvalue - eq.function(x));
                x       = t;
                fvalue  = eq.function(x_next);
                Console.WriteLine($"Iteration {i}: " + String.Format("{0:f7}", x_next) +
                                  " Function value: " + String.Format("{0:f7}", fvalue));
                i++;
            }
            Console.WriteLine("Result: " + String.Format("{0:f7}", x_next) +
                              " Function value: " + String.Format("{0:f7}", fvalue));
            return(x_next);
        }