在声明时,除了一个变量外,所有变量都已定义,我试图找到表达式的laplace逆:
from numpy import *
import mpmath as mp
p0 = 1
E = 2
c= 3
L = 4
x = 2.5
t = linspace(1,5,10)
ulaplace = []
def U(s):
return(c*p0*(-exp(L*s/c) + exp(s*(L + 2*x)/c))*exp(-s*x/c)/(E*s**2*(exp(2*L*s/c) + 1)))
for ti in t:
ulaplace.append(mp.invertlaplace(U, ti, method='talbot'))但是我发现了一个错误:
Traceback (most recent call last):
File "D:\TEMP\IDLEscripts\CompareAnalyticalSolutions2.py", line 46, in <module>
ulaplace.append(mp.invertlaplace(U, ti, method='talbot'))
File "C:\Python35\lib\site-packages\mpmath\calculus\inverselaplace.py", line 805, in invertlaplace
fp = [f(p) for p in rule.p]
File "C:\Python35\lib\site-packages\mpmath\calculus\inverselaplace.py", line 805, in <listcomp>
fp = [f(p) for p in rule.p]
File "D:\TEMP\IDLEscripts\CompareAnalyticalSolutions2.py", line 43, in U
return(c*p0*(-exp(L*s/c) + exp(s*(L + 2*x)/c))*exp(-s*x/c)/(E*s**2*(exp(2*L*s/c) + 1)))
TypeError: attribute of type 'int' is not callable我还尝试了doc网站建议的doc网站格式,但仍然得到了相同的错误。
mpmath.invertlaplace函数是否要求所有内容都在定义时间的数值条件下?我之所以这么问,是因为这样做是可行的:
>>> import mpmath as mp
>>> def F(s):
return 1/s
>>> mp.invertlaplace(F,5, method = 'talbot')
mpf('1.0')如果是这样的话,我需要能够避开这一切。对我来说,重点是围绕其他变量,看看它们如何影响逆拉普拉斯。此外,人们可能会认为,函数在传递到mpmath之前会得到评估。
如果没有,那么这里到底发生了什么?
发布于 2017-08-04 08:02:23
好了,我明白了。基本上,mp.invertlaplace本身需要的函数只使用mpmath定义的函数。在原问题中提供的代码中,我使用的是来自exp库的numpy。所以exp(x)真的是numpy.exp(x)。为了使代码工作,它需要调用mpmath.exp函数,如下所示:
def U(s):
return -p0*mp.exp(s*x/c)/(E*s*(-s*mp.exp(L*s/c)/c - s*mp.exp(-L*s/c)/c)) + p0*mp.exp(-s*x/c)/(E*s*(-s*mp.exp(L*s/c)/c - s*mp.exp(-L*s/c)/c))我还没有在我在原问题中提供的简化示例上测试上述内容,因为它是更通用脚本的子集。然而,它应该有效,这似乎是问题的根源。
https://stackoverflow.com/questions/45500397
复制相似问题