%-----------------------------------------------------------------------
% Script File: derr
%  Plot error (and it's derivative) vs. h to exhibit optimal step
clear
fname = 'sin';
fname1= 'cos';
delta = eps;
hmin  = 10^6*eps;
hmax  = 10^10*eps;
xmax  = 1;
x=pi/4;
M2=1;
[d,err,hopt] = Derivative(fname,x,delta,M2);
yopt = (M2/2)*hopt+2*delta/hopt;
   h = linspace(hmin,hmax,1000);
   y = (M2/2)*h+2*delta./h;
   y1=   (M2/2)*h;
   y2= 2*delta./h;
   derivh =(feval(fname,x+h) - feval(fname,x))./h;
   abserr=abs(feval(fname1,x)-derivh);
   loglog(h,y,h,y1,h,y2,h,abserr,hopt,yopt,'bo');
   xlabel(sprintf('hopt = %3.2d, d%s(%3.2d)/dx=%3.2d +/-%3.2d',hopt,...
      fname ,x,d,err))
   ylabel('abserr, maxerr')
   title(sprintf('derivative error as function of h for %s(x)',fname))
   axis([hmin hmax 10^(-12) 10^(-4)])
