static double Trapezium(Unary_Function f, double start, double end, double m2, double eps) { double delta = Math.Sqrt((12 * eps) / ((end - start) * m2)); double result = f(start) / 2 + f(end) / 2; start += delta; while (end > start) { result += f(start); start += delta; } result *= delta; return(result); }
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])}"); } }