0
0
SciPydata~10 mins

ODE solver methods (RK45, BDF) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ODE solver methods (RK45, BDF)
Define ODE function
Choose solver method: RK45 or BDF
Set initial conditions and time span
Run solver step-by-step
Check solver status
Collect solution points
Plot or analyze results
The flow starts by defining the ODE, choosing a solver method (RK45 or BDF), setting initial conditions, running the solver stepwise, checking status, and collecting results.
Execution Sample
SciPy
from scipy.integrate import solve_ivp

def ode(t, y):
    return -2 * y

sol = solve_ivp(ode, [0, 5], [1], method='RK45')
This code solves the ODE dy/dt = -2y from t=0 to t=5 with initial y=1 using the RK45 method.
Execution Table
Stept (time)y (solution)MethodActionStatus
10.0[1.0]RK45Start integrationRunning
20.1[0.8187]RK45Compute next stepRunning
30.2[0.6703]RK45Compute next stepRunning
40.3[0.5488]RK45Compute next stepRunning
50.4[0.4493]RK45Compute next stepRunning
60.5[0.3679]RK45Compute next stepRunning
75.0[4.54e-05]RK45Final step reachedSuccess
💡 Integration ends when t reaches 5.0, the end of the time span.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
t0.00.10.20.30.40.55.05.0
y[1.0][0.8187][0.6703][0.5488][0.4493][0.3679][4.54e-05][4.54e-05]
methodRK45RK45RK45RK45RK45RK45RK45RK45
Key Moments - 2 Insights
Why does the solver take smaller steps at the start and then a big jump to the final time?
The solver adapts step size to keep accuracy. Early steps are small to capture rapid changes; later it can jump to the final time if error is low. See execution_table rows 2-6 for small steps and row 7 for the big jump.
What's the difference between RK45 and BDF methods in solving ODEs?
RK45 is an explicit method good for smooth problems; BDF is implicit and better for stiff problems. Both solve step-by-step but handle stability differently. This example uses RK45, but switching method='BDF' changes solver behavior.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the approximate value of y at step 4?
A[0.5488]
B[0.6703]
C[0.4493]
D[0.3679]
💡 Hint
Check the 'y (solution)' column at step 4 in the execution_table.
At which step does the solver reach the final time t=5.0?
AStep 5
BStep 7
CStep 6
DStep 4
💡 Hint
Look at the 't (time)' column in execution_table for the value 5.0.
If we change the method to 'BDF', what would most likely happen to the step sizes?
AThey become larger and less adaptive
BThey stay exactly the same as RK45
CThey become smaller and more stable for stiff problems
DThe solver stops immediately
💡 Hint
Refer to key_moments about method differences and solver stability.
Concept Snapshot
Solve ODEs with solve_ivp:
- Define function ode(t, y)
- Choose method='RK45' (explicit) or 'BDF' (implicit)
- Set time span and initial y
- Run solver stepwise
- Adaptive step sizes for accuracy
- RK45 good for smooth, BDF for stiff problems
Full Transcript
This visual execution shows how to solve an ordinary differential equation (ODE) using scipy's solve_ivp function with the RK45 method. We start by defining the ODE function dy/dt = -2y. The solver begins at time t=0 with initial value y=1. It takes small steps, computing y values at each step, adapting step size to keep accuracy. The solution y decreases exponentially as expected. The solver finishes when it reaches the final time t=5. The RK45 method is explicit and good for smooth problems. The BDF method, not shown here, is implicit and better for stiff problems. Step sizes and solver behavior differ between methods. This step-by-step trace helps beginners see how the solver progresses through time and updates solution values.