clear
echo on
%This program demonstrates the machine round off
%error and algorithm error of the forward difference
%algorithm for calculating the derivative of a function.
%The forward difference algorithm is:
%      df/dx = (f(x+h)-f(x))/h
%where h is some small quantity.  We apply the formula
%to the function f(x)=exp(x) over the range x=0:.1.
%We average the error for a number of different values
%of x in order to average out the random round off
%error.  We also pick values of h in the range of
%e-6 to e-9.
pause

x=[0:.001:.1];
for i=1:30
  h(i)=10^(-i/10)/10^6;
  deriv=(exp(x+h(i))-exp(x))/h(i);
  error(i)=sum(abs(deriv-exp(x)))/101;
  echo off
end;
echo on

figure(1)
loglog(h,error)
xlabel('h')
ylabel('error')
grid on

%As can be seen, the minimum error occurs for a value
%of h of about 1.5*10^-8.  From the formula for the
%error of forward differences, this means that the
%machine precision is of order 10^-16, or double
%precision.  You will also note that the slope of the
%error is -1 on the left and +1 on the right (for loglog
%scale).  This is because the error for small h is
%proportional to 1/h (no algorithm error, just round off
%error), while for larger h it is proportional to h
%(just algorithm error).

echo off