1 #coding=utf-8 2 from sympy import * 3 4 h = input("请输入h的值:") 5 #定义变量x 6 x=Symbol("x") 7 #定义函数f 8 f = -0.1*x**4-0.15*x**3-0.5*x**2-0.25*x+1.2 9 d = diff(f,x,1) 10 #向前差商 11 d.subs(x,0.5) 12 g1 = (f.subs(x,0.5 + h) - f.subs(x,0.5))/ h 13 print "向前差商结果为:" 14 print g1 15 #向后差商 16 g2= (f.subs(x,0.5) - f.subs(x,0.5 - h)) / h 17 print "向后差商结果为:" 18 print g2 19 #中心差商 20 g3 = (f.subs(x,0.5 + h) - f.subs(x,0.5-h)) / (2 * h) 21 print "中心差商的结果为:" 22 print g3
转载于:https://www.cnblogs.com/the-wang/p/8021483.html