Computation and Programming for Engineers

Solving ODEs Numerically

Stepping an initial value problem forward: Euler's method and its first-order error, fourth-order Runge-Kutta, step size and stiffness, and handing real problems to SciPy's adaptive solve_ivp.

  • 4 min
  • 4 steps
  • 4 questions
  • 1 hands-on
  • Lesson 7 of 7 in Computation and Programming for Engineers

In this lesson

  1. When you cannot solve it by hand
  2. Euler’s method
  3. Better steps: Runge–Kutta
  4. In practice: use a tested solver
Numerical ODEs

When you cannot solve it by hand

The differential-equations lesson showed how to solve linear ODEs analytically. Most real engineering models — nonlinear, coupled, driven by messy inputs — have no closed-form solution, so we solve them numerically: start from a known initial condition and step the solution forward in small increments. The problem is the initial value problem,

\[\frac{dy}{dt} = f(t, y), \qquad y(t_0) = y_0,\]

and a numerical method approximates \(y\) at a sequence of times \(t_0, t_1, \dots\) spaced by a step size \(h\) 1.

Euler’s method

The simplest scheme follows the slope the equation gives. At each step, use the derivative at the current point to extrapolate forward:

\[y_{n+1} = y_n + h\, f(t_n, y_n).\]

This is Euler’s method. It is intuitive — “take a small step in the direction the equation points” — but only first-order accurate: halving the step roughly halves the error, so an accurate solution needs many tiny steps. With a coarse step the approximation visibly drifts away from the truth, as the dashed curve in the figure shows for \(\frac{dy}{dt} = -y\) (whose exact solution is \(e^{-t}\)).

The same ODE solved exactly and by two numerical methods at a coarse step: Euler drifts noticeably, while RK4 tracks the exact curve closely.
The same ODE solved exactly and by two numerical methods at a coarse step: Euler drifts noticeably, while RK4 tracks the exact curve closely. source

Quick check

Euler’s method is first-order accurate. What does halving the step size do to the error?

Better steps: Runge–Kutta

Most production solvers do something smarter than a single slope sample. The classic fourth-order Runge–Kutta (RK4) method samples the slope four times across each step and combines them in a weighted average, achieving fourth-order accuracy — error shrinks like \(h^4\) 1. At the same coarse step where Euler drifts, RK4 (solid green) sits almost on top of the exact curve. That accuracy-per-step is why higher-order methods dominate in practice.

There is always a trade-off: smaller steps and higher order mean more accuracy but more computation, and some “stiff” problems demand special implicit methods to stay stable. Choosing the method and step size is part of the engineering.

Log-log plot of error at t = 5 for dy/dt = -y versus step size: Euler's error falls slowly from about 6e-3 to 3e-4, while RK4 falls steeply from about 3e-5 to 2e-11.
First-order Euler versus fourth-order RK4 as the step shrinks. Credit: StudyCorner chart · CC BY 4.0 · Source

Quick check

How many slope evaluations does classic RK4 make per step?

In practice: use a tested solver

You rarely hand-code these. SciPy provides robust, adaptive solvers that choose their own step sizes to meet an error tolerance 2:

import numpy as np
from scipy.integrate import solve_ivp

def f(t, y):
    return -y                      # dy/dt = -y

sol = solve_ivp(f, t_span=(0, 5), y0=[1.0],
                t_eval=np.linspace(0, 5, 100))
# sol.t, sol.y hold the solution to compare against exp(-t)

solve_ivp defaults to an adaptive Runge–Kutta method and handles step-size control for you. The lesson of the figure still applies, though: numerical solutions are approximations, and understanding error and stability is what keeps you from trusting a plot that has quietly drifted from reality.

Quick check

What does solve_ivp do that a fixed-step loop doesn’t?

Find the mistake

One part of this call to solve_ivp for dy/dt = -y is wrong.

This exercise needs JavaScript.

Lesson complete

Nice work.

1day streak
0/1today's goal
–correct

Teach it back

In two sentences, explain Solving ODEs Numerically to a friend who missed it.

Up next · 7 min

Free-Body Diagrams That Can Be Audited

A beam rests on two rollers sitting on a level floor. Someone gives it a sideways push. What resists the push?

Next lesson

Related lessons

Among the stars
Sources for this lesson
  1. 1
  2. 2
    SciPy Documentation. SciPy Developers. verifiedReference for numerical routines — root finding, integration, ODE solvers. Cited at: integrate.solve_ivp.

    Also cited in Numerical Integration and Differentiation, Root Finding and Linear Systems.