% Script deriv.m % Uses the limit formula from calculus for estimating the % derivative of a function cos(x) at x = 0.5 % % Why does this eventually fail? num_iter = input('Enter number of iterations '); exact = cos(0.5); fprintf('Exact value = %.10e\n', exact); fprintf('%5s %18s %18s %18s\n','n','h','r','err'); h = 1.0; for n = 1 : num_iter r = (sin(0.5 + h) - sin(0.5)) / h; err = exact - r; fprintf('%5d %18.10e %18.10e %18.10e\n',n,h,r,err); h = h / 4.0; end