2014-02-13 35 views
1

代碼,請參閱代碼片斷片斷1:sympy中的積分對於x * cos(n * pi * x/L)不起作用,但對sqrt(1。)* x * cos(n * pi * x/L)有效。下面

from sympy import symbols, integrate, cos, pi 
from numpy import sqrt 
n = symbols('n', integer=True) 
x, L = symbols('x L', real=True) 
fs_coeff = integrate(sqrt(1.)*x*cos(n*pi*x/L), (x, 0, L)) 
print fs_coeff 

我也得到:

-1.0 *分段((0,N == 0),(0.101321183642338 * L * 2/N * 2,True))+ 1.0 *分段((L ** 2/2,n == 0),(0.318309886183791 * L ** 2 * sin(3.14159265358979 * n)/ n + 0.101321183642338 * L ** 2 * cos (3.14159265358979 * N)/ N ** 2,TRUE))

代碼段2:

from sympy import symbols, integrate, cos, pi 
from numpy import sqrt 
n = symbols('n', integer=True) 
x, L = symbols('x L', real=True) 
fs_coeff = integrate(x*cos(n*pi*x/L), (x, 0, L)) 
print fs_coeff 

我得到一個錯誤:

回溯(最近通話最後一個):

文件 「test-sympy.py」,第6行,在

fs_coeff =整合(X * COS( N * PI * X/L),(X,0,1))

...

ValueError異常:值過多解壓

我使用的拉泰什t Enthought Canopy python發行版,1.3版。 Python版本2.7.6,SymPy 0.7.3。

如果您對此有任何意見,我將不勝感激。

回答

2

SymPy 0.7.3不是最新版本。在0.7.4.1中嘗試。這是一個已修復的錯誤。

>>> fs_coeff = integrate(x*cos(n*pi*x/L), (x, 0, L)) 
>>> fs_coeff 
Piecewise((L**2/2, n == 0), ((-1)**n*L**2/(pi**2*n**2) - L**2/(pi**2*n**2), True)) 
+0

是的,這工作!謝謝! – user3304293

0

這個問題似乎是在處理n=0的分段間隔時出現的,當使用精確數學時該分段間隔會中斷,但不會與近似數學一起使用。 sqrt沒有必要,你可以用1.*x*cos(n*pi*x/L)得到相同的結果。因爲它是不太可能你想要n=0,您可以通過限制n來爲正得到一個乾淨的答案:

from sympy import symbols, integrate, cos, pi 
from numpy import sqrt 
n = symbols('n', integer=True, positive=True) 
x, L = symbols('x L', real=True) 
fs_coeff = integrate(x*cos(n*pi*x/L), (x, 0, L)) 
print fs_coeff 
+0

感謝您所寫的有關精確數學與近似數學的內容。 – user3304293

相關問題