参数没有传递给 Scipy solve_ivp

我已经从此处的solve_ivp文档中复制并粘贴了 Scipy 函数的 Lotka-Volterra 示例,但出现错误


lotkavolterra() missing 4 required positional arguments: 'a', 'b', 'c', and 'd'

在我看来,lotkavolterra尽管它是“带有附加参数的系统示例”,但参数并没有被传递给它。代码如下。


from scipy.integrate import solve_ivp


def lotkavolterra(t, z, a, b, c, d):

    x, y = z

    return [a*x - b*x*y, -c*y + d*x*y]


sol = solve_ivp(lotkavolterra, [0, 15], [10, 5], args=(1.5, 1, 3, 1), dense_output=True)


print(sol)

我该如何解决我的问题?


MMTTMM
浏览 105回答 1
1回答

婷婷同学_

如果您的 solve_ivp 版本不提供参数args,您可以使用 lambda 函数:sol = solve_ivp(lambda t, z: lotkavolterra(t, z, 1.5, 1, 3, 1), [0, 15], [10, 5], dense_output=True)但是,强烈建议更新您的 scipy 版本,就像 Warren 已经在评论中暗示的那样。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python