Bird
Raised Fist0
SciPydata~20 mins

ODE solver methods (RK45, BDF) in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ODE Solver Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of RK45 solver on a simple ODE
What is the approximate value of y(2) when solving dy/dt = -2y with y(0) = 1 using RK45 method?
SciPy
from scipy.integrate import solve_ivp

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

sol = solve_ivp(fun, [0, 2], [1], method='RK45')
result = sol.y[0, -1]
print(round(result, 3))
A0.135
B0.368
C0.018
D0.500
Attempts:
2 left
💡 Hint
Recall the exact solution is y(t) = exp(-2t). RK45 approximates this numerically.
❓ Predict Output
intermediate
2:00remaining
Output difference between RK45 and BDF on stiff ODE
Given the stiff ODE dy/dt = -1000y + 3000 - 2000 exp(-t), y(0)=0, what is the approximate value of y(0.1) using BDF method?
SciPy
import numpy as np
from scipy.integrate import solve_ivp

def stiff(t, y):
    return -1000*y + 3000 - 2000*np.exp(-t)

sol = solve_ivp(stiff, [0, 0.1], [0], method='BDF')
result = sol.y[0, -1]
print(round(result, 2))
A0.00
B1.19
C0.10
D1.00
Attempts:
2 left
💡 Hint
BDF is good for stiff problems and will quickly approach the steady state.
🔧 Debug
advanced
2:00remaining
Identify the error in ODE solver usage
What error will this code raise when trying to solve dy/dt = y with RK45 method? from scipy.integrate import solve_ivp def fun(t, y): return y sol = solve_ivp(fun, [0, 1], 1, method='RK45')
SciPy
from scipy.integrate import solve_ivp

def fun(t, y):
    return y

sol = solve_ivp(fun, [0, 1], 1, method='RK45')
ATypeError: 'int' object is not iterable
BAttributeError: 'int' object has no attribute 'shape'
CNo error, runs successfully
DValueError: y0 must be array-like
Attempts:
2 left
💡 Hint
Check the type of initial condition y0 parameter.
❓ data_output
advanced
2:00remaining
Number of time points returned by solve_ivp with dense output
Using solve_ivp with method='RK45' and dense_output=True on dy/dt = -y, y(0)=1 over [0,5], how many time points are in sol.t?
SciPy
from scipy.integrate import solve_ivp

def fun(t, y):
    return -y

sol = solve_ivp(fun, [0, 5], [1], method='RK45', dense_output=True)
print(len(sol.t))
AVaries depending on solver steps
B100
C50
D10
Attempts:
2 left
💡 Hint
RK45 adapts step size, so number of points is not fixed.
🚀 Application
expert
2:00remaining
Choosing solver method for a stiff system
You have a system of ODEs modeling chemical reactions with very fast and slow dynamics mixed. Which solver method is best to use in solve_ivp for stable and efficient results?
ABDF
BRK23
CEuler explicit
DRK45
Attempts:
2 left
💡 Hint
Stiff systems require implicit solvers for stability.

Practice

(1/5)
1. Which scipy ODE solver method is best suited for stiff problems?
easy
A. BDF
B. RK45
C. Euler
D. Midpoint

Solution

  1. Step 1: Understand stiff problems

    Stiff problems require methods that handle rapid changes without instability.
  2. Step 2: Match solver to problem type

    BDF (Backward Differentiation Formula) is designed for stiff problems, unlike RK45 which is for general cases.
  3. Final Answer:

    BDF -> Option A
  4. Quick Check:

    Stiff problems = BDF [OK]
Hint: BDF is for stiff problems, RK45 for general ones [OK]
Common Mistakes:
  • Confusing RK45 as best for stiff problems
  • Thinking Euler method is in solve_ivp
  • Assuming Midpoint is a scipy method
2. Which of the following is the correct way to specify the RK45 method in solve_ivp?
easy
A. method='RungeKutta45'
B. method='rk45'
C. method='RK_45'
D. method='RK45'

Solution

  1. Step 1: Check method parameter syntax

    The solve_ivp function expects method names as strings exactly matching documented names.
  2. Step 2: Verify correct spelling and case

    The correct method name is 'RK45' with uppercase letters and no underscores or spaces.
  3. Final Answer:

    method='RK45' -> Option D
  4. Quick Check:

    Exact method name = 'RK45' [OK]
Hint: Use exact method names with correct case in solve_ivp [OK]
Common Mistakes:
  • Using lowercase 'rk45' instead of 'RK45'
  • Adding underscores like 'RK_45'
  • Using descriptive names not accepted by solve_ivp
3. What will be the output of this code snippet?
from scipy.integrate import solve_ivp
import numpy as np

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

sol = solve_ivp(fun, [0, 2], [1], method='RK45', t_eval=[0, 1, 2])
print(sol.y[0])
medium
A. [1. 0.13533528 0.01831564]
B. [1. 0.5 0.25]
C. [1. 2. 4.]
D. [0. 0. 0.]

Solution

  1. Step 1: Understand the ODE and solution

    The ODE is dy/dt = -2y with initial y(0)=1, which has exact solution y(t) = exp(-2t).
  2. Step 2: Calculate values at t=0,1,2

    y(0)=1, y(1)=exp(-2) ≈ 0.1353, y(2)=exp(-4) ≈ 0.0183.
  3. Final Answer:

    [1. 0.13533528 0.01831564] -> Option A
  4. Quick Check:

    exp(-2t) at t=0,1,2 = [1. 0.13533528 0.01831564] [OK]
Hint: Use exact solution exp(-2t) to check output array [OK]
Common Mistakes:
  • Confusing exponential decay with linear decay
  • Misreading t_eval points
  • Assuming RK45 returns integers
4. Identify the error in this code using BDF method:
from scipy.integrate import solve_ivp

def f(t, y):
    return y - t**2 + 1

sol = solve_ivp(f, [0, 2], 0.5, method='BDF', t_eval=[0, 1, 2])
print(sol.y)
medium
A. Function f must take only one argument
B. Initial condition y0 should be a list or array, not a float
C. Method 'BDF' is not supported in solve_ivp
D. t_eval must be a numpy array, not a list

Solution

  1. Step 1: Check initial condition format

    The initial condition y0 must be an array-like object, even if single value, e.g., [0.5].
  2. Step 2: Verify given y0 type

    Here y0 is given as 0.5 (float), which is not array-like and causes error.
  3. Step 3: Confirm other parameters

    Function f has correct signature, method 'BDF' is valid, t_eval can be list or array.
  4. Final Answer:

    Initial condition y0 should be a list or array, not a float -> Option B
  5. Quick Check:

    y0 must be array-like [OK]
Hint: Always pass y0 as list or array, not single float [OK]
Common Mistakes:
  • Passing y0 as float instead of list
  • Thinking BDF is unsupported
  • Believing t_eval must be numpy array
5. You want to solve a stiff ODE system and compare solutions from RK45 and BDF methods. Which approach correctly compares their results at specific times?
hard
A. Use only RK45 since BDF is slow and ignore BDF results
B. Run RK45 without t_eval and BDF with t_eval, then compare sol.t arrays
C. Use solve_ivp with t_eval for both methods, then compare sol.y arrays at those times
D. Compare the initial conditions only, since methods differ internally

Solution

  1. Step 1: Understand comparison requirements

    To compare solutions, evaluate both methods at the same time points using t_eval.
  2. Step 2: Use solve_ivp with t_eval for both

    Passing t_eval ensures outputs have matching time points, so sol.y arrays can be directly compared.
  3. Step 3: Reject incorrect approaches

    Not using t_eval leads to different time points; ignoring BDF or comparing only initial conditions misses solution differences.
  4. Final Answer:

    Use solve_ivp with t_eval for both methods, then compare sol.y arrays at those times -> Option C
  5. Quick Check:

    Same t_eval for both = valid comparison [OK]
Hint: Use t_eval to get matching time points for comparison [OK]
Common Mistakes:
  • Not using t_eval for both methods
  • Comparing sol.t arrays instead of sol.y
  • Ignoring BDF results due to speed concerns
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