Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ЧМ1
- {
- class MainClass
- {
- public static double f(double e)
- {
- double fx = Math.Exp(e) * Math.Pow(Math.Cos(e), 2);
- return fx;
- }
- public static double F(double e)
- {
- double Fx = 0.5 * Math.Exp(e) * (1 + 0.2 * (2 * Math.Sin(2 * e) + Math.Cos(2 * e))) - 0.6;
- return Fx;
- }
- public static double Trap(double low, double high, int div)
- {
- double x = low;
- double s = 0;
- double h = (high - low) / div;
- double J = 0;
- for (int i = 1; i < div; i++)
- {
- x += h;
- s += f(x);
- }
- J = (f(high) + f(low) + 2 * s) * h / 2;
- return J;
- }
- public static double Simps(double low, double high, int div)
- {
- double x = low;
- double s = 0;
- double h = (high - low) / div;
- double J = 0;
- int c = 1;
- for (int i = 1; i < div; i++)
- {
- x += h;
- s += (3 + c) * f(x);
- c = -c;
- }
- J = (f(high) + f(low) + s) * h / 3;
- return J;
- }
- public static void Main(string[] args)
- {
- double a = 0;
- double b = Math.PI;
- Console.WriteLine("Пределы интегрирования: [{0},π]", a);
- double t = 0.001;
- double Nuton = F(b) - F(a);
- Console.WriteLine("Функция: f(x) = exp(x)*(cos(x))^2");
- Console.WriteLine("Первообразная: F(x) = 0.5exp(x)(1+0.2(2sin(2x)+cos(2x)))-0.6");
- Console.WriteLine("Ответ, согласно формуле Н-Л: {0}", Nuton);
- double dif = 1;
- int n = 1;
- double I = 0;
- while (dif > t)
- {
- n++;
- I = Trap(a, b, n);
- dif = Math.Abs(I - Nuton);
- }
- Console.WriteLine("Результат метода трапеций: {0}", I);
- Console.WriteLine("Число разбиений: {0}", n);
- Console.WriteLine("Абсолютная погрешность: {0}", dif);
- n = 1;
- dif = 1;
- while (dif > t)
- {
- n++;
- I = Simps(a, b, 2 * n);
- dif = Math.Abs(I - Nuton);
- }
- Console.WriteLine("Результат метода Симпсона: {0}", I);
- Console.WriteLine("Число разбиений: {0}", 2 * n);
- Console.WriteLine("Абсолютная погрешность: {0}", dif);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement