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;
end;
loglog(h,error)
xlabel('h')
ylabel('error')
pause

%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 about 0.5*10^-16, or double
%precision.

echo off