Evaluating the following integral using comp trap method for 2 trapezoids

5 vues (au cours des 30 derniers jours)
Oscar
Oscar le 6 Août 2022
Commenté : Alan Stevens le 7 Août 2022
% 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);

Réponses (1)

Alan Stevens
Alan Stevens le 7 Août 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 commentaires
Oscar
Oscar le 7 Août 2022
thank you but can you explain this true value formula?
Alan Stevens
Alan Stevens le 7 Août 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)).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by