SlideShare une entreprise Scribd logo
1  sur  41
1
SymPy 符號運算套件
簡要 python 學習講義
2
國立中央大學數學系
簡要 python 學習講義
 SymPy 簡介請由以下網址下載
http://www.python.math.ncu.edu.tw/files/sympy.pdf
SymPy 符號運算套件
 符號運算 Python 套件,始於 2007 年
 為計算機代數系統(Computer Algebra
System),如 Maple、Mathematica、
MathCad
 套件使用 Python 程式語言開發出來
3
國立中央大學數學系
使用 sympy 套件
 import sympy :
使用 sympy 套件內定義的名稱之前需加上套件
名稱,即 sympy.
 from sympy import * :
直接使用 sympy 套件內定義的所有名稱
4
國立中央大學數學系
 為節省列印空間,以下皆使用 from sympy import *
定義符號變數:symbols()
5
國立中央大學數學系
>>> foo = symbols(’t’) # 設定 foo 符號變數代表符號 t
>>> foo
t
>>> a , b = symbols(’x y’) # 設定 a b 分別代表 x 與 y 兩個符號
# ’x y’ 也可加逗號寫成 ’x, y’
>>> a
x
>>> foo , a , b # 輸出 foo , a , b 三個符號變數
(t, x, y)
>>> foo - 2*a + 3*a # 簡單符號運算
t + x
>>> fn = a + 2*sin(foo) # 定義新符號變數 fn
>>> fn # 輸出 fn
x + 2*sin(t)
預設符號變數:var()
6
國立中央大學數學系
>>> x , y = symbols(”x, y”) # 設定 x 與 y 變數代表 x 與 y 兩符號
>>> var(”x y”) # 效果同上
(x, y)
>>> x + 2*y - 3*x
-2*x + 2*y
>>> w , t = var(”a, b”) # 也可如 symbols 一樣設定符號變數
>>> w + 3*t
a + 3*b
設定多個符號變數:使用「:」範圍
7
國立中央大學數學系
>>> var(”x:3”) # 三個符號變數:x0,x1,x2
(x0, x1, x2)
>>> var(”x:z”) # 三個符號變數:x,y,z
(x, y, z)
>>> var(”x:z:2”) # 六個符號變數,以上每個再依次
(x0, x1, y0, y1, z0, z1) 新增 0 1 兩個
展開與取代:展開 expand() 與取代 subs()
8
國立中央大學數學系
>>> var("x,y")
(x, y)
>>> ((x+2*y)**2).expand() # expand() 展開
x**2 + 4*x*y + 4*y**2
>>> expand((x+2*y)**2) # 同上
x**2 + 4*x*y + 4*y**2
>>> (x-y)**2).subs(x,1) # subs() :x 用 1 取代
(-y + 1)**2
>>> ((x+y)**2).subs(x,-2*y) # x 用 -2y 取代
y**2
>>> ((x-y)**2).subs(x,1).expand() # 先取代再展開
y**2 - 2*y + 1
因式分解:factor()
9
國立中央大學數學系
>>> ((x**2-2*x+1)).factor() # 因式乘積
(x - 1)**2
>>> ((x-1)**2).expand().factor() # 先展開再回復
(x - 1)**2
>>> (x**2-1)/(x-1)
(x**2 - 1)/(x - 1)
>>> fn = x**3 - 4*x - 1 # 定義 fn gn 兩式子
>>> gn = -2*x**2 + x + 5
>>> factor(fn-gn) # 因式分解
(x - 2)*(x + 1)*(x + 3)
簡化運算式:simplify()
10
國立中央大學數學系
>>> ((x**2-1)/(x-1)).simplify() # 簡化
x + 1
>>> simplify((x**2-1)/(x-1)) # 同上
x + 1
>>> fn = cos(x)**2 - sin(x)**2 # 定義 fn gn 兩式子
>>> gn = sin(2*x)
>>> fn/gn # 式子相除
(cos(x)**2 - sin(x)**2)/sin(2*x)
>>> simplify(fn/gn) # 簡化運算
1/tan(2*x)
>>> (gn**2/fn).simplify() # 簡化運算
sin(2*x)**2/cos(2*x)
Rational(a,b) :b/a 分數
11
國立中央大學數學系
>>> 1/3 # 浮點數
0.3333333333333333
>>> Rational(2,6) # 自動約分
1/3
>>> a = Rational(8)/3 # 等同設定 a = Rational(8,3)
>>> a**2
64/9
>>> # y 用 1/3 浮點數取代
>>> ((x-y)**2).subs(y,1/3)
(x - 0.333333333333333)**2
>>> # y 用分數 1/3 取代
>>> ((x-y)**2).subs(y,Rational(1,3))
(x - 1/3)**2
>>> ((x-y)**2).subs(y,Rational(1,3)).expand()
x**2 - 2*x/3 + 1/9
π,e,∞
12
國立中央大學數學系
>>> pi # 圓周率符號變數
pi
>>> E # 大寫 E 代表 Euler 數
E
>>> oo # 兩個小 o,無限大符號變數
oo
>> oo * oo # 無限大相乘
oo
>> oo - oo # 無限大相減,無定義
nan # nan 代表 not a number
常用函數
13
國立中央大學數學系
evalf :計算式子數值
14
國立中央大學數學系
>>> pi.evalf()
3.14159265358979
>>> x + Rational(1,2)*pi
x + pi/2
>>> (x + Rational(1,2)*pi).evalf()
x + 1.5707963267949
>>> asin(1)
pi/2
>>> asin(1).evalf()
1.57079632679490
微積分的應用
 limit():極限
 diff()、Derivative():微分
 integrate()、Integral():積分
 series():級數展開
15
國立中央大學數學系
 一般極限:
16
國立中央大學數學系
>>> ((x**2-1)/(x+1)).subs(x,-1)
nan
>>> limit( (x**2-1)/(x+1) , x , -1 )
-2
>>> ((x**2-1)/(x+1)).limit(x,-1) # 同上
-2
limit():極限 (一)
 單邊極限:
17
國立中央大學數學系
>>> limit( floor(x)/x , x , 3 )
1
>>> limit( floor(x)/x , x , 3 , ’+’ ) # 同上
1
>>> limit( floor(x)/x , x , 3 , ’-’ )
2/3
>>> limit( sin(1/x) , x , 0 , ’+’ )
AccumBounds(-1,1) # 在 [-1,1] 之間
>>> limit( sqrt(x)*sin(1/x) , x , 0 , ’+’ )
limit():極限 (二)
 無窮極限:
18
國立中央大學數學系
limit():極限 (三)
>>> limit( (x**3-4)/(2*abs(x)**3+2) , x , oo )
1/2
>>> limit( (x**3-4)/(2*abs(x)**3+2) , x , -oo )
-1/2
>>> limit( sin(1/x) , x , oo )
0
>>> fn = x*sin(1/x)
>>> limit( fn , x , oo )
1
 Limit() 與 limit()
Limit() 為 limit() 的未運算版
在 init_printing() 下,Limit() 可印出漂亮
的輸出內容
Limit() 與 doit() 配合效果等同 limit()
19
國立中央大學數學系
limit():極限 (四)
20
國立中央大學數學系
limit():極限 (五)
 pprint() 與 pretty() :漂亮列印,漂亮字串
 在程式中,可使用 pprint() 產生漂亮的輸出
 pretty 可將 pprint() 的漂亮輸出存成字串
 pprint() 或列印 pretty 字串最好由新列起始,
否則會有對齊不良的情況發生
21
國立中央大學數學系
limit():極限 (六)
22
國立中央大學數學系
limit():極限 (七)
from sympy import *
init_printing()
var("x")
fn = x + floor(x) # fn = x +
for i in range(2) :
y1 = Limit( fn , x , i , ’-’ )
y2 = Limit( fn , x , i , ’+’ )
pprint(y1) # 漂亮列印 y1
print( ’=’ , y1.doit() , end="nn" )
pprint(y2) # 漂亮列印 y2
print( ’=’ , y2.doit() , end="nn" )
y3 = Limit( fn , x , i+Rational(1,2) )
pprint(y3) # 漂亮列印 y3
print( ’=’ , y3.doit() , end="nn" )
23
國立中央大學數學系
limit():極限 (八)
 pprint() 函式也產生微分,積分等函式的漂亮輸出
diff():微分 (一)
 單變數函式:
24
國立中央大學數學系
>>> fn = x**3 + 2*x**2 + 1
>>> diff(fn) # 計算一次微分
2
3·x + 4·x
>>> diff(fn,x) # 同上
2
3·x + 4·x
>>> diff(fn,x,1) # 同上,1 代表一次微分
2
3·x + 4·x
>>> diff(fn,x,2) # 函式 fn 對 x 連續執行兩次微分
2·(3·x + 2)
>>> diff(fn,x,x) # 同上
2·(3·x + 2)
>>> diff(fn,x,3) # 三次微分
6
>>> diff(fn,x,x,x) # 同上
6
diff():微分 (二)
 多變數函式:偏微分
25
國立中央大學數學系
>>> var(”x,y”) # 定義兩符號變數
>>> fn = x**3 + y**3 + x*y
>>> fn
3 3
x + x·y + y
>>> diff(fn,x) # fn 對 x 微分
2
3·x + y
>>> diff(fn,x,y) # fn 先對 x 微分,再對 y 微分
2
x + 3·y
>>> diff(fn,x,2,y) # fn 先對 x 兩次微分,再對 y 微分
0
>>> diff(fn,x,x,y) # 同上
0
diff():微分 (三)
 Derivative:顯示漂亮微分式子
Derivative 在互動模式下可將微分式子以漂亮方式
顯示出來
在程式執行中則需使用 pprint() 才能印出漂亮式子
Derivative 僅印出微分式子,並不會如 diff 一樣
計算微分
Derivative 後執行 doit() 等同 diff
26
國立中央大學數學系
diff():微分 (四)
 範例一:計算函式對 x 的前四次偏微分
27
國立中央大學數學系
from sympy import *
init_printing()
var("x,y")
fn = cos(x*y) + sin(y)
# 顯示與計算 fn 函數對 x 的一到四次偏微分
for i in range(4) :
# dfn 儲存微分的漂亮輸出
dfn = Derivative(fn,x,i+1)
# 漂亮列印微分式子
pprint( dfn )
print( ’=’ , dfn.doit() ) # dfn.doit() 等同 diff(fn,x,i)
print()
輸出見次頁。
diff():微分 (五)
28
國立中央大學數學系
diff():微分 (六)
 範例二:計算函數的所有二次偏微分
29
國立中央大學數學系
from sympy import *
init_printing()
var("x,y")
# 定義函數
fn = cos(x*y) + sin(y)
# v1 在 x , y 兩符號變數迭代
for v1 in [ x , y ] :
# v2 在 x , y 兩符號變數迭代
for v2 in [ x , y ] :
# fn 先對 v1 微分,再對 v2 微分
dfn = Derivative(fn,v1,v2)
# 漂亮列印 fn 的微分式子
pprint( dfn )
# 印出 dfn 的微分運算結果
print( ’= ’ , dfn.doit() )
print()
integrate():積分 (一)
 單變數積分
 不定積分
30
國立中央大學數學系
integrate():積分 (二)
 定積分
31
國立中央大學數學系
integrate():積分 (三)
 多變數積分
 不定積分
32
國立中央大學數學系
integrate():積分 (四)
 定積分
33
國立中央大學數學系
integrate():積分 (五)
 Integral:顯示漂亮積分式子
 Integral 在互動模式下可將積分式子以漂亮方式顯示出來
 在程式執行中則需使用 pprint() 才能印出漂亮式子
 Integral 僅印出積分式子,並不會如 integrate 一樣
計算積分
 Integral 後執行 doit() 等同 integrate
34
國立中央大學數學系
integrate():積分 (六)
35
國立中央大學數學系
from sympy import *
init_printing()
var("x")
fn = x**2*cos(x)
# 列印連續三次積分過程
for i in range(3) :
ifn = Integral(fn,x) # 儲存漂亮積分式子
fn = ifn.doit() # 執行積分並變更 fn 為積分後函式
pprint( ifn ) # 列印漂亮積分式子
print( ’=’ )
pprint( fn ) # 列印漂亮 fn 函式
print()
integrate():積分 (七)
輸出:
36
國立中央大學數學系
series:級數展開 (一)
 泰勒展開式
37
國立中央大學數學系
>>> sin(x).series() # 預設到 O(x**6) 誤差
x - x**3/6 + x**5/120 + O(x**6)
>>> series(sin(x)) # 同上
x - x**3/6 + x**5/120 + O(x**6)
>>> sin(x).series(x,0,4) # 展開誤差到 O(x**4)
x - x**3/6 + O(x**4)
>>> sin(x).series(x,0,5) # 展開誤差到 O(x**5)
x - x**3/6 + O(x**5)
>>> sin(x).series(x,0,5).removeO() # 去除誤差項
-x**3/6 + x
series:級數展開 (二)
 Maclaurin 展開式:對 x = x0 展開
38
國立中央大學數學系
>>> cos(x-2).series(x,x0=2) # 對 x0 展開
1 - (x - 2)**2/2 + (x - 2)**4/24 + O((x - 2)**6, (x, 2))
>>> cos(x-2).series(x,x0=2,n=6).removeO() # 展開誤差到
(x - 2)**4/24 - (x - 2)**2/2 + 1 O(x**6)
習題 1
39
國立中央大學數學系
 不用理會顏色變化
印出來。
40
 撰寫程式驗證函數 x cos(x)sin(y) 對 x 兩次微分與
對 y 兩次微分,不管微分順序如何都可得到一樣的結果,
印出以下的輸出樣式:
國立中央大學數學系
習題 2-1
41
可參考講義第 9 頁範例二程式,修改程式使用四層迴圈如下:
國立中央大學數學系
習題 2-2
var("x,y")
...
for v1 in [ x , y ] :
for v2 in [ x , y ] :
for v3 in [ x , y ] :
for v4 in [ x , y ] :
if v1+v2+v3+v4 == 2*x+2*y :
...

Contenu connexe

Similaire à SymPy在微積分上的應用.ppt

ncuma_函數微分計算.pptx
ncuma_函數微分計算.pptxncuma_函數微分計算.pptx
ncuma_函數微分計算.pptxNCU MCL
 
函數微分_範例.pptx
函數微分_範例.pptx函數微分_範例.pptx
函數微分_範例.pptxmclmath
 
ncuma_邏輯與迴圈.pptx
ncuma_邏輯與迴圈.pptxncuma_邏輯與迴圈.pptx
ncuma_邏輯與迴圈.pptxNCU MCL
 
ncuma_函數畫圖.pptx
ncuma_函數畫圖.pptxncuma_函數畫圖.pptx
ncuma_函數畫圖.pptxNCU MCL
 
函數畫圖.pptx
函數畫圖.pptx函數畫圖.pptx
函數畫圖.pptxmclmath
 
第2章符 号 运 算
第2章符 号 运 算第2章符 号 运 算
第2章符 号 运 算eterou
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Derek Lee
 
ncuma_型別與迴圈.pptx
ncuma_型別與迴圈.pptxncuma_型別與迴圈.pptx
ncuma_型別與迴圈.pptxNCU MCL
 

Similaire à SymPy在微積分上的應用.ppt (20)

Ppt 136-140
Ppt 136-140Ppt 136-140
Ppt 136-140
 
ncuma_函數微分計算.pptx
ncuma_函數微分計算.pptxncuma_函數微分計算.pptx
ncuma_函數微分計算.pptx
 
函數微分_範例.pptx
函數微分_範例.pptx函數微分_範例.pptx
函數微分_範例.pptx
 
Ppt 138-142
Ppt 138-142Ppt 138-142
Ppt 138-142
 
ncuma_邏輯與迴圈.pptx
ncuma_邏輯與迴圈.pptxncuma_邏輯與迴圈.pptx
ncuma_邏輯與迴圈.pptx
 
Ch2 教學
Ch2 教學Ch2 教學
Ch2 教學
 
Ch9
Ch9Ch9
Ch9
 
Ch5
Ch5Ch5
Ch5
 
Ch9 教學
Ch9 教學Ch9 教學
Ch9 教學
 
Ch2
Ch2Ch2
Ch2
 
P127 135 new
P127 135 newP127 135 new
P127 135 new
 
ncuma_函數畫圖.pptx
ncuma_函數畫圖.pptxncuma_函數畫圖.pptx
ncuma_函數畫圖.pptx
 
函數畫圖.pptx
函數畫圖.pptx函數畫圖.pptx
函數畫圖.pptx
 
第2章符 号 运 算
第2章符 号 运 算第2章符 号 运 算
第2章符 号 运 算
 
Ppt 127-135
Ppt 127-135Ppt 127-135
Ppt 127-135
 
Ppt 127-135
Ppt 127-135Ppt 127-135
Ppt 127-135
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18
 
ncuma_型別與迴圈.pptx
ncuma_型別與迴圈.pptxncuma_型別與迴圈.pptx
ncuma_型別與迴圈.pptx
 
Ch5 範例
Ch5 範例Ch5 範例
Ch5 範例
 
Ch11 範例
Ch11 範例Ch11 範例
Ch11 範例
 

Plus de mclmath

牛頓迭代法_範例.pptx
牛頓迭代法_範例.pptx牛頓迭代法_範例.pptx
牛頓迭代法_範例.pptxmclmath
 
二分逼近法_習題1.pptx
二分逼近法_習題1.pptx二分逼近法_習題1.pptx
二分逼近法_習題1.pptxmclmath
 
二分逼近法_範例.pptx
二分逼近法_範例.pptx二分逼近法_範例.pptx
二分逼近法_範例.pptxmclmath
 
函數微分_習題3.pptx
函數微分_習題3.pptx函數微分_習題3.pptx
函數微分_習題3.pptxmclmath
 
函數微分_習題2.pptx
函數微分_習題2.pptx函數微分_習題2.pptx
函數微分_習題2.pptxmclmath
 
函數微分_習題1.pptx
函數微分_習題1.pptx函數微分_習題1.pptx
函數微分_習題1.pptxmclmath
 
函數畫圖_習題3.pptx
函數畫圖_習題3.pptx函數畫圖_習題3.pptx
函數畫圖_習題3.pptxmclmath
 
函數畫圖_習題2.pptx
函數畫圖_習題2.pptx函數畫圖_習題2.pptx
函數畫圖_習題2.pptxmclmath
 
函數畫圖_習題1.pptx
函數畫圖_習題1.pptx函數畫圖_習題1.pptx
函數畫圖_習題1.pptxmclmath
 
9習題五.pptx
9習題五.pptx9習題五.pptx
9習題五.pptxmclmath
 
9習題四.pptx
9習題四.pptx9習題四.pptx
9習題四.pptxmclmath
 
極座標畫圖:範例程式.pptx
極座標畫圖:範例程式.pptx極座標畫圖:範例程式.pptx
極座標畫圖:範例程式.pptxmclmath
 
SymPy範例程式講義下載連結更新.ppt
SymPy範例程式講義下載連結更新.pptSymPy範例程式講義下載連結更新.ppt
SymPy範例程式講義下載連結更新.pptmclmath
 
Taylor exercise1
Taylor exercise1Taylor exercise1
Taylor exercise1mclmath
 

Plus de mclmath (14)

牛頓迭代法_範例.pptx
牛頓迭代法_範例.pptx牛頓迭代法_範例.pptx
牛頓迭代法_範例.pptx
 
二分逼近法_習題1.pptx
二分逼近法_習題1.pptx二分逼近法_習題1.pptx
二分逼近法_習題1.pptx
 
二分逼近法_範例.pptx
二分逼近法_範例.pptx二分逼近法_範例.pptx
二分逼近法_範例.pptx
 
函數微分_習題3.pptx
函數微分_習題3.pptx函數微分_習題3.pptx
函數微分_習題3.pptx
 
函數微分_習題2.pptx
函數微分_習題2.pptx函數微分_習題2.pptx
函數微分_習題2.pptx
 
函數微分_習題1.pptx
函數微分_習題1.pptx函數微分_習題1.pptx
函數微分_習題1.pptx
 
函數畫圖_習題3.pptx
函數畫圖_習題3.pptx函數畫圖_習題3.pptx
函數畫圖_習題3.pptx
 
函數畫圖_習題2.pptx
函數畫圖_習題2.pptx函數畫圖_習題2.pptx
函數畫圖_習題2.pptx
 
函數畫圖_習題1.pptx
函數畫圖_習題1.pptx函數畫圖_習題1.pptx
函數畫圖_習題1.pptx
 
9習題五.pptx
9習題五.pptx9習題五.pptx
9習題五.pptx
 
9習題四.pptx
9習題四.pptx9習題四.pptx
9習題四.pptx
 
極座標畫圖:範例程式.pptx
極座標畫圖:範例程式.pptx極座標畫圖:範例程式.pptx
極座標畫圖:範例程式.pptx
 
SymPy範例程式講義下載連結更新.ppt
SymPy範例程式講義下載連結更新.pptSymPy範例程式講義下載連結更新.ppt
SymPy範例程式講義下載連結更新.ppt
 
Taylor exercise1
Taylor exercise1Taylor exercise1
Taylor exercise1
 

SymPy在微積分上的應用.ppt