【问题标题】:Laplace inverse in Python with mpmathPython中的拉普拉斯逆与mpmath
【发布时间】:2016-11-23 22:06:41
【问题描述】:

我想使用“DE HOOG”算法进行数值拉普拉斯逆变换。我想使用“mpmath”包,我从链接安装了它:

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

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

谢谢!

【问题讨论】:

  • 你是如何安装mpmath的?我使用pip 安装它,但它无法识别invertlaplace 功能
  • 这个错误(以及其他几个错误)已得到修复,并且已与 mpmath 的主版本合并(参见版本 1.0)mpmath.org/doc/current/calculus/inverselaplace.html

标签: python python-2.7 mpmath


【解决方案1】:

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

解决方案:编辑您对invlapdehoog 的呼叫以每次重置度数。不过我建议直接调用invertlaplace 而不是invlapdehoog

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

编辑: 原始发帖人在 cmets 中针对此解决方案提出了一个相关问题。他们问为什么计算时间会随着对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 - 您可以使用以下 sn-p 一次将逆拉普拉斯应用于所有 t:

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

【讨论】:

  • 你说的解决了这个问题。这是另一个问题。 DE HOOG 方法是拉普拉斯逆变换的快速算法之一,但如果我将时间中的步数增加到 20 步:t = np.linspace(1,2,20) 那么代码永远不会停止运行.你可以试试。有没有办法解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 2016-11-13
相关资源
最近更新 更多