TExcelTAD
Home › Numerical Methods › Initial Value Problems

Initial Value Problems

An initial value problem (IVP) asks for u(t)u(t) with u′=f(t,u)u' = f(t, u) for t≥at \ge a and a given starting value u(a)=u0u(a) = u_0. 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 ff is continuous and Lipschitz in uu, ∣f(t,u)−f(t,v)∣≤L∣u−v∣\lvert f(t, u) - f(t, v)\rvert \le L\lvert u - v\rvert, then the IVP has exactly one solution (Existence and Uniqueness). The same constant controls sensitivity: two solutions starting δ\delta apart can separate at most like

∣u(t)−v(t)∣≤∣u(a)−v(a)∣ eL(t−a).\lvert u(t) - v(t)\rvert \le \lvert u(a) - v(a)\rvert\,e^{L(t - a)}.

This bound is often pessimistic: if solutions converge (as for u′=−uu' = -u), 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.

python · try it yourself
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 ff may give a solution that exists only for a finite time. u′=u2u' = u^2, u(0)=1u(0) = 1 has solution u=11−tu = \frac{1}{1 - t}, which blows up at t=1t = 1. A solver approaching t=1t = 1 must take smaller and smaller steps and eventually fails, which is the correct outcome.

python · try it yourself
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 10−310^{-3} and 10−610^{-6}, often too loose)
events functions whose zero crossings are located (and can stop the integration)
args extra parameters passed to fun
python · try it yourself
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) and odeint (older LSODA wrapper, argument order f(y, t)).
  • Julia's DifferentialEquations.jl, MATLAB's ode45/ode15s and SUNDIALS (CVODE, IDA) are the main alternatives; game engines use fixed-step integrators.

Exercises

Exercise 1: Lipschitz constant

Find a Lipschitz constant for f(t,u)=sin⁡(t) uf(t, u) = \sin(t)\,u on all of R2\mathbb{R}^2.

Answer

L=1L = 1.

Solution

∣sin⁡(t)u−sin⁡(t)v∣=∣sin⁡t∣ ∣u−v∣≤∣u−v∣\lvert\sin(t)u - \sin(t)v\rvert = \lvert\sin t\rvert\,\lvert u - v\rvert \le \lvert u - v\rvert.

Exercise 2: Blow-up time

When does the solution of u′=u2u' = u^2, u(0)=2u(0) = 2 blow up?

Answer

At t=12t = \frac{1}{2}.

Solution

u=112−tu = \frac{1}{\frac{1}{2} - t}; the denominator vanishes at t=12t = \frac{1}{2}.

Exercise 3: Event detection (code)

A ball is thrown up at 12 m/s from 2 m height (h′′=−9.81h'' = -9.81). Use solve_ivp with an event to find when it hits the ground, and compare with the formula.

Answer

About 2.60312.6031 s from both.

Solution
python · try it yourself
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 ff guarantees…

  1. the solution is a polynomial
  2. existence and uniqueness of the solution
  3. the solution decays
  4. Euler's method is exact

For u′=uu' = u an initial error of 10−610^{-6} grows by t=10t = 10 to about…

  1. 10−610^{-6}
  2. 2×10−22 \times 10^{-2}
  3. 10−1010^{-10}
  4. 11

Which solve_ivp option gives a continuous solution you can evaluate anywhere?

  1. t_eval
  2. dense_output=True
  3. events
  4. vectorized