码迷,mamicode.com
首页 > 其他好文 > 详细

(二)向前 向后 中心差商

时间:2017-12-11 11:12:47      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:col   body   class   div   log   inpu   diff   nbsp   sub   

 

 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

 

(二)向前 向后 中心差商

标签:col   body   class   div   log   inpu   diff   nbsp   sub   

原文地址:http://www.cnblogs.com/the-wang/p/8021483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!