""" Compute the absolute- and relative errors for the approximation f(x) ~= ( f(x+h) - f(x) )/h when f(x) = sin(x), x = 1.2 and h -> 0+ """ import matplotlib.pyplot as plt import numpy as np # # Compute the finite difference for different h's # x0 = 1.2 f0 = np.sin(1.2) fp_exact = np.cos(1.2) err_abs = [] err_asym = [] h = [] for i in np.linspace(-20,0,41): hi = 10**i h.append(hi) fp_approx = (np.sin(x0+hi)-f0)/hi err_abs.append(abs(fp_exact) - fp_approx) err_asym.append(0.5*f0*hi) plt.subplot(111) plt.loglog(h,err_abs,'-*b',h,err_asym,'--r') plt.show() """ # # Plot the absolute error # loglog(h,err_abs,'-*',h,err_asym,'--') xlabel('h') ylabel('Absolute error') """