Beispiel #1
0
        public double Solve(Equasion eq, double a, double b)
        {
            MathFunction func = eq.Left - eq.Right;

            if (func.Calculate(a) * func.Calculate(b) > 0)
            {
                throw new Exception("Method cannot be applied!");
            }

            if (Math.Abs(func.Calculate(a)) <= epsilanIter)
            {
                return(a);
            }
            if (Math.Abs(func.Calculate(b)) <= epsilanIter)
            {
                return(b);
            }

            double xn = (a + b) / 2;

            while (Math.Abs(func.Calculate(xn)) > epsilanIter)
            {
                if (func.Calculate(xn) * func.Calculate(a) < 0)
                {
                    b = xn;
                }
                else if (func.Calculate(xn) * func.Calculate(b) < 0)
                {
                    a = xn;
                }
                xn = (a + b) / 2;
            }

            return(xn);
        }
Beispiel #2
0
        private void InitializeDataGridView(MathFunction func, KeyValuePair <double, double>[] result)
        {
            DataGridViewColumn xCol             = new DataGridViewColumn(),
                               approximationCol = new DataGridViewColumn(),
                               accurateCol      = new DataGridViewColumn();

            ConfigureColumn(ref xCol, xString);
            ConfigureColumn(ref approximationCol, approximateY);
            ConfigureColumn(ref accurateCol, accurateY);

            dataGridView1.Columns.Add(xCol);
            dataGridView1.Columns.Add(approximationCol);
            dataGridView1.Columns.Add(accurateCol);

            for (int i = 0; i < result.Length; i++)
            {
                var newRow = new DataGridViewRow();

                newRow.HeaderCell.Value = i.ToString();
                for (int j = 0; j < 3; j++)
                {
                    newRow.Cells.Add(new DataGridViewTextBoxCell());
                }

                newRow.Cells[0].Value = Math.Round(result[i].Key, digitsAfterComma);
                newRow.Cells[1].Value = Math.Round(result[i].Value, digitsAfterComma);
                newRow.Cells[2].Value = Math.Round(func.Calculate(result[i].Key), digitsAfterComma);

                dataGridView1.Rows.Add(newRow);
            }
        }
Beispiel #3
0
        public double LOperator(MathFunction yx, double x)
        {
            MathFunction der2 = yx.Derivative(2),
                         der1 = yx.Derivative(1);

            return(der2.Calculate(x) + PX.Calculate(x) * der1.Calculate(x) +
                   QX.Calculate(x) * yx.Calculate(x));
        }
        public KeyValuePair <double, double>[] Solve(BoundaryValueTask task, int n)
        {
            MathFunction[] u = new MathFunction[collocationN + 1];
            for (int i = 0; i <= collocationN; i++)
            {
                u[i] = UK(task, i);
            }

            double[,] matrix = new double[collocationN, collocationN];
            double[] vect = new double[collocationN];
            double   h    = (task.b - task.a) / (collocationN + 1);

            for (int i = 0; i < collocationN; i++)
            {
                double xi = task.a + (i + 1) * h;

                vect[i] = task.FX.Calculate(xi) - task.LOperator(u[0], xi);
                for (int j = 0; j < collocationN; j++)
                {
                    matrix[i, j] = task.LOperator(u[j + 1], xi);
                }
            }

            Vector       C  = method.Solve(new SLAE(matrix, vect));
            MathFunction yn = u[0];

            yn.Calculate(0);
            for (int i = 1; i <= collocationN; i++)
            {
                yn += C[i - 1] * u[i];
            }

            h = (task.b - task.a) / n;
            KeyValuePair <double, double>[] result = new KeyValuePair <double, double> [n + 1];
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = new KeyValuePair <double, double>(task.a + i * h, yn.Calculate(task.a + i * h));
            }

            return(result);
        }
        protected virtual double GenericIterationMethod(MathFunction func, MathFunction ksi, double a, double b, double x0)
        {
            double xn = x0, counter = 0;
            double fDerivativeMin = func.Derivative(1).MinValue(a, b);

            MathFunction iterFunc = new XFunction(1.0d) + ksi * func;

            do
            {
                xn = iterFunc.Calculate(xn);
                counter++;
            } while (counter < maxIterationCount && !(Math.Abs(func.Calculate(xn)) / fDerivativeMin <= epsilanIter));

            if (counter == maxIterationCount)
            {
                return(xn);                             //  double.PositiveInfinity;
            }
            return(xn);
        }
        private PointF[] PointsForFunction(MathFunction func, double a, double b)
        {
            int multiplier = a > b ? -1 : 1;
            int count      = Convert.ToInt32(multiplier * (b - a) / epsilon) + 1;

            List <PointF> points = new List <PointF>();

            for (int i = 0; i < count; i++)
            {
                var argument = (float)(a + multiplier * i * epsilon);
                var value    = (float)(func.Calculate(a + multiplier * i * epsilon));

                if (!float.IsNaN(value) && !float.IsInfinity(value))
                {
                    points.Add(new PointF(argument, value));
                }
            }

            return(points.Select(x => new PointF((float)FunctionXToScreenX(x.X), (float)FunctionYToScreenY(x.Y))).ToArray());
        }
        public double Solve(Equasion eq, double a, double b)
        {
            MathFunction func = eq.Left - eq.Right;

            MathFunction der1 = func.Derivative(1),
                         der2 = func.Derivative(2);

            if (!IsSuitable(der1, a, b) ||
                !IsSuitable(der2, a, b))
            {
                throw new Exception("Method can not be applied!");
            }

            double c = SuitableRandomArgumnent((double x) => { return(func.Calculate(x) * der2.Calculate(x) > 0); }, a, b);

            MathFunction ksi = IterationFunc(func, a, b, c);

            double x0 = InitialX(func, a, b, c);

            return(GenericIterationMethod(func, ksi, a, b, x0));
        }
Beispiel #8
0
        private static MathFunction <BigFloat> CalculateFunction(int power, BigFloat point, ref MathFunction <BigFloat> function, ref int curPower)
        {
            function = function.Derivative(power - curPower);
            curPower = power;
            BigFloat value = function.Calculate(point);

            BigInteger fact = Factorial.Calculate(power);

            value /= fact;

            if (power == 0)
            {
                return(value);
            }
            else if (power == 1)
            {
                return(value * (new XFunction <BigFloat>(1.0) - point));
            }
            else
            {
                return(new PowerFunction <BigFloat>(value, new XFunction <BigFloat>(1.0) - point, (BigFloat)power));
            }
        }
 private MathFunction IterationFunc(MathFunction func, double a, double b, double c)
 {
     return(-(new XFunction(1.0d) - c) / (func - func.Calculate(c)));
 }
        protected double InitialX(MathFunction func, double a, double b, double c)
        {
            double val = func.Calculate(c);

            return(SuitableRandomArgumnent((double x) => { return func.Calculate(x) * val < 0; }, a, b));
        }
 protected double InitialX(MathFunction func, double a, double b)
 {
     return SuitableRandomArgumnent((double arg) => { return func.Calculate(arg) * func.Derivative(2).Calculate(arg) < 0; }, a, b);
 }
        public GraphicsPath PathForSimpsonMethod(MathFunction func, double a, double b, int n)
        {
            GraphicsPath path = new GraphicsPath();

            double difference = (b - a) / n;

            for (int i = 0; i < n; i++)
            {
                double       x           = a + difference * i;
                MathFunction simpsonFunc = FunctionForSimpsonInterval(new PointF((float)x, (float)func.Calculate(x)),
                                                                      new PointF((float)(x + difference / 2), (float)func.Calculate(x + difference / 2)),
                                                                      new PointF((float)(x + difference), (float)func.Calculate(x + difference)));
                path.AddPath(PathForFunction(simpsonFunc, x, x + difference), false);
            }

            return(path);
        }
        public GraphicsPath PathForFunction(MathFunction func, double a, double b)
        {
            GraphicsPath path = new GraphicsPath();

            path.StartFigure();
            path.AddLines(PointsForFunction(func, a, b));

            path.AddLine(new PointF((float)FunctionXToScreenX(b), (float)FunctionYToScreenY(func.Calculate(b))),
                         new PointF((float)FunctionXToScreenX(b), (float)FunctionYToScreenY(0)));
            path.AddLine(new PointF((float)FunctionXToScreenX(b), (float)FunctionYToScreenY(0)),
                         new PointF((float)FunctionXToScreenX(a), (float)FunctionYToScreenY(0)));
            path.CloseFigure();

            return(path);
        }
        public GraphicsPath PathForTrapezoidMethod(MathFunction func, double a, double b, int n)
        {
            GraphicsPath path = new GraphicsPath();

            float difference = (float)(b - a) / n;

            for (int i = 0; i < n; i++)
            {
                float    x      = (float)a + difference * i;
                PointF[] points = new PointF[4];

                points[0] = new PointF((float)FunctionXToScreenX(x), (float)FunctionYToScreenY(func.Calculate(x)));
                points[1] = new PointF((float)FunctionXToScreenX(x + difference), (float)FunctionYToScreenY(func.Calculate(x + difference)));
                points[2] = new PointF((float)FunctionXToScreenX(x + difference), (float)FunctionYToScreenY(0));
                points[3] = new PointF((float)FunctionXToScreenX(x), (float)FunctionYToScreenY(0));


                path.StartFigure();
                path.AddLines(points);
                path.CloseFigure();
            }

            return(path);
        }
        public GraphicsPath PathForRectangularMethod(MathFunction func, RectangularMethodType type, double a, double b, int n)
        {
            GraphicsPath path       = new GraphicsPath();
            double       difference = (b - a) / n;

            for (int i = 0; i < n; i++)
            {
                double x = 0;
                switch (type)
                {
                case RectangularMethodType.Left:
                    x = a + difference * i;
                    break;

                case RectangularMethodType.Right:
                    x = a + difference * (i + 1);
                    break;

                case RectangularMethodType.Central:
                    x = a + difference * (i + 0.5);
                    break;

                default:
                    break;
                }

                path.AddRectangle(new RectangleF((float)FunctionXToScreenX(a + difference * i), (float)FunctionYToScreenY(func.Calculate(x)), (float)FunctionXToScreenX(x + difference) - (float)FunctionXToScreenX(x), (float)FunctionYToScreenY(0) - (float)FunctionYToScreenY(func.Calculate(x))));
            }

            return(path);
        }
        private PointF[] PointsForFunction(MathFunction func, double a, double b)
        {
            int count = Convert.ToInt32((b - a) / epsilan) + 1;

            PointF[] points = new PointF[Convert.ToInt32((b - a) / epsilan) + 1];
            for (int i = 0; i < count; i++)
            {
                points[i] = new PointF((float)FunctionXToScreenX(a + i * epsilan), (float)FunctionYToScreenY(func.Calculate(a + i * epsilan)));
            }

            return(points);
        }