import numpy as np import matplotlib.pyplot as plt from math import cos,sin,fabs def f(x): return sin(x) def df(x): return cos(x) def dfh(f,x,h): return (f(x+h)-f(x)) / h n = 100 h_vec = 10.0**np.linspace(-14, -3, n) error_vec = np.zeros((n,1)) x = 1 for i in range(n): error_vec[i] = fabs(dfh(f,x,h_vec[i]) - df(x)) plt.loglog(h_vec, np.asarray(error_vec),'*-',label='sin(x), x=1') plt.xlabel('h') plt.ylabel('feil i numerisk derivert') plt.legend() plt.savefig('numdiff.pdf') plt.show()