Python 中的拉普拉斯逆与 mpmath

Laplace inverse in Python with mpmath

我想用"DE HOOG"算法进行数值拉普拉斯逆变换。我想使用 "mpmath" 包,我从 link:

安装了它

https://github.com/klkuhlm/mpmath

假设我需要在 t=1 处找到以下函数的拉普拉斯逆变换:

f = 1/(s-1)

f的拉普拉斯逆变换为:e^(t)

在 t=1 时,结果预计为 = e

import mpmath as mp
import numpy as np

def f(s):
    return 1 / (s-1)

t = np.linspace(0.01,0.5,10)

G = []

for i in range(0,4):
    G.append(mp.invlapdehoog(f, t[i]))

print G 

只有当我将 "i" 的范围设置为小于 4 时,它才能完美运行。例如,一旦我替换为:

for i in range(0,5): #or for i in range(0,more than 5):

我收到这个错误:

enter image description here

你能帮我解决这个问题吗?

谢谢!

对象 InverseLaplaceTransform 有一个属性 degrees,它规定了达到给定精度级别所需的近似级别。每次您用越来越小的值调用它时,您的 InverseLaplaceTransform 副本都会更新 degrees。最终,degrees小到参数fp只有一个值,不足以继续进一步计算。

解决方案:编辑对 invlapdehoog 的调用,以便每次都重置度数。但是我建议直接调用 invertlaplace 而不是 invlapdehoog.

for i in xrange(0,10):
    G.append(mp.invertlaplace(f, t[i], method = 'dehoog', degree = 18))

编辑: 原发布者在对该解决方案的评论中提出了相关问题。他们问为什么连续调用 mp.invertlaplace 计算时间会增加(非常剧烈)。简而言之,mp.invertlaplace 正在更新其属性精度,该精度指示在计算逆拉普拉斯时应计算多少小数位。与上述解决方案一样,我们可以将精度传递给每个调用以确保我们获得我们想要的精度(例如 - 10 位小数):

for i in xrange(0,10):
    G.append(mp.invertlaplace(f, t[i], method = 'dehoog', dps = 10, degree = 18))

PS - 您可以使用以下代码段立即将逆拉普拉斯应用于所有 t:

G = map( lambda x: mp.invertlaplace(f, x, method = 'dehoog', dps = 10, degree = 18), t)