- #1
ver_mathstats
- 260
- 21
- Homework Statement
- We need to evaluate an integral sin(1/x) where b = 1 and a = 0.1 using composite Simpson in Matlab for n = 2, 4, 8... until the absolute error is within 10^-4, then we are required to apply the adaptive Simpson rule and compare the two. The program needs to output the number and error.
- Relevant Equations
- composite simpson rule
Matlab:
function I=main_simpson(a,b,tol)
f = @(x) sin(1./x);
SO = 0;
N = 10;
S = 1;
while (abs(S-SO)>tol)
SO = S;
h = (b-a)/(2*N);
i = 0:N-1;
xi = a+2*i*h;
xi1 = a+2*(i+0.5)*h;
xi2 = a+2*(i+1)*h;
S = (h/3)*sum(f(xi)+4*f(xi1)+f(xi2));
N = 2*N;
end
end
I keep experiencing errors when trying to use my composite Simpson code, could anyone tell me how I can improve this? The error is always coming from the while loop but I'm really unsure why and I do not know how to proceed or nothing happens when I input data into my function so I am really confused. Any help is appreciated.
Last edited by a moderator: