COMP 521 · Lecture 11
Ordinary differential equations
Models, solutions, and numerical methods
Anna Rosen · San Diego State University
What an ODE describes
What does an ODE tell us?
An ODE relates an unknown function to its derivatives with respect to one independent variable.
Independent variable
Time, depth, radius, …
Unknown state function
State at coordinate x
Derivative
Change per unit coordinate
Think: Does a slope rule determine a unique function? What else is needed?
What an ODE describes
What information belongs in the state?
The state is the set of quantities we track to describe a system.
It can be a scalar y or a vector y of coupled variables.
Examples:
Population · scalar
How many individuals?
Spring · vector
Position and velocity
Star or planet · vector
Enclosed mass and pressure
Predict: Two spring masses have the same position but opposite velocities. How will their positions change next?
From a system to a mathematical model
Modeling: turn assumptions into equations
-
Choose the quantities. What do we want to describe?
-
State the assumptions. What detail can we neglect?
-
Write relationships. What laws or rules connect the quantities?
-
Set parameters and conditions. What specifies the problem?
From assumptions to a rate law
An ODE model: radioactive decay
Example: one radioactive isotope
-
State: N(t), expected undecayed nuclei.
-
Assumptions: independent decays; no replenishment.
Parameter: constant λ>0, units s−1.
Initial condition: N(0)=N0.
Initial value problem (IVP): an ODE and its starting state.
ODE models describe change with one independent variable, such as time or radius.
Applications: reactions and growth in time; pressure balance along radius.
What an ODE describes
“Ordinary” and “first order” mean different things
-
Ordinary: derivatives with respect to one independent variable.
-
Order: the highest derivative present.
First order
Population growth
Second order
An undamped spring: mass m, stiffness k (force per displacement).
Both are ODEs. Neither “ordinary” nor “order” requires time as the coordinate.
Define an ODE and its state
Partial differential equations (PDEs)
Heat equation
-
T(x,t): temperature varies with position and time.
-
α: thermal diffusivity, in cm2s−1.
One-temperature ODE: T(t) keeps one value for the whole object.
PDEs are central to computational modeling: fluid dynamics, electrostatics, and general relativity.
From a rate law to an exact solution
Model a growing bacterial culture
-
State: N(t), population size.
Rate law:
-
Parameter: constant net per-cell rate r>0 (s−1).
-
Initial condition: N(0)=N0>0.
-
Assumptions: no resource limit or migration.
Exact growth and decay models
Derive exponential growth and doubling time
r: constant net growth rate per individual, units time−1.
For N(0)=N0>0 and r>0:
Doubling time: set N(t2)=2N0.
Exact growth and decay models
Derive exponential decay and half-life
Rate law: N′=−λN, with constant λ>0 and N(0)=N0>0.
Separate:
Half-life: set N(t1/2)=N0/2.
a=r>0: growth. a=−λ<0: decay.
Exact growth and decay models
Derive cooling toward the bath temperature
-
Bath: fixed Tcold.
-
Initial state: Thot(0)=T0>Tcold.
1. Shift the temperature
2. Separate and integrate
3. Recover the temperature
Limit: θ→0; Thot→Tcold.
Newton’s law of cooling
-
Uniform temperature within the object.
-
Fixed bath temperature Tcold.
-
Constant rate: k>0, units s−1.
Spatial ODEs and computational motivation
Derive pressure as a function of depth
-
z increases downward from the surface.
-
Assume constant ρ and g; P(0)=Psurface.
Explain: Why can dP/dz=0 while ∂P/∂t=0?
Parameters: ρ: density; g: gravitational acceleration.
Spatial ODEs and computational motivation
Inside a planet, radius is the coordinate
-
m(r): enclosed mass.
-
P(r): pressure.
-
G: gravitational constant.
-
Outward: mass increases; pressure decreases.
Hydrostatic equilibrium: pressure forces balance gravity.
Newton’s second law: Fpressure+Fgravity=0.
Static model: neither expanding nor contracting.
Spatial ODEs and computational motivation
The equations need a material model and boundaries
-
Equation of state: pressure determines density, ρ(P).
-
Start with a trial central pressure; find the surface R.
Diagnose: Can smaller radial steps fix an incorrect ρ(P)? Explain.
Density closure joins radial mass and pressure equations into a specified planet model.
Spatial ODEs and computational motivation
We know the slope. Why still compute?
-
Knowing the derivative does not guarantee a useful solution formula.
-
Coupled equations, nonlinear material laws, and events lead us to compute.
Computable derivatives lead to an approximate solution with explicit numerical checks.
Spatial ODEs and computational motivation
Specify an initial value problem (IVP)
IVP: an ODE plus a specified starting state.
-
x0: starting coordinate.
-
y0: full starting state.
-
Solve: find y(x) satisfying both over the chosen interval.
For y′=y, if y(0) doubles, does y(1) double too? Why?
Yes: 2y(x) obeys the same ODE with twice the starting value.
From slopes to numerical steps
Approximate a derivative on a grid
Grid: xn=x0+nh, with h>0. Assume smooth y(x) and exact samples.
Forward: derivative error O(h).
Backward: derivative error O(h).
Centered: derivative error O(h2).
Nearby values give secant slopes that approximate y′(xn).
Euler, stability, and error
Euler turns a slope into a step
Euler: first order. Global state error O(h) over a fixed interval.
Discretize the derivative:
-
yn≈y(xn): a computed state.
Solve for the next state:
-
Use the starting slope for the whole step.
For growth y′=y, y(0)=1, the exact solution is y(t)=et. Will Euler lie above or below this curve? Why?
Decay model: u′=−u, u(0)=1.
Exact solution: uexact(t)=e−t.
Exact increment: integrate the ODE
-
The intermediate state y(s) is unknown.
Euler uses a left rectangle:
-
Starting slope × step approximates the increment.
Euler, stability, and error
Predict one Euler step
Decay model and exact solution:
Step: t0=0, h=0.2, so t1=0.2.
Compute the Euler value u1. Compare it with uexact(0.2) from the displayed solution. Explain using curvature.
-
Exact: uexact(0.2)=e−0.2≈0.818731.
Euler, stability, and error
Shrinking magnitude does not guarantee positive states
Same decay model: u′=−u, u(0)=1.
Exact: uexact(t)=e−t>0.
Euler: substitute f(tn,un)=−un.
-
h=0.25: multiplier 0.75 → positive decay.
-
h=1.5: -0.5 → alternating sign. h=3: -2 → growing magnitude.
Euler, stability, and error
One old state supplies every coupled slope
-
Compute every slope from the same old state.
-
Slope → increment → new state.
Euler computes all coupled slopes from the same old state.
Euler, stability, and error
What does Euler leave out?
Exact step: Taylor expansion
-
The ODE supplies y′=f(x,y).
Euler keeps the constant and linear terms:
First omitted term:
-
One-step state defect: O(h2).
-
Divide by h: derivative defect O(h).
-
Over a fixed interval: global error O(h).
Decay model: u′=−u, u(0)=1.
Exact solution: uexact(t)=e−t.
Euler, stability, and error
Local defects become global error
State error: en=∥yn−y(xn)∥.
-
L: bound on the slope’s sensitivity to the state.
-
Ch2: bound on the new defect from one Euler step.
-
Propagated error + new defects: first-order global error.
Decay model: u′=−u, u(0)=1.
Exact solution: uexact(t)=e−t.
Euler, stability, and error
Halve the step; measure the error
Decay benchmark: u′=−u, u(0)=1.
-
p: the method’s global order.
-
Hold fixed: initial state, endpoint T, and error norm.
-
Euler: halving h should approach half the error.
Oscillators as numerical laboratories
A spring needs position and velocity
-
m: mass; k: spring stiffness, force per displacement.
-
x: displacement from equilibrium; v: velocity.
-
ω: angular frequency, units s−1.
-
Position and velocity determine the next change.
Hooke’s law: the restoring force opposes displacement.
The harmonic oscillator as a benchmark
Derive the oscillator benchmark
Displacement amplitude: A.
Dimensionless variables: q=Ax, p=Aωv, t=ωtphys.
Initial state: q(0)=1, p(0)=0.
The harmonic oscillator as a benchmark
Phase diagram: same energy, different states
-
Phase diagram: each point (q,p) represents a state.
-
Energy conservation imposes an algebraic constraint:
-
(1,0) and (0,−1) lie on the same energy circle.
-
Energy fixes the circle, not the state at a given time.
The harmonic oscillator as a benchmark
Why does Euler spiral outward?
-
Both updates use the original state.
-
Phase experiment: (q0,p0)=(1,0), h=0.1, T=12.
-
Every h=0 expands this numerical orbit.
Midpoint and classical RK4
Midpoint: predict, sample, update
Explicit midpoint (RK2): second order. Global state error O(h2).
-
Predict a temporary midpoint state.
-
Update from the original state, using the midpoint slope.
For y′=y, set k2=f(xn+h/2,yn). Does the update differ from Euler?
For y′=y, missing the midpoint state gives Euler, first order.
Same oscillator: q′=p, p′=−q.
-
(q0,p0)=(1,0); h=0.1, T=12.
-
RK2: much less outward drift than Euler.
-
Small drift is still not exact energy conservation.
Explicit midpoint: starting slope, predicted midpoint, new slope, and old-state base.
Local Taylor check: set yn=y(xn)=y.
-
fx=∂f/∂x, fy=∂f/∂y.
-
Matches the exact step through h2.
-
One-step O(h3) → global O(h2).
Midpoint and classical RK4
Trace one midpoint step
Compute ymid and y1. Is the trial midpoint exact at x0+h/2?
-
Midpoint: y1=1+0.1(1.05)=1.105.
-
Exact midpoint: e0.05≈1.051271096.
-
Exact endpoint: e0.1≈1.105170918.
Midpoint and classical RK4
RK4: build four trial slopes
Classical RK4: fourth order. Global state error O(h4).
Trace: y′=y, y0=1, h=0.1.
If we stop here and use y1=y0+hk2, what method and order do we get?
k1=1, k2=1.05.
Stopping here gives midpoint RK2, second order.
Known: k2=1.05.
For this same step, is k3=k2? Explain before calculating.
k3=1.0525, k4=1.10525.
Same midpoint coordinate; different predicted states.
The first trial midpoint.
New slopes use new trial states.
Midpoint and classical RK4
RK4: combine slopes, then update once
-
Exact midpoint: e0.05≈1.051271096.
-
Exact endpoint: e0.1≈1.105170918.
-
Update the original state once.
For y′=y, the RK4 update becomes
-
RRK4: the step multiplier for this model.
Same oscillator: q′=p, p′=−q.
-
(q0,p0)=(1,0); h=0.1, T=12.
-
RK4: nearly overlaps the exact circle.
-
Energy is not conserved exactly.
-
Inspect the endpoint for state and phase error.
Completed scalar RK4 trace with the four stage states and slopes.
Exact step: y(x+h)=y(x)eh.
-
Matches the exact step through h4.
-
One-step state defect: O(h5).
-
Global state error: O(h4).
Order: global accuracy, not ODE order or stage count. This example is not a general RK4 order proof.
Midpoint and classical RK4
Higher order versus more work per step
-
Same experiment: (q0,p0)=(1,0), T=2; halve h.
-
Euler / midpoint / RK4: orders 1,2,4; calls/step 1,2,4.
-
Compare work at equal error.
Two ways to improve a numerical answer
Richardson: refine, then cancel the leading error
Same problem, same endpoint: u′=−u, u0=1, h=0.2.
One full Euler step, or two half steps.
Are two half Euler steps a second-order method? Distinguish refinement from correction.
Two half steps still give a first-order method.
This Euler correction is second order.
-
Assume a smooth solution and stable error propagation.
-
Order depends on the base method and its error expansion.
Leapfrog, energy, and phase
Leapfrog: kick, drift, kick
Leapfrog belongs to the symplectic family: second order, global state error O(h2).
-
H=p2/(2m)+V(q); force F(q)=−V′(q).
State: q: position; p: momentum.
Energy: H: total; V(q): potential; m: mass.
Scaled oscillator: m=1, F(q)=−q.
The symmetric composition changes momentum, position, then momentum.
Leapfrog, energy, and phase
Preserve the structure of the equations
-
Φh: the one-step map from (qn,pn) to (qn+1,pn+1).
-
J=DΦh: the state Jacobian of the map.
-
For one canonical pair: oriented phase-space area is preserved.
Symplectic structure, state accuracy, and physical energy are separate claims.
Fixed-step kick–drift–kick: symplectic, second order, and time reversible.
Leapfrog, energy, and phase
Same oscillator, different energy behavior
-
Resolution: 100 steps per period.
-
Duration: 20 periods.
-
Initial state: (q0,p0)=(1,0).
-
Same step; costs differ.
First plot (log): ∣E−E0∣/E0. Zero errors omitted.
-
RK2: growing error.
-
Leapfrog: bounded oscillations.
-
RK4: much smaller drift.
Next: signed RK4 error.
Leapfrog, energy, and phase
Good energy; wrong timing?
-
Phase: timing along the orbit.
-
Energy: its level set.
-
Log scale: phase-error magnitude. Zero values are omitted.
-
Same 100 steps per period and 20 periods.
-
Check the full state at the same time.
Return to Project 4 and synthesize
A planet model wraps integration in a root solve
-
Choose Pc → integrate → locate the surface → return M,R.
-
Adjust Pc until M(Pc)=Mtarget.
Shooting wraps center-to-surface IVP integrations in an outer root solve.
Return to Project 4 and synthesize
Explain the mechanism
-
Halving h halves endpoint error. Which order does this suggest?
-
An RK4 routine reuses k2 as k3. Which state calculation did it skip?
-
Oscillator peaks arrive late despite small energy error. Which diagnostic detects this?
Discuss: Which checks apply to a static planet profile? Which are oscillator-specific?
State, energy, and phase measurements answer different questions.
Optional references
Euler’s finite-interval error bound
-
Exact initial data; fixed T=Nh.
-
If L=0, the bound is CTh.
Optional references
Why explicit midpoint is second order
-
fx=∂f/∂x: coordinate derivative.
-
Jf=∂f/∂y: state Jacobian.
-
Multiply by h; add the original state.
-
The expansions agree through h2.
Stage-state and stage-coordinate shifts both contribute to the expansion.
Optional references
Implicit stability is not resolved accuracy
Backward Euler: first order. Global state error O(h).
Amplification factor: R=un+1/un.
-
Exact amplification: e−10.
-
Stable, but the fast transient is unresolved.
Optional references
Adaptive steps control an error estimate
-
Compare two approximations to estimate local error.
-
Accept or reject before advancing.
-
Internal steps need not match output spacing.
The error scale depends on component units and the requested observable.