Boole's Approximation

조회 수: 3 (최근 30일)
Syed Abdul Rafay
Syed Abdul Rafay 2022년 11월 24일
댓글: Image Analyst 2022년 11월 24일
I checked my code several times but I can not find the mistake. There is a huge difference between the exact answer and Boole's approximate value.
function Boole_approx(a,b)
a = -1;
b = 1;
N=10; %total number of intervals
h = (b-a)/(4*N); %shows the steps increment
x = a:h:b;
s=((2*h)/45)*(7*(f(a)+f(b)));
for i=1:2:N-1
s = s + ((2*h)/45)*(32*f(x(i)));
end
for i=2:4:N-2
s = s + ((2*h)/45)*(12*f(x(i)));
end
for i=4:4:N-4
s = s + ((2*h)/45)*(14*f(x(i)));
end
disp (s)
G = @(x)(tan(x)-2.*x);
exact_value = integral(G,0,pi)
end
function fx=f(x)
fx = tan(x)-2*x;
end
Answer
resuts =
0.232772096457974 -9.87116269500961
  댓글 수: 1
Image Analyst
Image Analyst 2022년 11월 24일
What did you pass in for a and b?
And why do you immediately ovrewrite them with -1 and 1, thus making the input arguments totally irrelevant?

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

답변 (1개)

Alan Stevens
Alan Stevens 2022년 11월 24일
You are using Boole from -1 to 1, but the "exact" from 0 to pi. (You should plot G against x to see why your "exact" integral won't produce a sensible numerical answer).
  댓글 수: 1
Syed Abdul Rafay
Syed Abdul Rafay 2022년 11월 24일
sorry I made a mistake her but it is to pi in original code. I forgot to correct it while pasting. I tried 5 to 6 time inf is the answer of boole's and 67 for exact

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by