% numerical verification of the finite difference approximation for
% derivatives

h = 0.1*2.^(0:-1:-5);
u = @(x) sin(x);
up = @(x) cos(x);
x0 = pi/3;
E_plus     = ( u(x0+h)-u(x0  ) )./h - up(x0);
E_minus    = ( u(x0  )-u(x0-h) )./h - up(x0);
E_centered = ( u(x0+h)-u(x0-h) )./(2*h) - up(x0);

loglog(h,abs(E_plus),'-o','linewidth',2); hold on;
loglog(h,abs(E_minus),'-*','linewidth',2);
loglog(h,abs(E_centered),'-d','linewidth',2);
legend('Forward one-sided approx','Backward one-sided approx','Centered approx','location','northwest');
grid on;