setup odes in python

China☆狼群 提交于 2019-12-02 08:51:51

问题


how to setup the following odes with the corresponding initial conditions in python?

x'(t) =x(t) - y(t) - e^t

y'(t) =x(t) + y(t) + 2e^t

with x(0)= -1 and y(0)= -1 and 0 <= t <= 4

The following is what I have so far:

def f(u, t):
    x, y = u
    return [x+y-e**t, x+y+2*e**t]

x0, y0 = [-1.0,-1.0]
t = numpy.linspace( 0,4,50 )

回答1:


I guess you're trying to solve them with odeint. First I'm assuming you use this prelude in you script :

import numpy as np
from scipy.integrate import odeint

Your equation is :

def equation(X, t):
    x, y = X
    return [ x+y-np.exp(t), x+y+2*np.exp(t) ]

and then you can solve them with

init = [ -1.0, -1.0 ]
t = np.linpsace(0, 4, 50)
X = odeint(equation, init, t)

You can extract x(t) and y(t) with

x = X[:, 0]
y = X[:, 1]


来源:https://stackoverflow.com/questions/16747624/setup-odes-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!