function derivative_error
%DERIVATIVE ERROR
%   Measures the total error (truncation error plus roundoff
%   error) as a function of the step size, when approximating
%   the derivative of a function.
%
%   (C) 2010/12/01 by Benjamin Seibold

f = @(x)exp(x); % define a function
x0 = 0; % define an approximation point
h = 10.^linspace(-16,0,400); % list of step sizes
df1 = (f(x0+h)-f(x0))./h; % one sided derivative approximations
df2 = (f(x0+h)-f(x0-h))./(2*h); % central derivative approximations
clf, loglog(h,abs(df1-1),h,abs(df2-1)) % plot errors in log-log scale
grid on, xlabel('step size h'), ylabel('total error')
legend('one sided','central',3)

