function finite_difference_demo(x,f,fpr)
%
% Demonstrates forward- and central-difference approximations 
% of f'(x) and computes the error. 
% 
% Inputs: 
%   x   = point at which f' is approximated 
%   f   = function handle for f
%   fpr = function handle for f'
%

if nargin < 3, 
   error('Not enough input arguments.');
end

while 1 == 1

  ind = input(['\n Choose an option: \n   0 => quit \n',...
           '   1 => forward difference approximation\n', ...
	   '   2 => central difference approximation\n']); 

  if ind <= 0, break; end 
  if ind > 2, 
    disp('Must choose option 0, 1, or 2.'); 
    ind = input(['\n Choose an option: \n   0 => quit \n',...
           '   1 => forward difference approximation\n', ...
	   '   2 => central difference approximation\n']); 
    end

  if ind == 1 
    disp(' '); 
    disp('Forward difference approximation:'); 
    disp(' '); 
    fprintf('    h          FD approx.           true value             error \n'); 
    h = 1; 
    for n = 1:16
      h = 10\h;
      fpr_app = h\(feval(f,x+h)-feval(f,x)); 
      fpr_val = feval(fpr,x);
      fprintf(' %.1e   %.12e   %.12e   %.12e \n',h,fpr_app,fpr_val,abs(fpr_app-fpr_val));
    end 
  else
    disp(' '); 
    disp('Central difference approximation:'); 
    disp(' '); 
    fprintf('    h          CD approx.           true value             error \n'); 

    h = 1; 
    for n = 1:16
      h = 10\h;
      fpr_app = (2*h)\(feval(f,x+h)-feval(f,x-h)); 
      fpr_val = feval(fpr,x);
    fprintf(' %.1e   %.12e   %.12e   %.12e \n',h,fpr_app,fpr_val,abs(fpr_app-fpr_val));
    end 
  end
end 

   
