function err=der_demo( f, df, x )
% err = der_demo( f, df, x )
%
%  f  - a function handle
%  df - function handle for the exact derivative
%  x  - the value to evaluate the derivative at
%
% Example:
%  f=@(x)(exp(pi*x));
%  df=@(x)(pi*exp(pi*x));
%  der_demo( f, df, 1.0 );


n = 20;
dx = logspace(-0,-5,n);
err = zeros(n,1);

xm1 = x-dx;
xm2 = xm1-dx;
xp1 = x+dx;
xp2 = xp1+dx;

der4 = ( f(xm2) - 8*f(xm1) + 8*f(xp1) - f(xp2) )./(12*dx);
der2 = ( f(xp1) - f(xm1) ) ./ (2*dx);
der1 = ( f(xp1) - f(x) ) ./ dx;
err2 = abs(der2-df(x));
err4 = abs(der4-df(x));
err1 = abs(der1-df(x));

loglog(dx,err1,dx,err2,dx,err4,'LineWidth',2);

legend('First Order','Second Order','Fourth Order','Location','best');
xlabel('h','FontSize',18);
ylabel('Error in df/dx','FontSize',18);
set(gca,'FontSize',16);
grid;