%Finite Centered Difference: This m-file will compute the derivative of a
%hardwired function (x^5) using the FCD and compare it to the true value

h=10.^(-20:1);                                      %given h-values

deriv_approx=[(1+h).^5-(1-h).^5]./(2.*h);           %finite centered derivative for each value of h
difference=deriv_approx-5;                          %difference between finite centered difference and true value

cla;
figure;                                                 %set up new plotting area
subplot(2,1,1);semilogx(h,deriv_approx,'g o');           %set up sub-plot and draw semilog plot 
axis([0 h(22) 0 10]);                                    %values for domain and range
xlabel('h value'); ylabel('approx. derivative of x^5 at x=1');
title('Finite Centered Difference');

subplot(2,1,2);semilogx(h,difference,'x');              %second plot shows difference between estimate and true value
axis([0 h(22) 0 10^-6]);
xlabel('h value'); ylabel('difference between FCD and true value');
title('Error in FCD'); 