Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace ConsoleApplication1
- {
- class Program
- {
- static double MyFunc(double x)
- {
- return Math.Log(2 * x);
- }
- static double IntRight(double a, double b, double h, int n) //Формула правых прямоугольников
- {
- double summ = 0;
- double x;
- for (int i = 1; i <= n; i++)
- {
- x = a + i * h;
- summ += h * MyFunc(x);
- }
- return summ;
- }
- static double IntLeft(double a, double b, double h, int n) //Формула левых прямоугольников
- {
- double summ = 0;
- double x;
- for (int i = 0; i <= n - 1; i++)
- {
- x = a + i * h;
- summ += h * MyFunc(x);
- }
- return summ;
- }
- static double IntCenter(double a, double b, double h, int n) //Формула центральных прямоугольников
- {
- double summ = 0;
- double x;
- for (int i = 0; i <= n - 1; i++)
- {
- x = a + i * h;
- summ += h * MyFunc(x + h / 2);
- }
- return summ;
- }
- static double IntTrapeze(double a, double b, double h, int n) //Формула трапеций
- {
- double summ = h / 2 * (MyFunc(a) + MyFunc(b));
- double x;
- for (int i = 1; i < n - 1; i++)
- {
- x = a + i * h;
- summ += h * MyFunc(x + h / 2);
- }
- return summ;
- }
- static double IntSimpson(Func<double, double> F, double a, double b, double h, double n)
- {
- if (n % 2 == 1)
- {
- n++;
- }
- double result = F(a) + F(b);
- for (int k = 1; k < n; k += 2)
- {
- if (k != n - 1)
- result += 4 * F(a + h * k) + 2 * F(a + h * (k + 1));
- else
- result += 4 * F(a + h * k);
- }
- result *= h / 3;
- return result;
- }
- static void Main(string[] args)
- {
- double MyInteger = 4.6246;
- Func<double, double> Func = MyFunc;
- double a = 1;
- double b = 4;
- int n = 1000;
- double h = (b - a) / n;
- double one = IntRight(a,b,h,n);
- double two = IntLeft(a, b, h,n);
- double three = IntCenter(a,b,h, n);
- double four = IntTrapeze(a, b, h, n);
- double five = IntSimpson(Func, a, b, h, n);
- Console.WriteLine("Мой интеграл, вычисленный аналитически: {0}", MyInteger);
- Console.WriteLine("Right: {0}, \nLeft: {1}, \nCentre: {2}, \nTrap: {3}, \nSimpson: {4}", one, two, three, four, five);
- Console.WriteLine();
- Console.WriteLine("Погрешности:");
- Console.WriteLine("Right: {0:F4}", Math.Abs(one - MyInteger));
- Console.WriteLine("Left: {0:F4}", Math.Abs(two - MyInteger));
- Console.WriteLine("Centre: {0:F4}", Math.Abs(three - MyInteger));
- Console.WriteLine("Trap: {0:F4}", Math.Abs(four - MyInteger));
- Console.WriteLine("Simpson: {0:F4}", Math.Abs(five - MyInteger));
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement