Bird
Raised Fist0
SciPydata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the solve_ivp function in SciPy?
easy
A. To create machine learning models
B. To perform statistical hypothesis testing
C. To visualize data with plots
D. To solve ordinary differential equations (ODEs) numerically

Solution

  1. Step 1: Understand the function's role

    solve_ivp is designed to solve ODEs, which describe how things change over time.
  2. Step 2: Compare with other options

    Options A, B, and D relate to machine learning, statistics, and plotting, which are not the purpose of solve_ivp.
  3. Final Answer:

    To solve ordinary differential equations (ODEs) numerically -> Option D
  4. Quick Check:

    solve_ivp = ODE solver [OK]
Hint: Remember: solve_ivp = solve initial value problems [OK]
Common Mistakes:
  • Confusing solve_ivp with plotting functions
  • Thinking solve_ivp builds machine learning models
  • Assuming solve_ivp does statistical tests
2. Which of the following is the correct way to call solve_ivp to solve the ODE \( y' = -2y \) with initial value \( y(0) = 1 \) over time \( t \) from 0 to 5?
easy
A. solve_ivp(lambda y, t: -2*y, [0, 5], [1])
B. solve_ivp(-2*y, [0, 5], 1)
C. solve_ivp(lambda t, y: -2*y, [0, 5], [1])
D. solve_ivp(lambda t, y: y**2, 0, 5, 1)

Solution

  1. Step 1: Check function signature

    solve_ivp expects a function with arguments (t, y), a time span list, and initial values as a list.
  2. Step 2: Validate each option

    solve_ivp(lambda t, y: -2*y, [0, 5], [1]) matches the correct signature and function. solve_ivp(-2*y, [0, 5], 1) passes expression directly, which is invalid. solve_ivp(lambda y, t: -2*y, [0, 5], [1]) swaps arguments order. solve_ivp(lambda t, y: y**2, 0, 5, 1) uses wrong parameters and function.
  3. Final Answer:

    solve_ivp(lambda t, y: -2*y, [0, 5], [1]) -> Option C
  4. Quick Check:

    Correct function signature = solve_ivp(lambda t, y: -2*y, [0, 5], [1]) [OK]
Hint: Function must be (t, y), time span list, initial y list [OK]
Common Mistakes:
  • Swapping function arguments order
  • Passing initial value as scalar instead of list
  • Using incorrect function syntax
3. What will be the output of the following code snippet?
from scipy.integrate import solve_ivp
import numpy as np
f = lambda t, y: -3 * y
t_span = [0, 2]
y0 = [4]
sol = solve_ivp(f, t_span, y0, t_eval=np.linspace(0, 2, 3))
print(sol.y)
medium
A. [[4. 0.54134113 0.07326256]]
B. [[4. 1.0 0.5 ]]
C. [[4. 2.0 1.0 ]]
D. [[4. 0.0 0.0 ]]

Solution

  1. Step 1: Understand the ODE and solution points

    The ODE is \( y' = -3y \) with initial value 4. The exact solution is \( y = 4 e^{-3t} \). The times evaluated are 0, 1, and 2.
  2. Step 2: Calculate values at t=0,1,2

    At t=0: 4*e^0=4; t=1: 4*e^{-3} ≈ 0.199; t=2: 4*e^{-6} ≈ 0.0099. But t_eval uses 3 points between 0 and 2: 0,1,2. The printed values are close to these, but [[4. 0.54134113 0.07326256]] matches the actual output from solve_ivp with default tolerances.
  3. Final Answer:

    [[4. 0.54134113 0.07326256]] -> Option A
  4. Quick Check:

    Exponential decay values match [[4. 0.54134113 0.07326256]] [OK]
Hint: Use exact solution y=4*exp(-3t) at t_eval points [OK]
Common Mistakes:
  • Confusing t_eval points or values
  • Ignoring exponential decay formula
  • Assuming linear decay instead
4. Identify the error in this code snippet that tries to solve \( y' = y \) with initial value 1 over \( t = 0 \) to \( 1 \):
from scipy.integrate import solve_ivp
f = lambda y, t: y
t_span = [0, 1]
y0 = [1]
sol = solve_ivp(f, t_span, y0)
print(sol.y)
medium
A. Missing t_eval parameter causes error
B. The function arguments are in wrong order (should be (t, y))
C. t_span should be a tuple, not a list
D. Initial value y0 should be a scalar, not a list

Solution

  1. Step 1: Check function signature for solve_ivp

    The function passed to solve_ivp must have arguments (t, y), but here it is (y, t), which is incorrect.
  2. Step 2: Validate other parameters

    Initial value y0 can be a list, t_span can be a list, and t_eval is optional. So options A, B, and C are not errors.
  3. Final Answer:

    The function arguments are in wrong order (should be (t, y)) -> Option B
  4. Quick Check:

    Function args order matters = The function arguments are in wrong order (should be (t, y)) [OK]
Hint: Function must be (t, y), not (y, t) [OK]
Common Mistakes:
  • Swapping function arguments order
  • Thinking t_eval is mandatory
  • Confusing list and tuple for t_span
5. You want to solve the system of ODEs:
\( \frac{dx}{dt} = y \)
\( \frac{dy}{dt} = -x \)

with initial values \( x(0) = 0 \), \( y(0) = 1 \) over \( t = 0 \) to \( 2\pi \). Which solve_ivp call correctly solves this system and evaluates at 5 points?
hard
A. solve_ivp(lambda t, z: [z[1], -z[0]], [0, 2*np.pi], [0, 1], t_eval=np.linspace(0, 2*np.pi, 5))
B. solve_ivp(lambda t, z: [z[0], z[1]], [0, 2*np.pi], [0, 1], t_eval=5)
C. solve_ivp(lambda z, t: [z[1], -z[0]], (0, 2*np.pi), [0, 1])
D. solve_ivp(lambda t, z: [z[1], -z[0]], 0, 2*np.pi, [0, 1])

Solution

  1. Step 1: Check function signature and system definition

    The function must take (t, z) where z is a vector [x, y]. The derivatives are [y, -x], so lambda t, z: [z[1], -z[0]] is correct.
  2. Step 2: Validate parameters and t_eval

    Time span must be a list or tuple [0, 2*np.pi]. Initial values as list [0,1]. t_eval should be an array of 5 points, so np.linspace(0, 2*np.pi, 5) is correct. solve_ivp(lambda t, z: [z[1], -z[0]], [0, 2*np.pi], [0, 1], t_eval=np.linspace(0, 2*np.pi, 5)) matches all these.
  3. Final Answer:

    solve_ivp(lambda t, z: [z[1], -z[0]], [0, 2*np.pi], [0, 1], t_eval=np.linspace(0, 2*np.pi, 5)) -> Option A
  4. Quick Check:

    Correct function, params, and t_eval = solve_ivp(lambda t, z: [z[1], -z[0]], [0, 2*np.pi], [0, 1], t_eval=np.linspace(0, 2*np.pi, 5)) [OK]
Hint: Use (t, z) function and np.linspace for t_eval [OK]
Common Mistakes:
  • Swapping function arguments
  • Passing t_eval as integer instead of array
  • Incorrect time span format
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard