首页 文章

用scipy求解python中的二维微分方程

提问于
浏览
1

我是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()

但结果不正确,并出现错误信息:

enter image description here

Excess work done on this call (perhaps wrong Dfun type).
Run with full_output = 1 to get quantitative information.

我不知道我的代码有什么问题,我该如何解决它 . 任何帮助对我都有用 .

1 回答

  • 4

    通过 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 的斜率变为无穷大 . 此时,解决方案不再是连续可微的,因此这是解决方案的极 endpoints . 恒定的延续是去除化的一个人为因素,而不是一个有效的解决方案 .

    graph of x and y over t

相关问题