More Related Content
Similar to Ppt 158-164 (9)
More from hungchiayang1 (14)
Ppt 158-164
- 4. 微分方程式數值求解 (三)
161國立中央大學數學系
此公式需設定起始條件才能求解,若讓 = c,c 要一開始就給定,
整個數值求解才能開始。以上的數值求解法稱為 Euler method,這
是最簡單的數值法用來計算起始值問題(initial value problem)
,缺點為計算精度偏低,若要得到較好的結果,h 要越小越好。
本程式利用 c 迴圈來設定起始值,c 分別為 0、5、10,對相同微分
方程式,三個不同起始值代表三個不同的微分方程式,可產生三個數值
解。一般來說,所有起始值微分方程的數值方法,離起始點越遠,誤差
越大。
- 6. 微分方程式數值求解 (五)
163國立中央大學數學系
import pylab
#----------------------------------------
# y’ = x**(1/3) sin(x) + 0.2
#
# i.c. y(0) = val val = range(0,11,5)
#----------------------------------------
def fn(x) :
return x**(1/3) * pylab.sin(x) + 0.2
# 設定周邊空白為白色
pylab.figure(facecolor=’white’)
# 設定 [a,b] 與執行次數 n
a , b , n = 0 , 20*pylab.pi , 500
dx = (b-a)/n
# 設定 xs , ys
xs = [ a + i*dx for i in range(n+1) ]
ys = [None] * (n+1)
# c :起始值,在此分別為 0 5 10 三數
# 以下計算相同微分方程式但不同起始值的解答
for c in range(0,11,5) :
- 7. 微分方程式數值求解 (六)
164國立中央大學數學系
ys[0] = c
for i in range(n) :
ys[i+1] = ys[i] + dx * fn(xs[i])
sym = ’y(0) = ’ + str(ys[0])
pylab.plot(xs,ys,label=sym)
# 設定圖形標頭文字
pylab.title(r”$y’ = sqrt[3]{x}, sin(x) + 0.2$",fontsize=20)
# 設定 X 軸與 Y 軸文字
pylab.xlabel(’X’)
pylab.ylabel(’Y’)
# 設定各線條圖例位置
pylab.legend(loc=’upper left’)
pylab.show()