Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Function      f      = new Function((x, y) => Math.Sin(x) - Math.Cos(y));
            EulerModified eulerM = new EulerModified(f, 0, 1, 1, 5);
            EulerSimple   eulerS = new EulerSimple(f, 0, 1, 1, 5);
            RungeKutta4   rk4    = new RungeKutta4(f, 0, 1, 1, 5);

            double[,] MAT = new double[, ] {
                { 1, 23, 32, 1 }, { 1, 5, 1, 1 }, { 32, 1, 1, 1 }, { 1, 10, 1, 1 }
            };

            // double[,] MAT = new double[,] { { 3 ,0 ,2 }, { 2,0,-2 }, { 0,1,1 } };

            MatrixInverse MATINV = new MatrixInverse();

            MAT = MATINV.InvMatrix(MAT, 4);



            double[,] EQU = new double[, ] {
                { 1, 2, -1 }, { 3, 5, -1 }, { -2, -1, -2 }
            };
            double[]     ANS    = new double[] { 6, 2, 4 };
            MatInvMethod SOLVER = new MatInvMethod();

            SOLVER.MatrixInverseMethod(EQU, ANS, 3);
            double[] result = SOLVER.GetSolution();
        }
Exemplo n.º 2
0
        //http://www.exponenta.ru/educat/class/courses/ode/theme2/mathcad/ex1/ex1.htm
        public void EulerModifiedTest1()
        {
            Function      f     = new Function((x, y) => Math.Sin(x) - Math.Cos(y));
            double        eps   = 0.001;
            EulerModified euler = new EulerModified(f, 0, 1, 1, 5);

            Assert.AreEqual(0, euler.Result[0, 0]);
            Assert.AreEqual(1, euler.Result[0, 1]);
            Assert.AreEqual(0.4, euler.Result[2, 0]);
            Assert.AreEqual(0.2, euler.Step);
            Assert.AreEqual(0.806, euler.Result[2, 1]);
        }
Exemplo n.º 3
0
        public static void Main(string[] args)
        {
            double begin = 0;
            double end = 3.14;
            double y0 = 1;
            int pointsNum;

            Console.WriteLine("y'(x) = x^2 + y\n\nbegin = {0}\nend = {1}\ny0 = {2}", begin, end, y0);
            Console.Write("Vvedit' chyslo rozbytt'a: ");
            pointsNum = Convert.ToInt32(Console.ReadLine());

            EulerCorrected ec = new EulerCorrected(Funct, begin, end, y0, pointsNum);
            EulerModified em = new EulerModified(Funct, begin, end, y0, pointsNum);
            EulerSimple es = new EulerSimple(Funct, begin, end, y0, pointsNum);
            RungeKutta4 rk = new RungeKutta4(Funct, begin, end, y0, pointsNum);

            Console.WriteLine("\nEulerCorrected");
            double[,] d = ec.GetSolution();
            for (int i = 0; i < d.Length/2; i++)
                Console.WriteLine("i={0}:\th = {1:f4}\t{2:f4}", i, d[1,i], d[0,i]);

            Console.WriteLine("\nEulerModified");
            d = em.GetSolution();
            for (int i = 0; i < d.Length/2; i++)
                Console.WriteLine("i={0}:\th = {1:f4}\t{2:f4}", i, d[1,i], d[0,i]);

            Console.WriteLine("\nEulerSimple");
            d = es.GetSolution();
            for (int i = 0; i < d.Length/2; i++)
                Console.WriteLine("i={0}:\th = {1:f4}\t{2:f4}", i, d[1,i], d[0,i]);

            Console.WriteLine("\nRungeKutta4");
            d = rk.GetSolution();
            for (int i = 0; i < d.Length/2; i++)
                Console.WriteLine("i={0}:\th = {1:f4}\t{2:f4}", i, d[1,i], d[0,i]);

            Console.ReadKey(true);
        }
Exemplo n.º 4
0
        public void ExecuteMethod(
            string nameMethod,
            double param1,
            double param2,
            double param3,
            double param4,
            string testFunction,
            int rangeArray,
            double[] LinSysMasA,
            double[,] LinSysMatrixB,
            double[] massX,
            double[] massF,
            double[] massW,
            double pointInterpolation,
            double[,]  MatrixAlgebraA,
            double pointPercentile,
            double pointGenerator)
        {
            TestFunction = testFunction;
            switch (nameMethod)
            {
            //*** Approximate decision of equalization f(x)=0 ***
            case "Bisection Method":
            {
                Bisection bisect = new Bisection(new FunctionOne(TestFunBisection), param1, param2, param3);
                result = "\n Result of the program: \n" +
                         "    x= " + string.Format("{0:f" + precision + "}", bisect.Result);
            }
            break;

            case "Chord Method":
            {
                Сhord chord = new Сhord(new FunctionOne(TestFunNewton), new FunctionOne(TestFunNewton2), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", chord.GetSolution());
            }
            break;

            case "Iteration Method":
            {
                IterationMethod itermet = new IterationMethod(new FunctionOne(TestFunIteration), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", itermet.GetSolution());
            }
            break;

            case "Newton Method":
            {
                Newton newton = new Newton(new FunctionOne(TestFunNewton), new FunctionOne(TestFunNewton2), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", newton.GetSolution());
            }
            break;

            //*** Differential Equations ***
            case "Euler Simple":
            {
                EulerSimple eulersimpl        = new EulerSimple(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var         result_eulersimpl = eulersimpl.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulersimpl[0, j])
                             + " : "
                             + string.Format("{0:f" + precision + "}", result_eulersimpl[1, j]) + "\n";
                }
            }
            break;

            case "Euler Modified":
            {
                EulerModified eulerModif        = new EulerModified(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var           result_eulerModif = eulerModif.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulerModif[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_eulerModif[1, j]) + "\n";
                    // result = result + "fun=\n";
                    // result = result +Convert.ToString( TestFunDifferEquations);
                }
            }
            break;

            case "Euler Corrected":
            {
                EulerCorrected eulerCorrect        = new EulerCorrected(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var            result_eulerCorrect = eulerCorrect.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulerCorrect[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_eulerCorrect[1, j]) + "\n";
                }
            }
            break;

            case "Runge-Kutta4":
            {
                RungeKutta4 rungeKutta4        = new RungeKutta4(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var         result_rungeKutta4 = rungeKutta4.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_rungeKutta4[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_rungeKutta4[1, j]) + "\n";
                }
            }
            break;

            //*** Integration ***
            case "Chebishev":
                Chebishev chebish        = new Chebishev(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var       result_chebish = chebish.GetSolution();

                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_chebish[1, j]) + "   \t   integral = "
                             + string.Format("{0:f" + precision + "}", result_chebish[0, j]);
                }
                break;

            case "Simpson":
                Simpson simps        = new Simpson(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var     result_simps = simps.GetSolution();
                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_simps[1, j]) +
                             " \t   integral = " + string.Format("{0:f" + precision + "}", result_simps[0, j]);
                }
                break;

            case "Simpson2":
                Simpson2 simps2        = new Simpson2(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var      result_simps2 = simps2.GetSolution();
                result = "\n\n  integral = " + string.Format("{0:f" + precision + "}", result_simps2);
                break;

            case "Trapezium":
                Trapezium trapez        = new Trapezium(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var       result_trapez = trapez.GetSolution();
                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_trapez[1, j])
                             + " \t   integral = " + string.Format("{0:f" + precision + "}", result_trapez[0, j]);
                }
                break;

            //*** Non Linear equalization ***
            case "Half Division":
                HalfDivision halfdiv = new HalfDivision(new FunctionOne(TestFunNonLinearEquations), param1, param2, param3);
                result = "\n    X = " + string.Format("{0:f" + precision + "}", halfdiv.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", halfdiv.GetSolution()[1, 0]);
                break;

            case "Hord Metod":
                HordMetod hormet = new HordMetod(new FunctionOne(TestFunNonLinearEquations), param1, param2, Convert.ToInt32(param3));
                result = "\n    X = " + string.Format("{0:f" + precision + "}", hormet.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", hormet.GetSolution()[1, 0]);
                break;

            case "Newton Metod":
                NewtonMethod newt = new NewtonMethod(new FunctionOne(TestFunNonLinearEquations), param1, param2);
                result = "\n     X = " + string.Format("{0:f" + precision + "}", newt.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", newt.GetSolution()[1, 0]);
                break;

            case "Secant Metod":
                Secant sec = new Secant(new FunctionOne(TestFunNonLinearEquations), param1, param2);
                result = "\n     X = " + string.Format("{0:f" + precision + "}", sec.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", sec.GetSolution()[1, 0]);
                break;

            default:
                result = "";
                break;

            //*** Linear Systems ***
            case "Gaus":
                double[,] LinSysMatrix;
                LinSysMatrix = new double[100, 100];
                for (int l = 0; l < rangeArray; l++)
                {
                    for (int j = 0; j < rangeArray; j++)
                    {
                        LinSysMatrix[l, j] = LinSysMatrixB[l, j];
                    }
                    LinSysMatrix[l, rangeArray] = LinSysMasA[l];
                }
                Gaus gaus        = new Gaus(4, LinSysMatrix);
                var  result_gaus = gaus.GetSolution();
                result = "";
                for (int j = 0; j < result_gaus.Length; j++)
                {
                    result = result + "\n         X" + j + "  =  "
                             + string.Format("{0:f" + precision + "}", result_gaus[j]) + ";";
                }
                break;

            case "Zeidel":
                Zeidel zeidel        = new Zeidel(4, LinSysMatrixB, LinSysMasA);
                var    result_zeidel = zeidel.GetSolution();
                result = "";
                for (int j = 0; j < result_zeidel.Length; j++)
                {
                    result = result + "\n         X" + j + "  =  "
                             + string.Format("{0:f" + precision + "}", result_zeidel[j]) + ";";
                }
                break;

            //*** Interpolation ***
            case "Lagrange Interpolator":

                LagrangeInterpolator lagran = new LagrangeInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", lagran.GetSolution());
                break;

            case "Newton Interpolator":

                NewtonInterpolator newinterpol = new NewtonInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", newinterpol.GetSolution());
                break;

            case "Neville Interpolator":

                NevilleInterpolator newill = new NevilleInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", newill.GetSolution());
                break;

            case "Spline Interpolator":
                SplineInterpolator spline = new SplineInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", spline.GetSolution());
                break;

            case "Barycentric Interpolator":

                BarycentricInterpolation barycen = new BarycentricInterpolation(massX, massF, massW, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", barycen.GetSolution());
                break;

            //*** Matrix Algebra ***
            case "Matrix Determinant":
                MatrixDeterminant matrdet = new MatrixDeterminant();
                result = " \n\n\n  Determinant =  " + string.Format("{0:f" + precision + "}", matrdet.MatrixDet(MatrixAlgebraA, rangeArray)) + ";\n";
                break;

            case "RMatrix LU":

                MatrixLU matrlu         = new MatrixLU(MatrixAlgebraA, rangeArray, rangeArray);
                var      result_matrlu  = matrlu.GetSolution();
                var      result_matrlu2 = matrlu.GetSolution2();
                result = "A:\n";
                for (int ii = 0; ii < rangeArray; ii++)
                {
                    for (int j = 0; j < rangeArray; j++)
                    {
                        result = result + "  \t " + string.Format("{0:f" + precision + "}", result_matrlu[ii, j]);
                    }
                    result = result + " \n";
                }
                result = result + "\nL:\n";
                for (int ii = 0; ii < rangeArray; ii++)
                {
                    result = result + "      " + string.Format("{0:f" + precision + "}", result_matrlu2[ii]);
                }
                result = result + " \n ";
                break;

            case "Matrix Inverse LU":
                RMatrixLuInverse matrluinv = new RMatrixLuInverse();

                MatrixLU matrlu2 = new MatrixLU(MatrixAlgebraA, rangeArray, rangeArray);
                if (matrluinv.rmatrixluinverse(MatrixAlgebraA, rangeArray, matrlu2.GetSolution2()) == true)
                {
                    result = "\n             An inverse matrix exists \n\n ";
                    var result_matrluinv = matrluinv.GetSolution();
                    for (int ii = 0; ii < rangeArray; ii++)
                    {
                        for (int j = 0; j < rangeArray; j++)
                        {
                            result = result + "    \t" + string.Format("{0:f" + precision + "}", result_matrluinv[ii, j]);
                        }
                        result = result + "\n\n";
                    }
                }
                else
                {
                    result = "\n             An inverse matrix does not exist";
                }

                break;

            //*** Optimizing***
            case "Brentopt":
                Brentopt brent = new Brentopt(new FunctionOne(TestFunOptimizing), param1, param2, param3);
                result = "\n    Point of the found minimum :";
                result = result + "\n\n    XMin = " + string.Format("{0:f" + precision + "}", brent.GetSolution());
                result = result + "\n\n     A value of function is in the found minimum :";
                result = result + "\n\n    F(XMin) = " + string.Format("{0:f" + precision + "}", brent.GetSolutionFunction());
                break;

            case "Golden Section":
                GoldenSection godsection = new GoldenSection(new FunctionOne(TestFunOptimizing), param1, param2, Convert.ToInt32(param3));
                result = "\n    Scopes   of segment  which a decision of task is on .";
                result = result + "\n\n    a = " + string.Format("{0:f" + precision + "}", godsection.GetSolutionA());
                result = result + "\n\n    b = " + string.Format("{0:f" + precision + "}", godsection.GetSolutionB());
                break;

            case "Pijavsky":
                Pijavsky pijavsky = new Pijavsky(new FunctionOne(TestFunOptimizing), param1, param2, param3, Convert.ToInt32(param4));
                result = "\n    Abscissa of the best point from found..";
                result = result + "\n\n    F = " + string.Format("{0:f" + precision + "}", pijavsky.GetSolution());
                break;

            //*** Statistics ***
            case "Correlation Pearson":


                CorrelationPearson corelperson = new CorrelationPearson(massX, massF, 6);
                result = "\n    Pearson product-moment correlation coefficient.";
                result = result + "\n\n    K = " + string.Format("{0:f" + precision + "}", corelperson.GetSolution());
                break;

            case "Correlation Spearmans Rank":
                CorrelationSpearmansRank corelspear = new CorrelationSpearmansRank(massX, massF, 6);
                result = "\n    Pearson product-moment correlation coefficient.";
                result = result + "\n\n    K = " + string.Format("{0:f" + precision + "}", corelspear.GetSolution());
                break;

            case "Descriptive Statistics Median":
                DescriptiveStatisticsADevMedian desceripM = new DescriptiveStatisticsADevMedian(massX, 6);
                result = "\n    Output parameters:";
                result = result + "\n\n    M = " + string.Format("{0:f" + precision + "}", desceripM.GetSolution());
                break;

            case "Descriptive Statistics Moments":
                DescriptiveStatisticsMoments desceripMo = new DescriptiveStatisticsMoments(massX, 6);
                result = "\n    Output parameters:";
                result = result + "\n\n    M = " + string.Format("{0:f" + precision + "}", desceripMo.GetSolution());
                result = result + "\n\n    Variance = " + string.Format("{0:f" + precision + "}", desceripMo.variance);
                result = result + "\n\n    Skewness = " + string.Format("{0:f" + precision + "}", desceripMo.skewness) + " (if variance<>0; zero otherwise)";
                result = result + "\n\n    Kurtosis = " + string.Format("{0:f" + precision + "}", desceripMo.kurtosis) + " (if variance<>0; zero otherwise)";
                break;

            case "Descriptive Statistics Percentile":
                DescriptiveStatisticsPercentile desceripP = new DescriptiveStatisticsPercentile(massX, 6, pointPercentile);
                result = "\n    Output parameters:";
                result = result + "\n\n    V = " + string.Format("{0:f" + precision + "}", desceripP.GetSolution());
                break;

            case "Generator 1":
                RandomGeneratorsMethod1 random1 = new RandomGeneratorsMethod1();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random1.GetSolution());
                break;

            case "Generator 2":
                RandomGeneratorsMethod2 random2 = new RandomGeneratorsMethod2(Convert.ToInt32(pointGenerator));
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random2.GetSolution());
                break;

            case "Generator 3":
                RandomGeneratorsMethod3 random3 = new RandomGeneratorsMethod3();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random3.GetSolution());
                break;

            case "Generator 4":
                RandomGeneratorsMethod4 random4 = new RandomGeneratorsMethod4();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random4.GetSolution());
                break;

            case "Generator 5":
                RandomGeneratorsMethod5 random5 = new RandomGeneratorsMethod5(pointGenerator);
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random5.GetSolution());
                break;
            }
        }