コード例 #1
0
        static double [] Newton(Binary_Function equations, Binary_Function [,] jacobiMatrix, double [] initialValues, double eps)
        {
            int n = equations.GetInvocationList().Length;

            double[,] intermediateMatrix = new double[n, n];
            double[] z = new double[n];
            do
            {
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        intermediateMatrix[i, j] = jacobiMatrix[i, j](initialValues[0], initialValues[1]);
                    }
                }
                Delegate[] equationsList = equations.GetInvocationList();
                double []  b             = new double[initialValues.GetLength(0)];
                int        q             = 0;
                foreach (Delegate f in equationsList)
                {
                    if (f is Binary_Function)
                    {
                        b[q++] = (f as Binary_Function)(initialValues[0], initialValues[1]);
                    }
                }
                z = Gauss(intermediateMatrix, b);
                for (int i = 0; i < initialValues.GetLength(0); i++)
                {
                    initialValues[i] = initialValues[i] - z[i];
                }
            } while (z[0] > eps);
            return(initialValues);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            // For Trapezium
            Console.WriteLine("Trapezium method for integral(3x^4 + x), x from 0 to 1.5");
            Unary_Function f1 = (x) => 3 * x * x * x * x + x;

            Console.WriteLine(String.Format("{0:f5}", Trapezium(f1, 0, 1.5, 81, 0.001)));
            // For Power Iteration
            double[,] powerIterationMatrix = { { 1.4, 0.5, 0.6 },
                                               { 0.5, 1.4, 0.3 },
                                               { 0.6, 0.3, 1.4 } };
            Console.WriteLine("\nPower Iteration method\nThe max eigenvalue is:");
            Console.WriteLine(String.Format("{0:f5}",
                                            PowerIteration(powerIterationMatrix, new double[] { 1, 1, 1 }, 0.001)));
            // For Newton
            Binary_Function equationsSystem = (x, y) => Math.Tan(x * y + 0.1) - x * x;

            equationsSystem += (x, y) => x * x + 2 * y * y - 1;

            Binary_Function eq1dx = (x, y) => y * (1 / Math.Cos(x * y + 0.1) * Math.Cos(x * y + 0.1)) - 2 * x;
            Binary_Function eq1dy = (x, y) => x * (1 / Math.Cos(x * y + 0.1) * Math.Cos(x * y + 0.1));
            Binary_Function eq2dx = (x, y) => 2 * x;
            Binary_Function eq2dy = (x, y) => 4 * y;

            Binary_Function[,] jacobiMatrix = { { eq1dx, eq1dy },
                                                { eq2dx, eq2dy } };

            double[] newtonResult = Newton(equationsSystem, jacobiMatrix, new double[] { 1.25, 0 }, 0.001);

            Console.WriteLine("\nSolution by Newton ");
            for (int i = 0; i < newtonResult.GetLength(0); i++)
            {
                Console.WriteLine($"x[{i}] = {String.Format("{0:f6}", newtonResult[i])}");
            }
        }