用scipy在python中求解两个二维微分方程 [英] solving two dimension-differential equations in python with scipy

查看:419
本文介绍了用scipy在python中求解两个二维微分方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手.我有一个简单的微分系统,它由两个变量和两个微分方程和初始条件x0=1, y0=2:

i am a newbie to python. I have a simple differential systems, which consists of two variables and two differential equations and initial conditions x0=1, y0=2:

dx/dt=6*y
dy/dt=(2t-3x)/4y

现在我正在尝试求解这两个微分方程,因此我选择odeint.这是我的代码:

now i am trying to solve these two differential equations and i choose odeint. Here is my code:

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

但是结果不正确,并且出现错误消息:

but the result is incorrect and there is a error message:

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

我不知道我的代码有什么问题以及如何解决. 任何帮助对我都是有用的.

I don't know what is wrong with my code and how can i solve it. Any help will be a useful to me.

推荐答案

应用技巧将y除以除数,打印所有ODE函数求值,绘制两个分量,并在修改后的代码中使用正确的微分方程

Apply trick to desingularize the division by y, print all ODE function evaluations, plot both components, and use the right differential equation with the modified code

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的斜率达到无穷大,因此无法克服这种奇异之处.此时,解不再是连续可微的,因此这是解的极点.常数连续是去奇化的产物,不是有效的解决方案.

and you see that at t=0.64230232515 the singularity of y=0 is assumed, where y behaves like a square root function at its apex. There is no way to cross that singularity, as the slope of y goes to infinity. At this point, the solution is no longer continuously differentiable, and thus this is the extremal point of the solution. The constant continuation is an artifact of the desingularization, not a valid solution.

这篇关于用scipy在python中求解两个二维微分方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆