% Compute the derivative of a function
% using forward difference formula with 
% acceleration from Richardson extrapolation
% Best result is given by R(end,end)
% K. Ming Leung, 02/10/03

clear all; 
fcn='sin'; 
x=1; 
p= 1; 
itMax = 9;   % best result is itMax = 8
h = 0.50; T(1,1) = forwardDiff(fcn,x,h);
h = 0.25; T(2,1) = forwardDiff(fcn,x,h);
q = 2;
qp = q^p;
T(2,2) = (qp*T(2,1)-T(1,1))/(qp-1);

for k = 2:itMax
    n = 2^k; h=0.5/n;
    T(k+1,1) = forwardDiff(fcn,x,h);
    for j = 2:k+1
        c = 2^(j-1);
        T(k+1,j) = (c*T(k+1,j-1)-T(k,j-1))/(c-1);
    end
end
I=T, 
exact=cos(1);   % Exact answer
error=I(end,end)-exact,