% truncation_example_1.m % Truncation error example #1 % Reducing "h" to make the secant slope a better approximation % to the derivative which is the mathematical limit of secant slopes. x = 1; % point at which we are approximating the derivative fprintf('\nTrying to approximate slope of sin at %f\n',x); true_slope = cos(x) % (here we happen to know the derivative exactly) n = 8; fprintf(' h secant slope error\n'); for i = 1:n h=10^-i; secant_slope = ( sin(x+h) - sin(x) ) / h; the_error = secant_slope - true_slope; fprintf('%23.15f %23.15f %23.15f\n',h,secant_slope,the_error); end