More Related Content
Similar to Ppt 136-140 (20)
More from hungchiayang1 (20)
Ppt 136-140
- 2. 函數微分計算 (一)
137國立中央大學數學系
利用微分公式估算微分:
微分函數
微分估算
程式範例:
# 函式
def f( x ) :
return pylab.sqrt( pylab.cos(2*x) )
# 微分函式
def df( x ) :
return ( -pylab.tan(2*x)*abs(pylab.cos(2*x)) /
pylab.sqrt( abs(pylab.cos(2*x)) ) )
# 微分估算
def df2( x , h ) :
return ( f(x+h) - f(x) ) / h
- 4. 函數微分計算 (三)
139國立中央大學數學系
import pylab
# 設定函式
def f(x) :
return pylab.sin(x) + pylab.cos(2*x)
# 真正微分
def df(x) :
return pylab.cos(x) - 2*pylab.sin(2*x)
# 利用微分定義估算微分
def df2(x,h) :
return ( f(x+h) - f(x) ) / h
pi = pylab.pi
# 設定 x 範圍 [a,b] 之間,n 為座標點數
a , b , n = 0 , 2*pi , 50
# h 區間大小
h = (b-a)/(n-1)
## 在 [a,b] 之間產生 n 個點存到 xs
xs = pylab.linspace(a,b,n)
## ys 為所有 xs 的 y 值
ys = f(xs)
## ys1 為所有 xs 的真正微分值
ys1 = df(xs)
## ys2 為所有 xs 的估微分值
ys2 = df2(xs,h)
## 畫紙底色為 white
pylab.figure(facecolor=’w’)
- 5. 函數微分計算 (四)
140國立中央大學數學系
## 畫圖:函數
pylab.plot(xs,ys,lw=2,label="f(x)")
## 畫圖:函數微分
pylab.plot(xs,ys1,label="exact f’(x)")
## 畫圖:函數微分估算
pylab.plot(xs,ys2,label="computed f’(x)")
## 顯示背景格線
pylab.grid()
# 設定 X 與 Y 軸
pylab.xlabel("X")
pylab.ylabel("Y")
# 設定標頭
pylab.title("sin(x)+cos(2x) and derivatives")
# 圖示:loc=2 圖形左上方
pylab.legend(loc=2)
# 儲存檔案
pylab.savefig(’derivatives.png’)
## 螢幕顯示圖形
pylab.show()