Info

This question is closed. Reopen it to edit or answer.

I need to use a while loop and int to calculate int(f(x),c,b) for c = a + (b−a)/2 , c = a + (b−a)/ 3 , c = a + (b−a)/ 4 , ... until successive integrals differ by less than tol. i cant find out what wrong with my code it wont return the right thing

1 view (last 30 days)
function a = myimproperintegral(f,a,b,tol)
syms x;
n = 0;
total = 0;
difference = int(f(x),a+((b-a)/2),b);
before = int(f(x),a+((b-a)/2),b);
while (tol < difference)
for k=[1:n]
I(x) = int(f(x));
c =a+((b-a)/k);
A = (I(b))-(I(c));
end
total = total+A;
difference = abs(before - total);
n=n+1;
end
end
%tESTiNG
%a = myimproperintegral(@(x) 1/sqrt(2-x),1,2,0.01)
%a =
%1.9069251784911846308935518430544

Answers (0)

Community Treasure Hunt

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

Start Hunting!