使用solve_ivp代替odeint来解决初始问题值

时间:2018-11-21 15:26:37

标签: python scipy ode differential-equations

当前,我使用odeint求解以下ODE方程组

dx / dt =(-x + u)/2.0

dy / dt =(-y + x)/5.0

初始条件:x = 0,y = 0

但是,我想使用resolve_ivp,它似乎是针对此类问题的推荐选项,但老实说,我不知道如何修改代码...

这是我与odeint一起使用的代码:

import numpy as np
from scipy.integrate import odeint, solve_ivp
import matplotlib.pyplot as plt

def model(z, t, u):
    x = z[0]
    y = z[1]
    dxdt = (-x + u)/2.0
    dydt = (-y + x)/5.0
    dzdt = [dxdt, dydt]
    return dzdt

def main():
    # initial condition
    z0 = [0, 0]

    # number of time points
    n = 401

    # time points
    t = np.linspace(0, 40, n)

    # step input
    u = np.zeros(n)
    # change to 2.0 at time = 5.0
    u[51:] = 2.0

    # store solution
    x = np.empty_like(t)
    y = np.empty_like(t)
    # record initial conditions
    x[0] = z0[0]
    y[0] = z0[1]

    # solve ODE
    for i in range(1, n):
        # span for next time step
        tspan = [t[i-1], t[i]]
        # solve for next step
        z = odeint(model, z0, tspan, args=(u[i],))
        # store solution for plotting
        x[i] = z[1][0]
        y[i] = z[1][1]
        # next initial condition
        z0 = z[1]

    # plot results
    plt.plot(t,u,'g:',label='u(t)')
    plt.plot(t,x,'b-',label='x(t)')
    plt.plot(t,y,'r--',label='y(t)')
    plt.ylabel('values')
    plt.xlabel('time')
    plt.legend(loc='best')
    plt.show()

main()

1 个答案:

答案 0 :(得分:0)

solve_ivp期望f(t,z)作为ODE的右侧很重要。如果您不想更改ode函数,也不想传递参数u,建议您定义一个包装函数:

def model(z, t, u):
    x = z[0]
    y = z[1]
    dxdt = (-x + u)/2.0
    dydt = (-y + x)/5.0
    dzdt = [dxdt, dydt]
    return dzdt

def odefun(t, z):
    if t < 5:
        return model(z, t, 0)
    else:
        return model(z, t, 2)

现在很容易致电solve_ivp

def main():
    # initial condition
    z0 = [0, 0]

    # number of time points
    n = 401

    # time points
    t = np.linspace(0, 40, n)

    # step input
    u = np.zeros(n)
    # change to 2.0 at time = 5.0
    u[51:] = 2.0

    res = solve_ivp(fun=odefun, t_span=[0, 40], y0=z0, t_eval=t)
    x = res.y[0, :]
    y = res.y[1, :]

    # plot results
    plt.plot(t,u,'g:',label='u(t)')
    plt.plot(t,x,'b-',label='x(t)')
    plt.plot(t,y,'r--',label='y(t)')
    plt.ylabel('values')
    plt.xlabel('time')
    plt.legend(loc='best')
    plt.show()

main()

请注意,求解器将在t_eval=t内自动选择存储解决方案的时间点,而无需通过tspan