odeint或solve_ivp中的初始条件

时间:2020-05-17 15:15:33

标签: matplotlib odeint

我是matplotlib / python的新手。我想在某个时间间隔内用odeint或resolve_ivp解决一些简单的ode,例如segment [0,10],其中“初始条件”在t_0 = 5而不是t_0 =0。在我看来odeint和Solve_ivp都将初始条件作为值y_0,即y_0是指定时间数组t开头的函数值。

在积分间隔的中间条件下,我该如何求解一个ode?

1 个答案:

答案 0 :(得分:0)

假设您具有以下ODE:地球引力场中的物体

 ''' x = distance from the sea level in meters
     R = the radius of earth in meters
     g = the gravitational constant at seal level in meters/seconds^2
     v = velocity in meters/s
 '''

v * dv / dx = -g * R ^ 2 /(R + x)^ 2

从一个高30 m的建筑物中以20 m / s的速度扔一个球。 因此非零边界条件为:

v(30)= 20

要绘制此解决方案,请在x坐标中移动值,使其始于建筑物的高度30米。不是0米,代表地面。

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

def model(v, x): 

    #constants
    g = 9.80665 #gravitational force at sea level
    R = 6.371e6 #radius of earth

    gamma = - g*(R/(R+x))**2

    dvdx = 1/v*gamma
    return dvdx

#Boundary conditions
v_30 = 20 # Velocity is 20 m/s at x = 30m 

#time points
x =  np.linspace(30, 55) # Start at 30 and not 0 

#solve ODE
v_x =  odeint(model, v_30, x)

#plot results
plt.plot(x, v_x)