# Evaluating the following integral using comp trap method for 2 trapezoids

4 ビュー (過去 30 日間)
Errol 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 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 件のコメント表示非表示 1 件の古いコメント
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)).

サインインしてコメントする。

### カテゴリ

Find more on Numbers and Precision 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!

Translated by