0
0
SciPydata~20 mins

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

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