Initial Value Problems
An initial value problem (IVP) asks for with for and a given starting value . Population growth, chemical kinetics, orbits, circuits and epidemics are all IVPs, and most have no formula for the solution. This part of the course is about computing them. The theory of what the solutions look like is in Differential Equations.
Existence, uniqueness and sensitivity
If is continuous and Lipschitz in , , then the IVP has exactly one solution (Existence and Uniqueness). The same constant controls sensitivity: two solutions starting apart can separate at most like
This bound is often pessimistic: if solutions converge (as for ), errors shrink instead. But for problems where solutions diverge, any small error, including the method's own, grows exponentially, and long-time accuracy is impossible no matter the method.
import numpy as np
from scipy.integrate import solve_ivp
for name, f in (("u' = -u (solutions converge)", lambda t, u: -u),
("u' = u (solutions diverge) ", lambda t, u: u)):
s1 = solve_ivp(f, (0, 10), [1.0], rtol=1e-12, atol=1e-12)
s2 = solve_ivp(f, (0, 10), [1.0 + 1e-6], rtol=1e-12, atol=1e-12)
print(f"{name}: initial gap 1e-6, gap at t = 10: {abs(s2.y[0, -1] - s1.y[0, -1]):.1e}")u' = -u (solutions converge): initial gap 1e-6, gap at t = 10: 4.5e-11
u' = u (solutions diverge) : initial gap 1e-6, gap at t = 10: 2.2e-02
Blow-up
Even a smooth may give a solution that exists only for a finite time. , has solution , which blows up at . A solver approaching must take smaller and smaller steps and eventually fails, which is the correct outcome.
from scipy.integrate import solve_ivp
sol = solve_ivp(lambda t, u: u**2, (0, 2), [1.0], rtol=1e-8)
print("status:", sol.status, "|", sol.message)
print(f"stopped at t = {sol.t[-1]:.6f}, u = {sol.y[0, -1]:.3e}")status: -1 | Required step size is less than spacing between numbers.
stopped at t = 1.000000, u = 2.585e+13
Using solve_ivp
SciPy's solve_ivp(fun, t_span, y0, ...) is the standard interface. Key options:
| Option | Purpose |
|---|---|
method |
"RK45" (default), "DOP853", "RK23", "Radau", "BDF", "LSODA" |
t_eval |
times at which to store the solution |
dense_output=True |
a continuous interpolant sol.sol(t) |
rtol, atol |
error tolerances (defaults and , often too loose) |
events |
functions whose zero crossings are located (and can stop the integration) |
args |
extra parameters passed to fun |
import numpy as np
from scipy.integrate import solve_ivp
def logistic(t, u, r, K):
return r * u * (1 - u / K)
sol = solve_ivp(logistic, (0, 10), [10.0], args=(0.9, 1000.0), dense_output=True, rtol=1e-8)
exact = lambda t: 1000 / (1 + 99 * np.exp(-0.9 * t))
for t in (2.0, 5.0, 8.0):
print(f"t = {t}: u = {sol.sol(t)[0]:.6f}, exact {exact(t):.6f}")
print("right-hand side evaluations:", sol.nfev)t = 2.0: u = 57.588462, exact 57.588460
t = 5.0: u = 476.237983, exact 476.237951
t = 8.0: u = 931.175000, exact 931.174998
right-hand side evaluations: 338
The plan for this part
We build the main families of methods from scratch: Euler's Method, Runge-Kutta Methods, Adaptive Step Size, Multistep Methods, then Stiff Equations and Long-Time Integration and Symplectic Methods.
In software
scipy.integrate.solve_ivp(modern) andodeint(older LSODA wrapper, argument orderf(y, t)).- Julia's DifferentialEquations.jl, MATLAB's
ode45/ode15sand SUNDIALS (CVODE, IDA) are the main alternatives; game engines use fixed-step integrators.
Exercises
Exercise 1: Lipschitz constant
Find a Lipschitz constant for on all of .
Answer
.
Solution
.
Exercise 2: Blow-up time
When does the solution of , blow up?
Answer
At .
Solution
; the denominator vanishes at .
Exercise 3: Event detection (code)
A ball is thrown up at 12 m/s from 2 m height (). Use solve_ivp with an event to find when it hits the ground, and compare with the formula.
Answer
About s from both.
Solution
import numpy as np
from scipy.integrate import solve_ivp
def ground(t, y): return y[0]
ground.terminal = True; ground.direction = -1
sol = solve_ivp(lambda t, y: [y[1], -9.81], (0, 10), [2.0, 12.0], events=ground, rtol=1e-10)
exact = (12 + np.sqrt(144 + 4 * 4.905 * 2)) / 9.81
print(f"{sol.t_events[0][0]:.6f} {exact:.6f}")2.603121 2.603121
Test yourself
A Lipschitz condition on guarantees…
- the solution is a polynomial
- existence and uniqueness of the solution
- the solution decays
- Euler's method is exact
For an initial error of grows by to about…
Which solve_ivp option gives a continuous solution you can evaluate anywhere?
t_evaldense_output=Trueeventsvectorized