%
%   DERIV_APPROX.M  Calculate approximation to the derivatives of exp(x) at x = 0 
%                      for different step sizes
%                 (used to generate data for Table 3 in Lesson 4 Lecture Notes)
%
%   Written by J. R. White, UMass-Lowell (last update: Oct. 2017)
%

      clear all,   close all
%
%   set different step sizes (decreasing by a factor of two)
      h = [0.5 0.25 0.125 0.0625 0.03125]';
%
%   evaluate fi-1, fi, and fi+1 for each step size (where xi = 0)
      fm = exp(-h);                   % fm = fi-1 = exp(0-h) = exp(-h)
      f = ones(size(h));              % f = fi = exp(0) = 1
      fp = exp(h);                    % fp = fi+1 = exp(0+h) = exp(h)
%
%   now form different derivative approximations
      fpb = (f - fm)./h;              % backward approx to f'
      fpf = (fp - f)./h;              % forward approx to f'
      fpc = (fp - fm)./(2*h);         % central approx to f'
      fp2c = (fm - 2*f + fp)./(h.*h); % central approx to f''
%    
%   create short table of results
      fprintf('       h       fpb       fpf       fpc       fp2c \n')
      for n = 1:size(h)
      fprintf('  %8.5f  %8.5f  %8.5f  %8.5f  %8.5f \n', ...
              h(n),fpb(n),fpf(n),fpc(n),fp2c(n));
      end
%
%  end of problem
