I have a function
f(x) = sin(x)/x^3 whose first derivative I am trying to estimate using 1st, 2nd and 4th order Finite Difference schemes. I tried to plot the truncation error in MATLAB and I found myself with increasing error with smaller step size. I am not sure where I am wrong. Attached is my code snippet.
syms y dx = linspace(10^(-5), 10^(-0)); f = @(x) sin(x)/x^3; x = 4; df1 = (f(x+1)-f(x))./dx; df2 = (f(x+1)-f(x-1))./(2.*dx); df4 = (f(x-2)-8*f(x-1)+8*f(x+1)-f(x+2))./(12.*dx); exact = cos(x)/x^3 - (3*sin(x))/x^4; for i=1:length(dx) error1(i) = abs(df1(i)-exact) error2(i) = abs(df2(i)-exact) error4(i) = abs(df4(i)-exact) end figure loglog(dx,error1,'r','linewidth',2) hold on loglog(dx,error2,'b','linewidth',2) loglog(dx,error4,'k','linewidth',2)