using System using System Collections Generic using System Linq using

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace cw_12
{
public enum namemethod { trapeze, simpson };
class Integral
{
public double a;// нижняя граница интегрирования
public double b;// верхняя граница интегрирования
public int n;// количество частей разбиений
public Integral(double a, double b, int n)
{
this.a = a;
this.b = b;
this.n = n;
}
public delegate double D(double x);
public double summ(D F, namemethod met, double S)
{
double h = 0, s = 0, x = 0, x1 = 0;
if (met == namemethod.trapeze)
{
double sum = 0;
h = (b - a) / n;
for (int i = 0; i <= n; i++)
{
x = a + i * h;
x1 = a + (i + 1) * h;
sum += F(x1) + F(x);
}
s = sum * h / 2;
return s;
}
if (met == namemethod.simpson)
{
h = (b - a) / n;
s = 0;
x = a;
while (x < b)
{
s = s + 4 * F(x);
x = x + h;
s = s + 2 * F(x);
x = x + h;
}
s = h / 3 * (s + F(a) - F(b));
}
return s;
}
}
class Program
{
public static double functions(double x)
{
return Math.Pow(Math.Tan(x), 2) + 1 / Math.Pow(Math.Tan(x), 2);
}
public static double primitive(double x)
{
return Math.Tan(x) - 1 / Math.Tan(x) - 2 * x - Math.Tan(Math.PI / 6) + 1 / Math.Tan(Math.PI / 6) + Math.PI / 3;
}
static void Main(string[] args)
{
int n = 54;
double a = Math.PI / 6, b = Math.PI / 3, S = 0;
Integral ret = new Integral(a, b, n);
namemethod method = namemethod.simpson;
namemethod method2 = namemethod.trapeze;
Console.WriteLine("Метод Симпсона\nЗначение интеграла:\t");
Console.WriteLine("{0:f2}\n", ret.summ(Program.functions, method2, S));
Console.WriteLine("Метод трапеций\nЗначение интеграла:\t");
Console.WriteLine("{0:f2}\n", ret.summ(Program.functions, method, S));
Console.WriteLine("Проверка. Вычислим первообразную в пределах интегрирования\nПервообразная:\t");
Console.WriteLine("{0:f2}\n", Program.primitive(b) - Program.primitive(a));
}
}
}