-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrapezoidal_rule.m
108 lines (71 loc) · 2.28 KB
/
trapezoidal_rule.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
100
101
102
103
104
105
106
%% Trapezoidal Rule
% Here we use the principle behind finding area of trapezoids.
clc;
clear all;
% Declare x to be a symbolic variable
syms x;
%% Input Values
% Function
f = exp(-(x^2));
% Lower Limit
a = 0;
% Upper Limit
b = 1;
% Mention the number of parts to divide the range
num_parts = 4;
%% Initialization
% Calculate the h value
h = (b - a) / num_parts;
% Find the total number of points in the table
n = num_parts + 1;
% To store the x values and its function values
table = zeros(2, n);
% To store the terms in the equation
terms = zeros(1, 2);
%% Computation
% Compute and fill the table
for i=1:n
% Compute the x value
table(1, i) = a + ((i - 1) * h);
% Find its corresponding function value
[numerator, denominator] = numden(f);
if (subs(denominator, x, table(1, i)) == 0) && (subs(numerator, x, table(1, i)) == 0)
% L'Hopitals Rule
while (subs(denominator, x, table(1, i)) == 0) && (subs(numerator, x, table(1, i)) == 0)
numerator = diff(numerator,x);
denominator = diff(denominator,x);
end
ftemp = numerator/denominator;
table(2, i) = subs(ftemp, x, table(1, i));
else
table(2, i) = subs(f, x, table(1, i));
end
end
% Compute the term values of the equation
terms(1) = table(2, 1) + table(2, n);
for i=2:n-1
terms(2) = terms(2) + table(2, i);
end
% Substitute these term values in the equation to get the integral value
I = (h / 2) * (terms(1) + (2 * terms(2)));
%% Results Display
disp("---------------------------------------------------------------")
fprintf("\nh = %f\n\n", h)
disp("The function table : ")
fprintf("x : ")
disp(table(1, :))
fprintf("f(x) : ")
disp(table(2, :))
disp("---------------------------------------------------------------")
disp("Equation : ")
fprintf("I = (%f / 2) * [%f + (2 * %f)]\n", h, terms(1), terms(2))
disp("---------------------------------------------------------------")
fprintf("Intergral Value I = %f\n", I)
%% Verification
% We verify this answer by performing a normal definite integral
% calculation.
I_verify = int(f, a, b);
fprintf("\n------------------ Verification ---------------------------\n")
fprintf("Integration of the function f : ")
disp(int(f))
fprintf("The Actual value of the Integral is : %f\n", I_verify)