%MATLAB script to illustrate Richardson’s extrapolation for derivative
%%
MyFunc=@(x) x.^2.*cos(x)
MyDeriv=@(x)x.*(2.*cos(x)-x.*sin(x))
%%
Dexact=MyDeriv(2)
%%
xi=2;
Delta1=0.2;
xim2=xi-2*Delta1;
xim1=xi-Delta1;
xip1=xi+Delta1;
xip2=xi+2*Delta1;
D1=(1./(12*Delta1))*(MyFunc(xim2)-8*MyFunc(xim1)+8*MyFunc(xip1)-MyFunc(xip2))
%%
Delta2=0.1;
xim2=xi-2*Delta2;
xim1=xi-Delta2;
xip1=xi+Delta2;
xip2=xi+2*Delta2;
D2=(1./(12*Delta2))*(MyFunc(xim2)-8*MyFunc(xim1)+8*MyFunc(xip1)-MyFunc(xip2))
%%
D=D2*(1+1/((Delta1/Delta2)^4-1))-D1*(1/((Delta1/Delta2)^4-1))