-
Notifications
You must be signed in to change notification settings - Fork 1
/
10-Simpson.pas
72 lines (65 loc) · 1.27 KB
/
10-Simpson.pas
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
PROGRAM simpson;
USES crt;
{---Equation of integral by Simpson method---}
FUNCTION func(x: REAL): REAL;
VAR
t: REAL;
BEGIN
t := sin(x);
func := t;
END;
{Solution}
VAR
x, I1, I2, eps, h, a, b: REAL;
s0, s1, s2: REAL;
k: INTEGER;
i, n: LONGINT;
BEGIN
ClrScr;
Write('Vvedite a: '); ReadLn(a);
Write('Vvedite b: '); ReadLn(b);
Write('Vvedite n (n - even): '); ReadLn(n);
Write('Vvedite eps-pogreshnost: '); ReadLn(eps);
h := (b - a)/n;
s0 := func(a) + func(b);
s1 := 0; s2 := 0;
FOR k := 1 TO (n - 1) DO
BEGIN
If Odd(k) THEN
BEGIN
x := a + k * h;
s1 := s1 + func(x);
END
ELSE
BEGIN
x := a + k * h;
s2 := s2 + func(x);
END;
END;
I1 := (h * (s0 + 4 * s1 + 2 * s2)) / 3;
I2 := I1 + 100 * eps;
i := 0;
WHILE Abs(I1 - I2) > eps DO
BEGIN
I1 := I2;
n := 2 * n;
h := (b - a)/n;
s2 := s1 + s2;
s1 := 0;
FOR k := 1 TO (n - 1) DO
BEGIN
IF Odd(k) THEN
BEGIN
x := a + k * h;
s1 := s1 + func(x);
END;
END;
i := i + 1;
I2 := (h * (s0 + 4 * s1 + 2 * s2)) / 3;
END;
WriteLn;
WriteLn(i);
WriteLn;
WriteLn('Integral = ', I2:5:5);
ReadKey;
END.