Exemplo n.º 1
0
        private double[][] GenerateAMatrix(int N, MathExpression tFunc, MathExpression bFunc, MathExpression sigmaFunc)
        {
            double h = 1.0 / N;

            double[][] res = new double[N][];
            for (int i = 0; i < N; i++)
            {
                res[i] = new double[N];
                for (int j = 0; j < N; j++)
                {
                    if (i != N - 1)
                    {
                        if (i == j)
                        {
                            res[i][j] = (1.0 / h) * (tFunc.Calculate(h * (i + 1) - (h / 2.0)) + tFunc.Calculate(h * (i + 1) + (h / 2.0))) +
                                        (h / 3.0) *
                                        (sigmaFunc.Calculate(h * (i + 1) - (h / 2.0)) + sigmaFunc.Calculate(h * (i + 1) + (h / 2.0))) +
                                        (1.0 / 2.0) * (bFunc.Calculate(h * (i + 1) - (h / 2.0)) - bFunc.Calculate(h * (i + 1) + (h / 2.0)));
                        }
                        else if (j == i + 1)
                        {
                            res[i][j] = -(1.0 / h) * tFunc.Calculate(h * (i + 1) + (h / 2.0)) +
                                        (1.0 / 2.0) * bFunc.Calculate(h * (i + 1) + (h / 2.0)) +
                                        (h / 6.0) * sigmaFunc.Calculate(h * (i + 1) + (h / 2.0));
                        }
                        else if (j == i - 1)
                        {
                            res[i][j] = -(1.0 / h) * tFunc.Calculate(h * (i + 1) - (h / 2.0)) -
                                        (1.0 / 2.0) * bFunc.Calculate(h * (i + 1) - (h / 2.0)) +
                                        (h / 6.0) * sigmaFunc.Calculate(h * (i + 1) - (h / 2.0));
                        }
                        else
                        {
                            res[i][j] = 0;
                        }
                    }
                    else
                    {
                        if (j < N - 2)
                        {
                            res[i][j] = 0;
                        }
                        else if (j == N - 2)
                        {
                            res[i][j] = -(1.0 / h) * tFunc.Calculate(h * (i + 1) - (h / 2.0)) -
                                        (1.0 / 2.0) * bFunc.Calculate(h * (i + 1) - (h / 2.0)) +
                                        (h / 6.0) * sigmaFunc.Calculate(h * (i + 1) - (h / 2.0));
                        }
                        else if (j == N - 1)
                        {
                            res[i][j] = (1.0 / h) * tFunc.Calculate(h * (i + 1) - (h / 2.0)) +
                                        (1.0 / 2.0) * bFunc.Calculate(h * (i + 1) - (h / 2.0)) +
                                        (h / 3.0) * sigmaFunc.Calculate(h * (i + 1) - (h / 2.0));
                        }
                    }
                }
            }

            return(res);
        }
Exemplo n.º 2
0
        public double[] GenerateFVector(int N, MathExpression fFunc)
        {
            double h = 1.0 / N;

            double[] res = new double[N];
            for (int i = 0; i < N - 1; i++)
            {
                res[i] = (h / 2.0) * (fFunc.Calculate(h * (i + 1) - (h / 2.0)) + fFunc.Calculate(h * (i + 1) + (h / 2.0)));
            }
            res[N - 1] = (h / 2.0) * (fFunc.Calculate(h * N - (h / 2.0))) /*- q*/;
            return(res);
        }
Exemplo n.º 3
0
        private double[] GenerateFVector()
        {
            MathExpression fFunc = new MathExpression(fFunction);
            double         h     = 1.0 / n;

            double[] res = new double[n];
            for (int i = 0; i < n - 1; i++)
            {
                res[i] = (h / 2.0) * (fFunc.Calculate(h * (i + 1) - (h / 2.0)) + fFunc.Calculate(h * (i + 1) + (h / 2.0)));
            }
            res[n - 1] = (h / 2.0) * (fFunc.Calculate(h * n - (h / 2.0))) - q;
            return(res);
        }
Exemplo n.º 4
0
        public static double Calculate(double a, double b, int amountOfPartitions, MathExpression function, params Var[] variables)
        {
            List <double> Xk = new List <double>()
            {
                -0.8611363, -0.3399810, 0.3399810, 0.8611363
            };
            List <double> Ck = new List <double>()
            {
                0.3478548, 0.6521452, 0.6521452, 0.3478548
            };

            double result = 0;

            double step = (b - a) / amountOfPartitions;

            for (int i = 0; i < amountOfPartitions; i++)
            {
                double a_new = a + step * i;
                double b_new = a + step * (i + 1);

                double sum = 0;
                for (int j = 0; j < Xk.Count; j++)
                {
                    variables[0].Value = (a_new + b_new) / 2.0 + (b_new - a_new) / 2.0 * Xk[j];
                    sum += Ck[j] * function.Calculate(variables);
                }

                result += sum * (b_new - a_new) / 2.0;
            }

            return(result);
        }
Exemplo n.º 5
0
        private double GetENormSquared(double q1, double q2, double h, double x1)
        {
            double         midPoint  = x1 + h / 2.0;
            MathExpression muFunc    = new MathExpression(muFunction);
            MathExpression betaFunc  = new MathExpression(betaFunction);
            MathExpression sigmaFunc = new MathExpression(sigmaFunction);
            MathExpression fFunc     = new MathExpression(fFunction);
            double         qPrime    = (q2 - q1) / h;
            double         qMiddle   = (q2 + q1) / 2.0;
            double         Pe        = h * betaFunc.Calculate(midPoint) / muFunc.Calculate(midPoint);
            double         sh        = h * h * sigmaFunc.Calculate(midPoint) / muFunc.Calculate(midPoint);
            double         result    = (5.0 / 6.0) * Math.Pow(h, 3.0) * Math.Pow(fFunc.Calculate(midPoint) - qPrime * betaFunc.Calculate(midPoint) - sigmaFunc.Calculate(midPoint) * qMiddle, 2.0)
                                       / (muFunc.Calculate(midPoint) * (10 + Pe * sh));

            return(result);
        }
Exemplo n.º 6
0
        public static double Integrate(MathExpression function, double lowerBound, double higherBound)
        {
            double h = (higherBound - lowerBound) / percision;

            double result = 0;

            for (double i = lowerBound; i <= higherBound; i += h)
            {
                double a = function.Calculate(i);
                double b = function.Calculate(i + h);

                double S = ((a + b) / 2) * h;
                result += S;
            }

            return(result);
        }
Exemplo n.º 7
0
        private double[] GenerateFVector(double[] mesh)
        {
            MathExpression fFunc = new MathExpression(fFunction);

            double[] res = new double[mesh.Length - 1];
            double   h   = 0.0;

            for (int i = 1; i < res.Length; i++)
            {
                h = Math.Abs(mesh[i] - mesh[i - 1]);
                double hNext = Math.Abs(mesh[i + 1] - mesh[i]);
                res[i - 1] = (h / 2.0) * fFunc.Calculate(mesh[i] - (h / 2.0)) + (hNext / 2.0) * fFunc.Calculate(mesh[i] + (hNext / 2.0));
            }
            h = mesh[mesh.Length - 1] - mesh[mesh.Length - 2];
            res[res.Length - 1] = (h / 2.0) * (fFunc.Calculate(mesh[mesh.Length - 1] - (h / 2.0)));
            return(res);
        }
Exemplo n.º 8
0
        private double GetValueOnElement(double x1, double x2, double q1, double q2)
        {
            double         h         = Math.Abs(x1 - x2) / 2.0;
            double         midPoint  = x1 + h / 2.0;
            MathExpression muFunc    = new MathExpression(muFunction);
            MathExpression betaFunc  = new MathExpression(betaFunction);
            MathExpression sigmaFunc = new MathExpression(sigmaFunction);
            MathExpression fFunc     = new MathExpression(fFunction);
            double         mu        = muFunc.Calculate(midPoint);
            double         beta      = betaFunc.Calculate(midPoint);
            double         sigma     = sigmaFunc.Calculate(midPoint);
            double         f         = fFunc.Calculate(midPoint);
            double         pe        = h * beta / mu;
            double         sh        = h * h * sigma / mu;
            double         qPrime    = Math.Abs(q2 - q1) / h;
            double         qVal      = (q2 + q1) / 2.0;

            return((Math.Pow(h, 3.0) * Math.Pow((fFunc.Calculate(midPoint) - beta * qPrime - sigma * qVal), 2.0)) / (mu * (10 + pe * sh)));
        }
Exemplo n.º 9
0
        public List <double> Generate(double[] mesh)
        {
            MathExpression fFunc   = new MathExpression(_presizeFunction);
            List <double>  results = new List <double>();

            for (int i = 0; i < mesh.Length; i++)
            {
                results.Add(fFunc.Calculate(mesh[i]));
            }
            return(results);
        }
        public static List <double> Calculate_Fj(string func, List <double> points)
        {
            List <double> result = new List <double>();

            MathExpression function = new MathExpression(func);

            for (int i = 0; i < points.Count; i++)
            {
                result.Add(function.Calculate(points[i]));
            }

            return(result);
        }
Exemplo n.º 11
0
        private void buttonCalculateExpr_Click(object sender, RoutedEventArgs e)
        {
            listBoxTokens.Items.Clear();
            textBoxReversePolishNotation.Clear();
            textBoxResult.Clear();

            string         input = textBoxInput.Text;
            MathExpression expr  = new MathExpression(input);

            foreach (Token t in expr.Tokens)
            {
                listBoxTokens.Items.Add(t);
            }

            foreach (Token t in expr.ReversePolishNotation)
            {
                textBoxReversePolishNotation.Text += t.Lexeme;
            }

            double result = 0;

            if (textBoxVar.Text != "")
            {
                double var = double.Parse(textBoxVar.Text);
                result = expr.Calculate(var);
            }
            else
            {
                result = expr.Calculate();
            }
            textBoxResult.Text = result.ToString();

            MathExpression expr2 = new MathExpression("1/(x*y)");

            MessageBox.Show(expr2.Calculate(new Var("x", 2), new Var("y", 5)).ToString());
        }
Exemplo n.º 12
0
        private void buttonCalculate_Click(object sender, RoutedEventArgs e)
        {
            var x = 2;

            var formula1 = new MathExpression("40");
            var formula2 = new MathExpression("40*x");
            var formula3 = new MathExpression("40 * x");
            var formula4 = new MathExpression("3*x^2");

            var res1 = formula1.Calculate(x);
            var res2 = formula2.Calculate(x);
            var res3 = formula3.Calculate(x);
            var res4 = formula4.Calculate(x);


            try
            {
                var N     = int.Parse(textBoxN.Text);
                var miu   = new MathExpression(fixMinus(textBoxMuFunction.Text));
                var beta  = new MathExpression(fixMinus(textBoxBetaFunction.Text));
                var sigma = new MathExpression(fixMinus(textBoxSigmaFunction.Text));
                var f     = new MathExpression(fixMinus(textBoxFFunction.Text));

                var calculator = new Calculator_MSE(miu, beta, sigma, f, -1.0, 1.0, 0.0, 0.0);

                var result = calculator.CalculateApproximation(N);

                var points = new List <Point>();
                for (int i = 0; i < result.XValues.Length; i++)
                {
                    points.Add(new Point(result.XValues[i], result.UValues[i]));
                }

                drawGraphic(points, "u(x)");
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }

            buttonClear.IsEnabled = true;
        }
Exemplo n.º 13
0
 public void ComplexExpressionTest2()
 {
     expr = new MathExpression("sin(pi/2)+(x+2)*5");
     Assert.AreEqual(21, expr.Calculate(2));
 }
Exemplo n.º 14
0
        public void SeveralVariablesTest4()
        {
            MathExpression expr = new MathExpression("1/(x*y)");

            Assert.AreEqual(0.1, expr.Calculate(2, 5));
        }
Exemplo n.º 15
0
        private CalculationResult CalculateApproximation(int N, double[] discret_x)
        {
            double[]   L = new double[N];
            double[][] A = new double[N][];
            for (int i = 0; i < N; i++)
            {
                A[i] = new double[N];

                if (i > 0)
                {
                    Func <double, double> a__i_minus_1__i = new Func <double, double>((x) =>
                    {
                        double phi_i         = kurant_function(i, x);
                        double phi_i_minus_1 = kurant_function(i - 1, x);

                        double derivative_phi_i         = derivative_kurant_function(i, x);
                        double derivative_phi_i_minus_1 = derivative_kurant_function(i - 1, x);

                        return(_miu.Calculate(x) * derivative_phi_i_minus_1 * derivative_phi_i +
                               _beta.Calculate(x) * derivative_phi_i_minus_1 * phi_i +
                               _sigma.Calculate(x) * phi_i_minus_1 * phi_i);
                    });

                    A[i][i - 1] = IntegralCalculator.Integrate(a__i_minus_1__i, discret_x[i - 1], discret_x[i]);
                }

                Func <double, double> a__i__i = new Func <double, double>((x) =>
                {
                    double phi_i = kurant_function(i, x);

                    double derivative_phi_i = derivative_kurant_function(i, x);

                    return(_miu.Calculate(x) * derivative_phi_i * derivative_phi_i +
                           _beta.Calculate(x) * derivative_phi_i * phi_i +
                           _sigma.Calculate(x) * phi_i * phi_i);
                });

                A[i][i] = IntegralCalculator.Integrate(a__i__i, i > 0 ? discret_x[i - 1] : _a, (i < N - 1) ? discret_x[i + 1] : _b);

                if (i < N - 1)
                {
                    Func <double, double> a__i__i_plus_1 = new Func <double, double>((x) =>
                    {
                        double phi_i        = kurant_function(i, x);
                        double phi_i_plus_1 = kurant_function(i + 1, x);

                        double derivative_phi_i        = derivative_kurant_function(i, x);
                        double derivative_phi_i_plus_1 = derivative_kurant_function(i + 1, x);

                        return(_miu.Calculate(x) * derivative_phi_i * derivative_phi_i_plus_1 +
                               _beta.Calculate(x) * derivative_phi_i * phi_i_plus_1 +
                               _sigma.Calculate(x) * phi_i * phi_i_plus_1);
                    });

                    A[i][i + 1] = IntegralCalculator.Integrate(a__i__i_plus_1, discret_x[i], discret_x[i + 1]);
                }

                Func <double, double> l_func = new Func <double, double>((x) => _f.Calculate(x) * kurant_function(i, x));
                L[i] = IntegralCalculator.Integrate(l_func, _a, _b) /*+ _ua * _miu.Calculate(1) * kurant_function(i, 1)*/;
            }

            //A = GenerateAMatrix(N, _miu, _beta, _sigma);

            //L = GenerateFVector(N, _f);

            double[] q = MatrixCalculation.ProhoncaCalculation(A, L);

            var res = new Func <double, double>(x =>
            {
                var sum = 0.0;
                for (int i = 0; i < N; i++)
                {
                    sum += (q[i] * kurant_function(i, x));
                }

                return(_ua + sum);
            });

            var resVec = new double[N];

            for (int i = 0; i < N; i++)
            {
                if (i == N - 1)
                {
                    resVec[i] = 0.0;
                }
                else
                {
                    resVec[i] = res(discret_x[i]);
                }
            }

            return(new CalculationResult(discret_x, resVec));

            double kurant_function(int i, double x)
            {
                if (i == 0)
                {
                    return(0);
                }
                if (i == N - 1)
                {
                    return(0);
                }

                if (x >= _a && x <= discret_x[i - 1])
                {
                    return(0);
                }
                else if (x > discret_x[i - 1] && x <= discret_x[i])
                {
                    return((x - discret_x[i + 1]) / (discret_x[i + 1] - discret_x[i]));
                    //return ((x - discret_x[i - 1]) / (discret_x[i + 1] - discret_x[i]));
                }
                else if (x > discret_x[i] && x <= discret_x[i + 1])
                {
                    return(1 - ((x - discret_x[i + 1]) / (discret_x[i + 1] - discret_x[i])));
                    //return ((discret_x[i + 1] - x) / (discret_x[i + 1] - discret_x[i]));
                }
                else
                {
                    return(0);
                }
            }

            double derivative_kurant_function(int i, double x)
            {
                if (i == 0)
                {
                    return(0);
                }
                else if (i == N - 1)
                {
                    return(0);
                }
                else if (x >= _a && x <= discret_x[i - 1])
                {
                    return(0);
                }
                else if (x > discret_x[i - 1] && x <= discret_x[i])
                {
                    return(1 / (discret_x[i + 1] - discret_x[i]));
                }
                else if (x > discret_x[i] && x <= discret_x[i + 1])
                {
                    return(-(1 / (discret_x[i + 1] - discret_x[i])));
                }
                else
                {
                    return(0);
                }
            }
        }
Exemplo n.º 16
0
        public void SimpleTest2()
        {
            MathExpression expr = new MathExpression("x");

            Assert.AreEqual(2, expr.Calculate(2));
        }
Exemplo n.º 17
0
        public void SeveralVariablesTest5()
        {
            MathExpression expr = new MathExpression("x*y+y*z+z*x");

            Assert.AreEqual(11, expr.Calculate(1, 2, 3));
        }
Exemplo n.º 18
0
        public void SimpleTest3()
        {
            MathExpression expr = new MathExpression("x");

            Assert.AreEqual(0.1, expr.Calculate(0.1));
        }
Exemplo n.º 19
0
 public void PlusTest2()
 {
     expr = new MathExpression("x+2");
     Assert.AreEqual(5, expr.Calculate(3));
 }
Exemplo n.º 20
0
 public void MinusTest3()
 {
     expr = new MathExpression("sin(x)-2");
     Assert.AreEqual(-1, expr.Calculate(Math.PI / 2));
 }
Exemplo n.º 21
0
 public void MinusTest2()
 {
     expr = new MathExpression("x-2");
     Assert.AreEqual(1, expr.Calculate(3));
 }
Exemplo n.º 22
0
 public void MultiplicationTest2()
 {
     expr = new MathExpression("x*2");
     Assert.AreEqual(6, expr.Calculate(3));
 }
Exemplo n.º 23
0
 public double Sigma(double x)
 {
     return(_sigma.Calculate(x));
 }
Exemplo n.º 24
0
 public double Mu(double x)
 {
     return(_mu.Calculate(x));
 }
Exemplo n.º 25
0
 public void ComplexExpressionTest1()
 {
     expr = new MathExpression("x^2+2*x+1");
     Assert.AreEqual(9, expr.Calculate(2));
 }
Exemplo n.º 26
0
 public void DivisionTest2()
 {
     expr = new MathExpression("x/2");
     Assert.AreEqual(5, expr.Calculate(10));
 }
Exemplo n.º 27
0
 public double Beta(double x)
 {
     return(_beta.Calculate(x));
 }
Exemplo n.º 28
0
 public void ComplexExpressionTest3()
 {
     expr = new MathExpression("abs(2-4)+tg(pi)");
     Assert.AreEqual(2, expr.Calculate());
 }
Exemplo n.º 29
0
 public double F(double x)
 {
     return(_f.Calculate(x));
 }
Exemplo n.º 30
0
 public void ComplexExpressionTest4()
 {
     expr = new MathExpression("x^2 + 2,5");
     Assert.AreEqual(6.5, expr.Calculate(2));
 }