0
0
SciPydata~10 mins

Solving ODEs (solve_ivp) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Solving ODEs (solve_ivp)
Define ODE function f(t, y)
Set initial conditions y0 and time span
Call solve_ivp with f, time span, y0
solve_ivp integrates step-by-step
Collect solution times and values
Use or plot solution results
We define the ODE, set initial values and time range, then call solve_ivp which calculates the solution step-by-step and returns the results.
Execution Sample
SciPy
from scipy.integrate import solve_ivp

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

sol = solve_ivp(dydt, [0, 5], [1])
This code solves the simple ODE dy/dt = -2y from time 0 to 5 with initial value y=1.
Execution Table
Stept (time)y (value)ActionNotes
10.0[1.0]Start integrationInitial condition y(0)=1
20.1[0.8187]Compute next yy(0.1) approx e^(-2*0.1) = 0.8187
30.2[0.6703]Compute next yy(0.2) approx e^(-2*0.2) = 0.6703
40.3[0.5488]Compute next yy(0.3) approx e^(-2*0.3) = 0.5488
50.4[0.4493]Compute next yy(0.4) approx e^(-2*0.4) = 0.4493
60.5[0.3679]Compute next yy(0.5) approx e^(-2*0.5) = 0.3679
75.0[0.0000454]Final stepy(5) approx e^(-2*5) = 4.54e-05
💡 Reached end of time span at t=5.0, integration complete.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
t0.00.10.20.30.40.55.0
y[1.0][0.8187][0.6703][0.5488][0.4493][0.3679][0.0000454]
Key Moments - 2 Insights
Why does the time t jump from 0.5 to 5.0 in the last step?
solve_ivp adapts step sizes for accuracy. It takes small steps at first, then jumps to the final time 5.0 to finish integration, as shown in the last row of the execution_table.
Why is y value decreasing at each step?
Because the ODE is dy/dt = -2y, which means y decreases exponentially over time. The execution_table rows show y values getting smaller as t increases.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3. What is the approximate value of y?
A[0.8187]
B[0.5488]
C[0.6703]
D[0.3679]
💡 Hint
Check the 'y (value)' column at Step 3 in the execution_table.
At which step does the integration reach the final time t=5.0?
AStep 6
BStep 7
CStep 5
DStep 4
💡 Hint
Look at the 't (time)' column in the execution_table for the largest time value.
If the initial value y(0) was 2 instead of 1, how would y at Step 2 change?
AIt would be about 1.6374
BIt would stay 0.8187
CIt would be about 0.3351
DIt would be about 2.0
💡 Hint
Since y scales with initial value, double initial y doubles y at each step (see variable_tracker).
Concept Snapshot
solve_ivp(func, t_span, y0) solves ODE dy/dt=func(t,y)
- func(t,y) returns derivative
- t_span = [start, end] times
- y0 = initial value(s)
- Returns solution with times and y values
- Uses adaptive steps for accuracy
Full Transcript
We start by defining the ODE function that returns the derivative at any time and value. Then we set initial conditions and the time range to solve over. Calling solve_ivp runs the integration step-by-step, adapting step sizes for accuracy. The solution includes times and corresponding y values. For example, solving dy/dt = -2y from 0 to 5 with y(0)=1 shows y decreasing exponentially. The execution table traces each step's time and y value, showing how the solution progresses until the final time is reached.