%% Annotated Matlab transcript -- 4/17 %% CS 323 - Doug DeCarlo % Numerical differentiation n = 1:10; h = 2.^(-n) x = pi/6; % approximate cos'(x) at x=pi/6 for different h's d = (cos(x+h)-cos(x))./h; % error (actualy answer is -1/2) e = -0.5 - d; plot(n,e,'.-') % ratio approaches 2 as error is O(h) e(1:end-1)./e(2:end) % semilog plot of error shows a line semilogy(n,e,'.-'); % we can also plot the upper/lower bounds on the error plot(n,e,'.-',n,h/2*cos(pi/6),n,h/2.*cos(pi/6+h)) semilogy(n,e,'.-',n,h/2*cos(pi/6),n,h/2.*cos(pi/6+h))