python - 使用 scipy.integrate.odeint 求解微分方程 : "Result from function call is not a proper array of floats."

标签 python scipy differential-equations nonlinear-functions

我正在绘制这个非线性一阶微分方程的图形:

dv/dt + 3 v**2 + 2 = 0

这是代码:

from scipy.integrate import odeint

from matplotlib import pyplot as plt

import numpy as np

def diff(v, t):

      return [v, -3*v*v - 2]

t = np.arange(-1,1,0.01)

v0 = 1

f = odeint(diff, v0, t)

plt.figure(figsize=(5,5))

plt.plot(t, f)

plt.show()

但是,这不起作用:

odepack.error: Result from function call is not a proper array of floats.

最佳答案

odeint routine期望第一个参数仅计算导数:

func : callable(y, t0, …)
Computes the derivative of y at t0.

但是,您的函数 diff 返回一个 2 元素列表除了导数之外还包含解:

def diff(v, t):

      return [v, -3*v*v - 2]

要解决此问题,您只需从返回值中删除 v 并避免将其包装在列表中:

def diff(v, t):

      return -3*v*v - 2

关于python - 使用 scipy.integrate.odeint 求解微分方程 : "Result from function call is not a proper array of floats.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494281/

相关文章:

Python 绘制时间序列并强调其中的一部分

python - Tensorflow "Attempting to use uninitialized value ..."恢复时出错

Python:Plotly:平行坐标不透明度

python - 尽管 x0 在范围内,Scipy 优化仍会引发 ValueError

c# - 使用 C# 求解偏微分方程

javascript - 为不同的网络屏幕尺寸加载不同的图像

python - pickle Boost.Python 公开的枚举

python - Pandas 在由列表组成的元素上删除重复项

python - 带有对数频率轴的 scipy 频谱图?

Julia 微分方程抑制检测到的不稳定性警告