Evaluating the following integral using comp trap method for 2 trapezoids

5 views (last 30 days)
% I'm trying to evaulate this integral using the composite trapezoid method
% The output for the 'I' gives me 8.7797 which matches the solution, yet
% when I calculate the error percentage, I get 0.45278 when the solution
% is 0.45484.
% I've used the same code to calculate for n # of trapezoids and dont seem
% to have an issue.
y = @(x) (10 - 2*cos(x));
b= pi/3;
a = 0;
n = 2; % Compute with comp trap method for 2 trapezoids
h = (b-a)/n;
x= a:h:b;
I = h*(y(a)*0.5+y(b)*0.5+y(x(2)));
err = 100*abs((I-integral(y,a,b))/I);

Answers (1)

Alan Stevens
Alan Stevens on 7 Aug 2022
You need to divide by the true value of the integral in calculating the error:
y = @(x) (10 - 2*cos(x));
b= pi/3;
a = 0;
n = 2; % Compute with comp trap method for 2 trapezoids
h = (b-a)/n;
x= a:h:b;
I = h*(y(a)*0.5+y(b)*0.5+y(x(2)))
I = 8.7797
Itrue = 10*(b-a) - 2*(sin(b) -sin(a))
Itrue = 8.7399
err = 100*abs((I-Itrue)/Itrue)
err = 0.4548
  2 Comments
Alan Stevens
Alan Stevens on 7 Aug 2022
I just integrated ypour y-function by hand - it's easy to do. However, you can replace it by your integral(y,a,b) if you wish (i.e. set Itrue = integral(y,a,b)).

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!