2013-06-01 31 views
7

sage這是很容易做到的未知功能F(X)的泰勒展開做一個未知函數$ F(X)$的一個象徵性的泰勒展開,如何使用sympy

x = var('x') 
h = var('h') 
f = function('f',x) 
g1 = taylor(f,x,h,2) 

這怎麼能在sympy中完成?


更新

asmeurer指出,這是一個將很快面市,從拉入請求http://github.com/sympy/sympy/pull/1888 sympy功能。我安裝使用PIP分支,

pip install -e [email protected]:renatocoutinho/[email protected]#egg=sympy --upgrade 

然而,當我嘗試計算系列F(X)的,

x, h = symbols("x,h") 
f = Function("f") 
series(f,x,x+h) 

我碰到下面的錯誤,

TypeError: unbound method series() must be called with f instance as first argument (got Symbol instance instead)

+0

你不能。只需使用循環和'diff'。這個函數被稱爲「系列」而不是「泰勒」。 – Krastanov

+1

在https://github.com/sympy/sympy/pull/1888有一個請求來完成這項工作。 – asmeurer

+0

@asmeurer太棒了!這尚未合併到主分支中,是否可以使用pip進行安裝。或者我需要克隆回購,應用補丁,然後從源生成? –

回答

5

在sympy中沒有此功能,但「手工」操作相當容易:

In [3]: from sympy import * 
     x, h = symbols('x, h') 
     f = Function('f') 
     sum(h**i/factorial(i) * f(x).diff(x, i) for i in range(4)) 

Out[3]: h**3*Derivative(f(x), x, x, x)/6 + h**2*Derivative(f(x), x, x)/2 + h*Derivative(f(x), x) + f(x) 

請注意,sympy通常使用表達式(如f(x)),而不是裸函數(如f)。

8

由於@asmeurer描述,這是現在可以通過

from sympy import init_printing, symbols, Function 
init_printing() 

x, h = symbols("x,h") 
f = Function("f") 

pprint(f(x).series(x, x0=h, n=3)) 

from sympy import series 
pprint(series(f(x), x, x0=h, n=3)) 

兩個回報

           ⎛ 2  ⎞│       
              2 ⎜ d   ⎟│       
            (-h + x) ⋅⎜────(f(ξ₁))⎟│       
               ⎜ 2  ⎟│       
       ⎛ d  ⎞│     ⎝dξ₁  ⎠│ξ₁=h ⎛  3  ⎞ 
f(h) + (-h + x)⋅⎜───(f(ξ₁))⎟│  + ──────────────────────────── + O⎝(-h + x) ; x → h⎠ 
       ⎝dξ₁  ⎠│ξ₁=h    2          

如果你想有一個有限差分逼近,比如,你可以寫

FW = f(x+h).series(x+h, x0=x0, n=3) 
FW = FW.subs(x-x0,0) 
pprint(FW) 

獲得正向逼近,它返回

        ⎛ 2  ⎞│ 
           2 ⎜ d   ⎟│ 
           h ⋅⎜────(f(ξ₁))⎟│ 
            ⎜ 2  ⎟│ 
      ⎛ d  ⎞│   ⎝dξ₁  ⎠│ξ₁=x₀ ⎛ 3 2  2 3     ⎞ 
f(x₀) + h⋅⎜───(f(ξ₁))⎟│  + ────────────────────── + O⎝h + h ⋅x + h⋅x + x ; (h, x) → (0, 0)⎠ 
      ⎝dξ₁  ⎠│ξ₁=x₀    2