# -*- coding: utf-8 -*- f= lambda x: 0.1*x**5-0.2*x**3+0.1*x-0.2 x=0.1 h=0.1 dif1= 0.5*x**4-0.6*x**2+0.1 print(dif1) print("\t f'(x)O(h)\t Error\t\t f'(x)O(h2)\t f'(x)O(h4)") #forward differences diff1= (f(x+h)-f(x))/h diff2= (-f(x+2*h)+(4*f(x+h))-3*f(x))/(2*h) print("FFD\t%f\t%f\t%f"%(diff1,diff1-dif1,diff2)) #backward differences diff1= (f(x)-f(x-h))/h diff2= (3*f(x)-4*f(x-h)+f(x-2*h))/(2*h) print("BFD\t%f\t%f\t%f"%(diff1,diff1-dif1,diff2)) #central differences diff1= (f(x+h)-f(x-h))/(2*h) diff2= (-f(x+2*h)+8*f(x+h)-8*f(x-h)+f(x-2*h))/(12*h) print("CFD\t\t\t%f\t%f\t%f"%(diff1,diff1-dif1,diff2))