-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmynumintegrate.m
99 lines (90 loc) · 3.21 KB
/
mynumintegrate.m
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
% limits - we set a and b as the desired limits inside each condition
f = @y;
prompt = "Enter a for the answer to part 2a, b for the answer to part 2b, and c for the answer to part 2c: ";
ip = input(prompt, 's');
if strcmp(ip,'a')
% finding the integrals and plotting the antiderivative function
a = -2*pi;
b = 2*pi;
i = 1;
for n = [5, 50, 100, 500] % n determines the grid space dx (dx = (b-a)/n)
x = linspace(a,b,n);
[ti, si, ai] = my_num_integrate(f, x, a, b);
disp(newline + "The value of the integral using the Trapezoidal rule when n is " + n+ " is " + ti);
disp(newline + "The value of the integral using the Simpson's rule when n is " + n + " is " + si);
disp(newline + "The actual value of the integral when n is " + n + " is " + ai);
figure(i);
plot(x, -cos(x), 'Linewidth', 1.25); hold on;
grid on;
title("Antiderivative function of sin(x) when n is " + n);
legend("- cos(x)");
xlabel("x"); ylabel("Integral");
xlim([-2*pi, 2*pi]); hold off;
i = i+1;
end
elseif strcmp(ip,'b')
a = 0;
b = 2*pi;
% plotting the error of each integral separately, for different dx values
all_error_ti = [];
all_error_si = [];
for n = 2:1:500
x = linspace(a,b,n);
[error_ti, error_si] = find_err(f,x,a,b);
%#ok<*AGROW>
all_error_ti = [all_error_ti, error_ti];
all_error_si = [all_error_si, error_si];
end
n = 2:1:500;
dx = (b-a)./n;
plot(dx, all_error_ti, 'Linewidth', 1.25); hold on;
plot(dx, all_error_si, 'Linewidth', 1.25); hold on;
title("Error as a function of dx");
legend("Trapezoidal rule", "Simpson's rule");
xlabel("dx"); ylabel("Error");
grid on;
elseif strcmp(ip, 'c')
a = -2*pi;
b = 2*pi;
for n = [5, 50, 100, 500] % n determines the grid space dx (dx = (b-a)/n)
x = linspace(a,b,n);
mat_ti = trapz(x,sin(x));
[ti, si, ai] = my_num_integrate(f, x, a, b);
disp(newline + "The value of the integral using the Trapezoidal rule when n is " + n+ " is " + ti);
disp(newline + "The value of the integral using the Simpson's rule when n is " + n + " is " + si);
disp(newline + "The actual value of the integral when n is " + n + " is " + ai);
disp(newline + "The value of the integral using the inbuilt trapz function when n is " + n+ " is " + mat_ti);
end
else
disp("Undefined input");
return;
end
function [func] = y(val)
func = sin(val);
end
function [ti, si, ai] = my_num_integrate(f, x, a, b)
dx = x(2) - x(1);
%computing the integral using the trapezoidal rule
x0 = a;
sum = f(x0);
while x0<b
x0 = x0 + dx;
sum = sum + 2*f(x0);
end
ti = sum*(dx/2);
%computing the integral using the Simpson's rule
x0 = a;
sum = f(x0);
while x0<(b - dx)
x0 = x0 + dx;
sum = sum + 4*f(x0) + 2*f(x0 + dx);
end
si = (sum + f(b))*(dx/3);
% computing the analytical integral of sin(x), which is -cos(x)
ai = -cos(a) + cos(b);
end
function [error_ti, error_si] = find_err(f,x,a,b)
[ti, si, ai] = my_num_integrate(f,x,a,b);
error_ti = abs(ai - ti);
error_si = abs(ai - si);
end