function t = adaptrap(f,a,b,n) % % function t = adaptrap(f,a,b,n) % % Returns a vector of n approximations for the definite integral of % the function f over the interval [a,b], the i-th entry in the vector % uses 2^i function evaluations. % % Example: % t = adaptrap('sin',0,pi/2,10); % t = zeros(n,1); h = (b-a); % size of subinterval m = 1; % number of subintervals t(1) = (feval(f,a) + feval(f,b))*h/2; for i = 2:n h = h/2; for j=0:m-1 t(i) = t(i) + feval(f,a+h+j*2*h); end; t(i) = t(i-1)/2 + h*t(i); m = 2*m; end;