% this takes the interval [a,b], and starts by integrating on an interval % with n mesh-points. It then takes finer and finer meshes until the error % is less than the given tolerance (tol). % % It then returns the area, the error, the mesh-size, and the number of % flops it took to perform the integral using the trapezoidal rule: % function [area,h,error,t_run] = trap_filter(a,b,n,tol) function [area,h,error,t_run] = trap_filter(a,b,n,tol) % set the initial error to be larger than tol so that we can enter the % following while loop: error = 2*tol; % enter the while loop. CAREFUL, need to take the absolute value of the % error since if it's negative we'd exit the while loop immediately. while abs(error) > tol, % set the flops count to zero: flops(0); % call the trapezoidal rule subroutine with n mesh-points: [area,h,error] = trap(a,b,n); t_run = flops; % now want to double the number of intervals and try again. There % are (n-1) intervals, so we want 2*(n-1) = 2n-2 intervals and % 2n-1 mesh-points. Since n is the present number of mesh-points, % we replace n with 2n-1: n = 2*n-1; end