% demo_fd.m % % Demonstrate effect of floating point arithmetic on divided difference % approximations to the derivative of f(x) = exp(x) at x0=1. h = 1; n = 70; x0 = 1; expx0 = exp(x0); hvec = zeros(n,1); evec = zeros(n,1); true_deriv = expx0; for i = 1:n ddif = (exp(x0+h) - expx0)/h; % Compute divided difference approx. abs_error = abs(ddif - true_deriv); % Compute error of approx. hvec(i) = h; evec(i) = abs_error; h = h/2; % Decrease step size h. end loglog(hvec,evec) xlabel('h'), ylabel('abs error(h)') title('Divided Difference Approx Error vs h')