如何在Python中设置以下具有相应初始条件的ode?
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
以下是我目前所掌握的内容:
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 )
我猜你正在尝试用 odeint 来解决它们。首先,我假设您在脚本中使用了这个前奏:
import numpy as np
from scipy.integrate import odeint
你的等式是:
def equation(X, t):
x, y = X
return [ x+y-np.exp(t), x+y+2*np.exp(t) ]
然后你可以用以下方法解决它们
init = [ -1.0, -1.0 ]
t = np.linpsace(0, 4, 50)
X = odeint(equation, init, t)
您可以使用以下方法提取 x(t) 和 y(t)
x = X[:, 0]
y = X[:, 1]
我是一名优秀的程序员,十分优秀!