import numpy as np import matplotlib.pyplot as plt f= lambda x: 0.1*x**5-0.2*x**3+0.1*x-0.2 df= lambda x: 0.5*x**4-0.6*x**2+0.1 h=0.1 x=np.linspace(-0.3,0.3) plt.plot(x,df(x),'-k') #forward differences diff1= (f(x+h)-f(x))/h plt.plot(x,diff1,'-b') #backward differences diff1= (f(x)-f(x-h))/h plt.plot(x,diff1,'-r') #central differences diff1= (f(x+h)-f(x-h))/(2*h) plt.plot(x,diff1,'-g') plt.xlabel('x') plt.ylabel('y') plt.legend(["Equat.","FFD","BFD","CFD"]) plt.grid() plt.show