【发布时间】:2015-08-21 15:48:36
【问题描述】:
我想使用 Python 求解非线性一阶微分方程。
例如,
df/dt = f**4
我写了下面的程序,但是matplotlib有问题,所以不知道我用scipy的方法是否正确。
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
derivate=lambda f,t: f**4
f0=10
t=np.linspace(0,2,100)
f_numeric=scipy.integrate.odeint(derivate,f0,t)
print(f_numeric)
plt.plot(t,f_numeric)
plt.show()
这会导致以下错误:
AttributeError: 'float' object has no attribute 'rint'
【问题讨论】:
-
试试
t=np.linspace(0,0.00033,1000) -
这很奇怪,因为当我将
scipy.integrate.odeint更改为odeint时,上面的代码对我来说工作正常(因为您实际上没有导入 scipy 命名空间,所以应该只按其名称调用 odeint 函数) -
@HYRY :我试过
t=np.linspace(0,0.00033,1000),它适用于这个t,我怎样才能让它适用于t=np.linspace(0,2,100)? @Azrathud:它符合您的建议......但并非一直如此。实际上,如果你尝试我的代码 10 次,它不会工作 10 次:会有AttributeError。你有其他建议让它发挥作用吗? -
@Jack,请查看符号解决方案:wolframalpha.com/input/?i=df%2Fdt%20%3D%20f**4
-
@HYRY :我将您的链接与
df/dt = f^4, f(0)=10一起使用,WolframAlpha 给出了f(x) = 10 / (1 - 3000x)^(1 / 3)作为解决方案。然后我在 Geogebra 上绘制了解决方案,它与我运行代码后绘制的结果不一样。
标签: python math numpy matplotlib scipy