2012-07-19 69 views
3

我目前使用Sympy來幫助我執行數學計算。現在,我正在嘗試執行數值積分,但每次運行腳本時都會收到錯誤。下面是該腳本:使用Sympy在Python中集成

from sympy import * 
cst = { 'qe':1.60217646*10**-19, 'm0':N(1.25663706*10**-6) } 

d = 3.6*10**-2 
l = 20.3*10**-2 
n = 217.0 
I = 10.2 

# Circum of loops 
circ = l/n; 
# Radius 
r = N(circ/(2*pi)) 
# Flux through a ring a distance R from the ceter 
def flux(rad, I, loopRad): 
    distFromWire = loopRad - rad 
    bPoint = cst['m0']*I/(2*pi*distFromWire) 
    return (bPoint*2*pi*rad) 

# Integrate from r=0 to r=wireRad 
x = Symbol('x') 
ig = Symbol('ig') 
ig = flux(x, I, r) 
print(ig) 
integrate(ig*x,x) 

我相信有可能是錯誤的東西與實際物理/數學,但現在我只是希望它的集成。下面是輸出我得到的,當我運行該腳本:

8.05359718208634e-5*x/(-6.28318530717959*x + 0.000935483870967742) 
Traceback (most recent call last): 
    File "script.py", line 34, in <module> 
    integrate(ig*x,x) 
    File "C:\Python27\lib\site-packages\sympy\utilities\decorator.py", line 24, in threaded_func 
    return func(expr, *args, **kwargs) 
    File "C:\Python27\lib\site-packages\sympy\integrals\integrals.py", line 847, in integrate 
    return integral.doit(deep = False) 
    File "C:\Python27\lib\site-packages\sympy\integrals\integrals.py", line 364, in doit 
    antideriv = self._eval_integral(function, xab[0]) 
    File "C:\Python27\lib\site-packages\sympy\integrals\integrals.py", line 577, in _eval_integral 
    parts.append(coeff * ratint(g, x)) 
    File "C:\Python27\lib\site-packages\sympy\integrals\rationaltools.py", line 42, in ratint 
    g, h = ratint_ratpart(p, q, x) 
    File "C:\Python27\lib\site-packages\sympy\integrals\rationaltools.py", line 124, in ratint_ratpart 
    H = f - A.diff()*v + A*(u.diff()*v).quo(u) - B*u 
    File "C:\Python27\lib\site-packages\sympy\core\decorators.py", line 75, in __sympifyit_wrapper 
    return func(a, sympify(b, strict=True)) 
    File "C:\Python27\lib\site-packages\sympy\polys\polytools.py", line 3360, in __mul__ 
    return f.mul(g) 
    File "C:\Python27\lib\site-packages\sympy\polys\polytools.py", line 1295, in mul 
    _, per, F, G = f._unify(g) 
    File "C:\Python27\lib\site-packages\sympy\polys\polytools.py", line 377, in _unify 
    F = f.rep.convert(dom) 
    File "C:\Python27\lib\site-packages\sympy\polys\polyclasses.py", line 277, in convert 
    return DMP(dmp_convert(f.rep, f.lev, f.dom, dom), dom, f.lev) 
    File "C:\Python27\lib\site-packages\sympy\polys\densebasic.py", line 530, in dmp_convert 
    return dup_convert(f, K0, K1) 
    File "C:\Python27\lib\site-packages\sympy\polys\densebasic.py", line 506, in dup_convert 
    return dup_strip([ K1.convert(c, K0) for c in f ]) 
    File "C:\Python27\lib\site-packages\sympy\polys\domains\domain.py", line 85, in convert 
    raise CoercionFailed("can't convert %s of type %s to %s" % (a, K0, K1)) 
sympy.polys.polyerrors.CoercionFailed: can't convert DMP([1, 0], ZZ) of type ZZ[_b1] to RR 
[Finished in 0.3s with exit code 1] 

編輯: 好了,所以我拿出的數字,該方案使用,並把它在Wolfram Alpha的。原來,積分不收斂,因此錯誤。我想這只是一個數學錯誤。

回答

5

使用符號庫進行數值工作是一個非常糟糕的主意。只需使用scipy/numpy。這就是說,對於這樣一個簡單的積分,你可以使用sympy。但是,您應該實際使用sympy表達式,而不是在不透明函數中轉儲所有內容。

首先,學習如何在Python工作要做變量:

ig = Symbol('ig') 
ig = flux(x, I, r) 

後此操作ig不是一個符號,就說明它只是flux返回值。

定義你所有的符號,然後用它們表達出來。積分對於sympy處理它非常簡單。

最後,像你的情況一樣簡單的const*x/(x-const)應該手工完成,而不是浪費在軟件上。

[編輯]:我已經徹底改寫它,仍然sympy由於錯誤沒有正確集成。你可以在郵件列表或問題追蹤器上報告,他們會嘗試糾正它。話雖如此,表達如此簡單,以至於可以通過手工整合。

[EDIT2]:

In [5]: integrate(a*x/(b*x+c), x) 
Out[5]: 

    ⎛   ⎛ 2  ⎞⎞ 
    ⎜x c⋅log⎝b ⋅x + b⋅c⎠⎟ 
a⋅⎜─ - ─────────────────⎟ 
    ⎜b   2  ⎟ 
    ⎝   b  ⎠