function demo4_1 % demo for the accuracy of the two point finite differencies. x=[-1:0.1:1]; %% h=0.01 h=0.01; figure(1) subplot(2,3,1); plot(x,f_der(x),'-r'); hold on; plot(x,(f(x+h)-f(x))/h,'-b'); % forward difference plot(x,(f(x+h)-f(x-h))/(2*h),'-g'); % central difference hold off %% h=0.00001 h=0.00001; figure(1) subplot(2,3,2); plot(x,f_der(x),'-r'); hold on; plot(x,(f(x+h)-f(x))/h,'-b'); % forward difference plot(x,(f(x+h)-f(x-h))/(2*h),'-g'); % central difference hold off %% h=0.000001 h=0.000000000000001; figure(1) subplot(2,3,3); plot(x,f_der(x),'-r'); hold on; plot(x,(f(x+h)-f(x))/h,'-b'); % forward difference plot(x,(f(x+h)-f(x-h))/(2*h),'-g'); % central difference hold off %%%%%%%%%%%%%%%%%%%%%%%% h=0.01; figure(1) subplot(2,3,4); plot(x,f_der(x)-(f(x+h)-f(x))/h,'-b'); % forward difference hold on; plot(x,f_der(x)-(f(x+h)-f(x-h))/(2*h),'-g'); % central difference hold off %% h=0.00001 h=0.00001; figure(1) subplot(2,3,5); plot(x,f_der(x)-(f(x+h)-f(x))/h,'-b'); % forward difference hold on; plot(x,f_der(x)-(f(x+h)-f(x-h))/(2*h),'-g'); % central difference hold off %% h=0.000001 h=0.000000000000001; figure(1) subplot(2,3,6); plot(x,f_der(x)-(f(x+h)-f(x))/h,'-b'); % forward difference hold on; plot(x,f_der(x)-(f(x+h)-f(x-h))/(2*h),'-g'); % central difference hold off function y=f(x) y=x.^2; function y=f_der(x) y=2.*x;