-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrap.m
More file actions
46 lines (46 loc) · 1.02 KB
/
Trap.m
File metadata and controls
46 lines (46 loc) · 1.02 KB
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
function [ ] = Trap( a, b, eps )
tic
n = 2;
h = (b - a) / n;
f = false;
prev = 0;
%plot(0,0,'-r');
while(1)
S = 0;
x0=a;
x=x0+h;
for k = 1 : n
X1(k)=x0;
X2(k)=x;
x0 = a + k * h - h / 2;
x = x0 + h;
S = S + (source_func(x0)+source_func(x))/2;
end
int = S * h;
if(abs(int - prev) < eps)
f = true;
for k=1:n
hold on;
plot([X1(k) X2(k)], [source_func(X1(k)) source_func(X2(k))], 'b');
plot([X1(k) X1(k)], [0 source_func(X1(k))], 'b');
plot([X2(k) X2(k)], [0 source_func(X2(k))], 'b');
end
else
n = n * 2;
h = h / 2;
prev = int;
end
if(f == true)
break;
end
end
t = toc;
title('Method of trapezoid');
fprintf('Integral: %g \n',int);
fprintf('Integration step: %g \n', h);
fprintf('Amount of integration step: %i \n', n);
fprintf('Time: %g \n', t);
X = a:0.1:b;
Y = source_func(X);
plot (X, Y, 'r');
end