Example #1
0
        public double SquareApproximation(double a, double b)
        {
            FileLine header = new FileLine();

            header.X_prev = "y = 5x^3 - 3x^5";
            Output.Add(header);
            double h, x1, x2, x3, F1, F2, F3, x_min, F_min;

            x2 = (a + b) / 2;
            x1 = a;
            x3 = b;
            h  = delta;
            int n = 0;

            x_min = x2;
            F_min = F(x_min);
            int intervals = 10;

            do
            {
                FileLine line = new FileLine();
                n++;
                //x1 = (a + b) / 2;
                h = (x3 - x1) / (intervals + 1);
                Tuple <double, double>[] points = new Tuple <double, double> [intervals + 1];
                for (int i = 1; i <= intervals; i++)
                {
                    points[i] = new Tuple <double, double>(x1 + h * i, F(x1 + h * i));
                }
                double[,] matrix = new double[3, 4];
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        matrix[i, j] = 0;
                    }
                }
                matrix[0, 0] = intervals;
                for (int i = 1; i <= intervals; i++)
                {
                    matrix[1, 0] += points[i].Item1;
                    matrix[2, 0] += points[i].Item1 * points[i].Item1;
                    matrix[2, 1] += points[i].Item1 * points[i].Item1 * points[i].Item1;
                    matrix[2, 2] += points[i].Item1 * points[i].Item1 * points[i].Item1 * points[i].Item1;
                    matrix[0, 3] += points[i].Item2;
                    matrix[1, 3] += points[i].Item2 * points[i].Item1;
                    matrix[2, 3] += points[i].Item1 * points[i].Item1 * points[i].Item2;
                }
                matrix[0, 1] = matrix[1, 0];
                matrix[1, 1] = matrix[2, 0];
                matrix[0, 2] = matrix[2, 0];
                matrix[1, 2] = matrix[2, 1];
                var result = KrammerCalc(matrix);
                if (result.Item3 == 0)
                {
                    break;
                }
                x_min = -1 * result.Item2 / (2 * result.Item3);
                double A = result.Item3;
                double B = result.Item2;
                double C = result.Item1;
                //F_min = A * x_min * x_min + B * x_min + C;
                //F2 = A * x2 * x2 + B * x2 * C;
                F_min = F(x_min);
                F2    = F(x2);
                line.Iteration_number = n.ToString();
                line.X_prev           = x2.ToString();
                line.X_min            = x_min.ToString();
                line.Func_prev        = F2.ToString();
                line.Func_min         = F_min.ToString();
                line.a = x1.ToString();
                line.b = x3.ToString();
                Output.Add(line);
                //F1 = F(a);
                //F3 = F(b);
                if (Math.Abs(x2 - x_min) < delta)
                {
                    break;
                }
                else
                {
                    if (x1 < x_min && x_min < x2)
                    {
                        if (F_min < F2)
                        {
                            x3 = x2;
                            F3 = F2;
                            x2 = x_min;
                            F2 = F_min;
                        }
                        else
                        {
                            x1 = x_min;
                            F1 = F_min;
                        }
                    }
                    else if (x2 < x_min && x_min < x3)
                    {
                        if (F_min < F2)
                        {
                            x1 = x2;
                            F1 = F2;
                            x2 = x_min;
                            F2 = F_min;
                        }
                        else
                        {
                            x3 = x_min;
                            F3 = F_min;
                        }
                    }
                }
            } while (n < 30);
            //Print(n);
            FileLine Down_header = new FileLine();

            Down_header.X_prev    = "x_min = ";
            Down_header.X_min     = x_min.ToString();
            Down_header.Func_prev = "f(x_min) = ";
            Down_header.Func_min  = F_min.ToString();
            Output.Add(Down_header);
            SaveToCSV("X_polynom_approximation");
            return(x_min);
        }
Example #2
0
        public double SquareInterpolation(double a, double b)
        {
            FileLine header = new FileLine();

            header.X_prev = "y = 5x^3 - 3x^5";
            Output.Add(header);
            int    n = 0;
            double x1 = a;
            double x3 = b;
            double x2 = (b + a) / 2;
            double x_min, F_min;
            double F1, F2, F3;

            F1 = F(x1);
            F2 = F(x2);
            F3 = F(x3);
            do
            {
                FileLine line = new FileLine();
                n++;
                double[,] matrix = new double[3, 4];

                matrix[0, 0] = x1 * x1;
                matrix[1, 0] = x2 * x2;
                matrix[2, 0] = x3 * x3;
                matrix[0, 1] = x1;
                matrix[1, 1] = x2;
                matrix[2, 1] = x3;
                matrix[0, 2] = 1;
                matrix[1, 2] = 1;
                matrix[2, 2] = 1;
                matrix[0, 3] = F1;
                matrix[1, 3] = F2;
                matrix[2, 3] = F3;
                var    res = KrammerCalc(matrix);
                double c2  = res.Item1;
                double c1  = res.Item2;
                x_min = -c1 / (2 * c2);
                F_min = F(x_min);
                line.Iteration_number = n.ToString();
                line.X_prev           = x2.ToString();
                line.X_min            = x_min.ToString();
                line.Func_prev        = F2.ToString();
                line.Func_min         = F_min.ToString();
                line.a = x1.ToString();
                line.b = x3.ToString();
                Output.Add(line);
                if (Math.Abs(x2 - x_min) < delta)
                {
                    break;
                    //return x_min;
                }
                else
                {
                    if (x1 < x_min && x_min < x2)
                    {
                        if (F_min < F2)
                        {
                            x3 = x2;
                            F3 = F2;
                            x2 = x_min;
                            F2 = F_min;
                        }
                        else
                        {
                            x1 = x_min;
                            F1 = F_min;
                        }
                    }
                    else if (x2 < x_min && x_min < x3)
                    {
                        if (F_min < F2)
                        {
                            x1 = x2;
                            F1 = F2;
                            x2 = x_min;
                            F2 = F_min;
                        }
                        else
                        {
                            x3 = x_min;
                            F3 = F_min;
                        }
                    }
                    //x2 = x_min;
                }
            } while (true);
            FileLine Down_header = new FileLine();

            Down_header.X_prev    = "x_min = ";
            Down_header.X_min     = x_min.ToString();
            Down_header.Func_prev = "f(x_min) = ";
            Down_header.Func_min  = F_min.ToString();
            Output.Add(Down_header);
            SaveToCSV("X_polynom_interpolation");
            return(x_min);
        }