error of repeat simpsons rule in matlab

If you are using the simpsons function in MATLAB for numerical integration and you get the "repeated Simpson's rule" error, that means that the function was unable to achieve the desired level of accuracy and needs to use a finer grid (i.e., more intervals) to compute the integral.

To fix this error, you can either increase the number of intervals manually (by increasing the n input argument to simpsons), or you can use a different integration method altogether (such as the integral function in MATLAB).

Here's an example of how to adjust the number of intervals in the simpsons function:

main.m
f = @(x) exp(-x.^2); % function to integrate
a = 0; % lower limit
b = 1; % upper limit
n = 10; % initial number of intervals
tol = 1e-6; % desired tolerance

% compute the integral using simpson's rule
I_old = simpsons(f,a,b,n);
err = Inf;
while err > tol
    n = n+10; % increase the number of intervals
    I_new = simpsons(f,a,b,n);
    err = abs(I_new - I_old)/15; % estimate the error
    I_old = I_new;
end

disp(I_new); % display the final result
454 chars
18 lines

This code will keep increasing the number of intervals by 10 each time until the estimated error is below the desired tolerance.

related categories

gistlibby LogSnag