Evaluating the following integral using comp trap method for 2 trapezoids

조회 수: 5 (최근 30일)
Oscar
Oscar 2022년 8월 6일
댓글: Alan Stevens 2022년 8월 7일
% 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);

답변 (1개)

Alan Stevens
Alan Stevens 2022년 8월 7일
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
Oscar
Oscar 2022년 8월 7일
thank you but can you explain this true value formula?
Alan Stevens
Alan Stevens 2022년 8월 7일
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)).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by