Customizing ivp in scipy, where to control each step size in Runge-Kutta method?

44 Views Asked by At

The question is related to ivp function in scipy https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html#scipy.integrate.solve_ivp

If one loads a Runge-Kutta method, from https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods the RK4 example, in each step h, there are s smaller steps calling f and solve the difference. My question is, take the below Python code for example (modified from https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html#scipy.integrate.solve_ivp) where to customize after each h?

import numpy as np

from scipy.integrate import solve_ivp

def exponential_decay(t, y): 
    print(t)
    return -0.5 * y

sol = solve_ivp(exponential_decay, [0, 3], [2, 4, 8], method = 'DOP853')

print(sol.t)


I will get

0.0
0.02
0.022975306924280958
0.03446296038642143
0.09190122769712383
0.10211247521902646
0.11487653462140478
0.11487653462140478
0.3446296038642143
0.4595061384856191
1.033888811592643
1.1360012868116693
1.2636418808354524
1.2636418808354524
1.6109135046683618
1.7845493165848167
2.6527283761670906
2.807071320092828
3.0
3.0
[0.         0.11487653 1.26364188 3.        ]

looks like [0. 0.11487653 1.26364188 3. ] is h and 0.0 0.02 0.022975306924280958... is t_n + c_s h.

If I want to do some additional setup after each h, (not each t_t + c_s h), e.g., print hello world at 0.11487653 1.26364188 3., how to do?

look for wikipedia and provide an example; expect e.g., print hello world after each $h$

0

There are 0 best solutions below