简体   繁体   中英

An implementation of solve_ivp (ODE solver)

This is my first post on stack overflow. So I came across this example of solve_ivp solver in SciPy's documentation. The problem being solved is the following:

Cannon fired upward with terminal event upon impact. The terminal and direction fields of an event are applied by monkey patching a function. Here y[0] is position and y[1] is velocity. The projectile starts at position 0 with velocity +10. Note that the integration never reaches t=100 because the event is terminal.

The code in the documentation is the following:

>>> def upward_cannon(t, y): return [y[1], -0.5]
>>> def hit_ground(t, y): return y[0]
>>> hit_ground.terminal = True
>>> hit_ground.direction = -1
>>> sol = solve_ivp(upward_cannon, [0, 100], [0, 10], events=hit_ground)
>>> print(sol.t_events)
[array([40.])]
>>> print(sol.t)
[0.00000000e+00 9.99900010e-05 1.09989001e-03 1.10988901e-02
 1.11088891e-01 1.11098890e+00 1.11099890e+01 4.00000000e+01]

I have used this solver for solving other differential equations. Further, I understand the usage terminal and direction fields. But for this example only, I am not able to understand how the function upward_cannon() is working by using return [y[1],-0.5] .The output corresponding to print(sol.y) is the following:


[[ 0.00000000e+00  9.99897510e-04  1.09985977e-02  1.10958105e-01
   1.10780373e+00  1.08013149e+01  8.02419261e+01 -1.42108547e-14]
 [ 1.00000000e+01  9.99995000e+00  9.99945005e+00  9.99445055e+00
   9.94445555e+00  9.44450555e+00  4.44500550e+00 -1.00000000e+01]]

Since there is no underlying differential equation mentioned in the code, how is the solver producing the above values for y ? I know return [y[1],-0.5] is doing something but I am not able to explain what it is doing.

You wrote

Since there is no underlying differential equation mentioned in the code...

It is not explicitly mentioned, but in fact, there is a differential equation. Let h(t) be the height of the cannonball. The differential equation is

h''(t) = -0.5

That is, the acceleration is constant and in the downward direction. This is Newton's second law , with the assumption of constant gravitational force. The example assumes that the ratio of the gravitational constant and the mass is 0.5.

The initial conditions are

h(0) = 0,  h'(0) = 10

The equation h''(t) = -0.5 is a (trivial!) second order differential equation. We could solve it with plain old definite integration, but for the sake of the example, we use an ODE solver. To use solve_ivp (or any of the other ODE solvers in SciPy), we must convert the second order DE to a system of first order equations. Let y0(t) = h(t) and y1(t) = h'(t). Then

y0'(t) = h'(t) = y1(t)
y1'(t) = h''(t) = -0.5

That is the system that is implemented in

def upward_cannon(t, y):
    return [y[1], -0.5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM