Evaluating the following integral using comp trap method for 2 trapezoids

5 visualizaciones (últimos 30 días)
% 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);

Respuestas (1)

Alan Stevens
Alan Stevens el 7 de Ag. de 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 comentarios
Oscar
Oscar el 7 de Ag. de 2022
thank you but can you explain this true value formula?
Alan Stevens
Alan Stevens el 7 de Ag. de 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)).

Iniciar sesión para comentar.

Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by