0
0
SciPydata~20 mins

Solving ODEs (solve_ivp) 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 solve_ivp with simple ODE
What is the output array of y values when solving dy/dt = 2t with initial condition y(0)=1 over t in [0,1] using solve_ivp with default settings?
SciPy
from scipy.integrate import solve_ivp

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

sol = solve_ivp(ode, [0, 1], [1])
print(sol.y[0])
A
[1.         1.02040816 1.08163265 1.18367347 1.32653061 1.51020408
 1.73469388 2.      ]
B[1. 1. 1. 1. 1. 1. 1. 1.]
C[1. 2. 3. 4. 5. 6. 7. 8.]
D[1. 1.5 2.5 4. 6.5 10. 15.5 23.]
Attempts:
2 left
💡 Hint
Recall the analytical solution y = t^2 + 1 and that solve_ivp samples points automatically.
data_output
intermediate
1:30remaining
Number of time points in solve_ivp solution
Using solve_ivp to solve dy/dt = -3y with y(0)=5 over [0,1], how many time points does the solution contain by default?
SciPy
from scipy.integrate import solve_ivp

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

sol = solve_ivp(ode, [0, 1], [5])
print(len(sol.t))
A10
B100
C50
D5
Attempts:
2 left
💡 Hint
Check the default number of points solve_ivp evaluates if no t_eval is given.
🔧 Debug
advanced
1:30remaining
Identify the error in solve_ivp call
What error will this code raise? from scipy.integrate import solve_ivp def f(t, y): return t - y sol = solve_ivp(f, 0, [1])
SciPy
from scipy.integrate import solve_ivp

def f(t, y):
    return t - y

sol = solve_ivp(f, 0, [1])
ANo error, runs successfully
BTypeError: 'int' object is not iterable
CValueError: t_span must be a sequence of length 2
DTypeError: solve_ivp() missing 1 required positional argument: 'y0'
Attempts:
2 left
💡 Hint
Check the type and format of the second argument to solve_ivp.
🚀 Application
advanced
2:00remaining
Predict final value of logistic growth model
Using solve_ivp to solve the logistic growth ODE dy/dt = 0.5*y*(1 - y/10) with y(0)=1 over [0, 10], what is the approximate value of y at t=10?
SciPy
from scipy.integrate import solve_ivp

def logistic(t, y):
    return 0.5 * y * (1 - y / 10)

sol = solve_ivp(logistic, [0, 10], [1], t_eval=[10])
print(sol.y[0][0])
A5.0
B9.9
C10.5
D1.0
Attempts:
2 left
💡 Hint
The logistic model approaches the carrying capacity 10 over time.
🧠 Conceptual
expert
2:30remaining
Effect of changing 'method' parameter in solve_ivp
Which statement correctly describes the effect of changing the 'method' parameter in solve_ivp from 'RK45' to 'BDF'?
AChanging 'method' only affects the output format, not the solving process.
B'BDF' always runs faster than 'RK45' regardless of the problem type.
C'RK45' uses implicit methods suitable for stiff problems, 'BDF' uses explicit methods.
D'BDF' is better for stiff ODEs and may use implicit methods, while 'RK45' is explicit and better for non-stiff problems.
Attempts:
2 left
💡 Hint
Consider the difference between explicit and implicit solvers and stiffness of ODEs.