Advertisement
mlmisha

NM1

May 13th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ЧМ1
  4. {
  5.     class MainClass
  6.     {
  7.         public static double f(double e)
  8.         {
  9.             double fx = Math.Exp(e) * Math.Pow(Math.Cos(e), 2);
  10.             return fx;
  11.         }
  12.         public static double F(double e)
  13.         {
  14.             double Fx = 0.5 * Math.Exp(e) * (1 + 0.2 * (2 * Math.Sin(2 * e) + Math.Cos(2 * e))) - 0.6;
  15.             return Fx;
  16.         }
  17.         public static double Trap(double low, double high, int div)
  18.         {
  19.             double x = low;
  20.             double s = 0;
  21.             double h = (high - low) / div;
  22.             double J = 0;
  23.             for (int i = 1; i < div; i++)
  24.             {
  25.                 x += h;
  26.                 s += f(x);
  27.             }
  28.             J = (f(high) + f(low) + 2 * s) * h / 2;
  29.             return J;
  30.         }
  31.         public static double Simps(double low, double high, int div)
  32.         {
  33.             double x = low;
  34.             double s = 0;
  35.             double h = (high - low) / div;
  36.             double J = 0;
  37.             int c = 1;
  38.             for (int i = 1; i < div; i++)
  39.             {
  40.                 x += h;
  41.                 s += (3 + c) * f(x);
  42.                 c = -c;
  43.             }
  44.             J = (f(high) + f(low) + s) * h / 3;
  45.             return J;
  46.         }
  47.         public static void Main(string[] args)
  48.         {
  49.             double a = 0;
  50.             double b = Math.PI;
  51.             Console.WriteLine("Пределы интегрирования: [{0},π]", a);
  52.             double t = 0.001;
  53.             double Nuton = F(b) - F(a);
  54.             Console.WriteLine("Функция: f(x) = exp(x)*(cos(x))^2");
  55.             Console.WriteLine("Первообразная: F(x) = 0.5exp(x)(1+0.2(2sin(2x)+cos(2x)))-0.6");
  56.             Console.WriteLine("Ответ, согласно формуле Н-Л: {0}", Nuton);
  57.             double dif = 1;
  58.             int n = 1;
  59.             double I = 0;
  60.             while (dif > t)
  61.             {
  62.                 n++;
  63.                 I = Trap(a, b, n);
  64.                 dif = Math.Abs(I - Nuton);
  65.             }
  66.             Console.WriteLine("Результат метода трапеций: {0}", I);
  67.             Console.WriteLine("Число разбиений: {0}", n);
  68.             Console.WriteLine("Абсолютная погрешность: {0}", dif);
  69.             n = 1;
  70.             dif = 1;
  71.             while (dif > t)
  72.             {
  73.                 n++;
  74.                 I = Simps(a, b, 2 * n);
  75.                 dif = Math.Abs(I - Nuton);
  76.             }
  77.             Console.WriteLine("Результат метода Симпсона: {0}", I);
  78.             Console.WriteLine("Число разбиений: {0}", 2 * n);
  79.             Console.WriteLine("Абсолютная погрешность: {0}", dif);
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement