用 scipy 求解 python 中的二维微分方程
solving two dimension-differential equations in python with scipy
我是 python 的新手。我有一个简单的微分系统,它由两个变量和两个微分方程组成,初始条件为 x0=1, y0=2
:
dx/dt=6*y
dy/dt=(2t-3x)/4y
现在我正在尝试求解这两个微分方程,我选择 odeint
。这是我的代码:
import matplotlib.pyplot as pl
import numpy as np
from scipy.integrate import odeint
def func(z,b):
x, y=z
return [6*y, (b-3*x)/(4*y)]
z0=[1,2]
t = np.linspace(0,10,11)
b=2*t
xx=odeint(func, z0, b)
pl.figure(1)
pl.plot(t, xx[:,0])
pl.legend()
pl.show()
但结果不正确,出现错误信息:
Excess work done on this call (perhaps wrong Dfun type).
Run with full_output = 1 to get quantitative information.
我不知道我的代码有什么问题,我该如何解决。
任何帮助都会对我有用。
应用技巧对 y
的除法进行去奇异化,打印所有 ODE 函数评估,绘制两个分量,并使用修改后的代码
的右微分方程
import matplotlib.pyplot as pl
import numpy as np
from scipy.integrate import odeint
def func(z,t):
x, y=z
print t,z
return [6*y, (2*t-3*x)*y/(4*y**2+1e-12)]
z0=[1,2]
t = np.linspace(0,1,501)
xx=odeint(func, z0, t)
pl.figure(1)
pl.plot(t, xx[:,0],t,xx[:,1])
pl.legend()
pl.show()
并且您看到在 t=0.64230232515
假设 y=0
的奇点,其中 y
在其顶点处表现得像平方根函数。没有办法越过那个奇点,因为 y
的斜率趋于无穷大。此时解不再连续可微,因此这是解的极值点。持续延续是去奇异化的产物,不是有效的解决方案。
我是 python 的新手。我有一个简单的微分系统,它由两个变量和两个微分方程组成,初始条件为 x0=1, y0=2
:
dx/dt=6*y
dy/dt=(2t-3x)/4y
现在我正在尝试求解这两个微分方程,我选择 odeint
。这是我的代码:
import matplotlib.pyplot as pl
import numpy as np
from scipy.integrate import odeint
def func(z,b):
x, y=z
return [6*y, (b-3*x)/(4*y)]
z0=[1,2]
t = np.linspace(0,10,11)
b=2*t
xx=odeint(func, z0, b)
pl.figure(1)
pl.plot(t, xx[:,0])
pl.legend()
pl.show()
但结果不正确,出现错误信息:
Excess work done on this call (perhaps wrong Dfun type).
Run with full_output = 1 to get quantitative information.
我不知道我的代码有什么问题,我该如何解决。 任何帮助都会对我有用。
应用技巧对 y
的除法进行去奇异化,打印所有 ODE 函数评估,绘制两个分量,并使用修改后的代码
import matplotlib.pyplot as pl
import numpy as np
from scipy.integrate import odeint
def func(z,t):
x, y=z
print t,z
return [6*y, (2*t-3*x)*y/(4*y**2+1e-12)]
z0=[1,2]
t = np.linspace(0,1,501)
xx=odeint(func, z0, t)
pl.figure(1)
pl.plot(t, xx[:,0],t,xx[:,1])
pl.legend()
pl.show()
并且您看到在 t=0.64230232515
假设 y=0
的奇点,其中 y
在其顶点处表现得像平方根函数。没有办法越过那个奇点,因为 y
的斜率趋于无穷大。此时解不再连续可微,因此这是解的极值点。持续延续是去奇异化的产物,不是有效的解决方案。