问题
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
andy(0)= -1
and0 <= 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