Advertisement
Pafnytiu

Методы вычислений 2

Oct 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace ConsoleApplication1
  5. {
  6.     class Program
  7.     {
  8.  
  9.         static double MyFunc(double x)
  10.         {
  11.             return Math.Log(2 * x);
  12.         }
  13.        
  14.         static double IntRight(double a, double b, double h, int n) //Формула правых прямоугольников
  15.         {
  16.             double summ = 0;
  17.             double x;
  18.             for (int i = 1; i <= n; i++)
  19.             {
  20.                 x = a + i * h;
  21.                 summ += h * MyFunc(x);
  22.             }
  23.             return summ;
  24.         }
  25.  
  26.         static double IntLeft(double a, double b, double h, int n) //Формула левых прямоугольников
  27.         {
  28.             double summ = 0;
  29.             double x;
  30.             for (int i = 0; i <= n - 1; i++)
  31.             {
  32.                 x = a + i * h;
  33.                 summ += h * MyFunc(x);
  34.             }
  35.             return summ;
  36.         }
  37.  
  38.         static double IntCenter(double a, double b, double h, int n) //Формула центральных прямоугольников
  39.         {
  40.             double summ = 0;
  41.             double x;
  42.             for (int i = 0; i <= n - 1; i++)
  43.             {
  44.                 x = a + i * h;
  45.                 summ += h * MyFunc(x + h / 2);
  46.             }
  47.             return summ;
  48.         }
  49.  
  50.         static double IntTrapeze(double a, double b, double h, int n) //Формула трапеций
  51.         {
  52.             double summ = h / 2 * (MyFunc(a) + MyFunc(b));
  53.             double x;
  54.             for (int i = 1; i < n - 1; i++)
  55.             {
  56.                 x = a + i * h;
  57.                 summ += h * MyFunc(x + h / 2);
  58.             }
  59.             return summ;
  60.         }
  61.  
  62.         static double IntSimpson(Func<double, double> F, double a, double b, double h, double n)
  63.         {
  64.  
  65.             if (n % 2 == 1)
  66.             {
  67.                 n++;
  68.             }
  69.  
  70.             double result = F(a) + F(b);
  71.  
  72.             for (int k = 1; k < n; k += 2)
  73.             {
  74.                 if (k != n - 1)
  75.                     result += 4 * F(a + h * k) + 2 * F(a + h * (k + 1));
  76.                 else
  77.                     result += 4 * F(a + h * k);
  78.             }
  79.             result *= h / 3;
  80.             return result;
  81.         }
  82.  
  83.         static void Main(string[] args)
  84.         {
  85.             double MyInteger = 4.6246;
  86.             Func<double, double> Func = MyFunc;
  87.             double a = 1;
  88.             double b = 4;  
  89.  
  90.             int n = 1000;
  91.             double h = (b - a) / n;
  92.             double one = IntRight(a,b,h,n);
  93.             double two = IntLeft(a, b, h,n);
  94.             double three = IntCenter(a,b,h, n);
  95.             double four = IntTrapeze(a, b, h, n);
  96.             double five = IntSimpson(Func, a, b, h, n);
  97.             Console.WriteLine("Мой интеграл, вычисленный аналитически: {0}", MyInteger);
  98.             Console.WriteLine("Right: {0}, \nLeft: {1}, \nCentre: {2}, \nTrap: {3}, \nSimpson: {4}", one, two, three, four, five);
  99.             Console.WriteLine();
  100.             Console.WriteLine("Погрешности:");
  101.             Console.WriteLine("Right: {0:F4}", Math.Abs(one - MyInteger));
  102.             Console.WriteLine("Left: {0:F4}", Math.Abs(two - MyInteger));
  103.             Console.WriteLine("Centre: {0:F4}", Math.Abs(three - MyInteger));
  104.             Console.WriteLine("Trap: {0:F4}", Math.Abs(four - MyInteger));
  105.             Console.WriteLine("Simpson: {0:F4}", Math.Abs(five - MyInteger));
  106.             Console.Read();
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement