Numerical Methods · Learn

First-Order ODEs

A mini field guide to the differential equations you meet first — what they look like, the three you can solve by hand, and what to do with the many you can't.

▶ Play the lab 📖 Learn the theory

1What is an ODE?

An ordinary differential equation relates a function to its own derivatives. A first-order ODE involves only the first derivative and can almost always be written as

$$ \frac{dy}{dx} = f(x,y) $$

That right-hand side is a recipe for the slope at every point — which is exactly what the direction field in the lab draws. Pair the equation with an initial condition $y(x_0)=y_0$ and (under mild conditions) you get one unique solution curve threading through that point.

2Separable equations

The friendliest kind. If the slope factors into an $x$-part times a $y$-part,

$$ \frac{dy}{dx} = g(x)\,h(y), $$

you can pull all the $y$'s to one side and all the $x$'s to the other, then integrate both sides:

$$ \int \frac{dy}{h(y)} = \int g(x)\,dx + C $$

Worked example · y′ = x y

Separate: $\dfrac{dy}{y} = x\,dx$. Integrate: $\ln|y| = \tfrac12 x^2 + C$. Solve: $\;y = A\,e^{x^2/2}$.

3First-order linear equations

The workhorse of engineering — RC circuits, mixing tanks, Newton's cooling. Anything of the form

$$ \frac{dy}{dx} + p(x)\,y = q(x) $$

is solved with an integrating factor $\mu(x)=e^{\int p\,dx}$. Multiply through by $\mu$ and the left side becomes a perfect derivative, $\frac{d}{dx}\!\left(\mu y\right)=\mu q$, so:

$$ y = \frac{1}{\mu(x)}\left( \int \mu(x)\,q(x)\,dx + C \right) $$

Recipe

  1. Put it in standard form $y' + p(x)y = q(x)$.
  2. Compute $\mu = e^{\int p\,dx}$.
  3. Then $\;\mu y = \int \mu q\,dx + C$, and divide by $\mu$.

4Exact equations

Written as $M(x,y)\,dx + N(x,y)\,dy = 0$, the equation is exact when

$$ \frac{\partial M}{\partial y} = \frac{\partial N}{\partial x}. $$

Then there's a potential $F(x,y)$ with $F_x=M$, $F_y=N$, and the solution is simply the level curves $F(x,y)=C$. (When it isn't exact, an integrating factor can sometimes make it so.)

5When you can't solve by hand

Most first-order ODEs — including innocent-looking ones like $y'=x^2+y^2$ — have no closed-form solution. That's the rule, not the exception, so engineers march them forward numerically.

Euler's method

Step along the current slope:

$$ y_{n+1} = y_n + h\,f(x_n,y_n) $$

Dead simple, but the slope changes mid-step, so Euler cuts corners — its global error is only $\mathcal{O}(h)$. Halving the step only halves the error.

Runge–Kutta (RK4)

Sample the slope four times per step and blend them:

$$ y_{n+1} = y_n + \tfrac{h}{6}\,(k_1 + 2k_2 + 2k_3 + k_4) $$

with $k_1=f(x_n,y_n)$, $k_2=f(x_n+\tfrac h2, y_n+\tfrac h2 k_1)$, $k_3=f(x_n+\tfrac h2, y_n+\tfrac h2 k_2)$, $k_4=f(x_n+h, y_n+hk_3)$. The error drops to $\mathcal{O}(h^4)$ — halving the step cuts error by sixteen. That's why RK4 is the everyday default.

▶ In the lab

Type any $f(x,y)$, click to launch a solution, and slide the step size up: the red Euler path peels away from the gray truth while green RK4 stays locked on. Open the ODE solver →

6Worked example

From the course · RK4 vs. predictor–corrector

Problem. Solve $y' = -2t\,y^2,\ y(0)=1$ on $[0,1]$ with step $h=0.2$ using fourth-order Runge–Kutta, then again with the Adams–Bashforth–Moulton predictor–corrector. Compare to the exact solution $y(t)=\dfrac{1}{1+t^2}$.

What happens. RK4 marches $t=0\to1$ in just five steps and lands on $y(1)\approx 0.5000$ — matching the exact $1/(1+1^2)=0.5$ to four decimals. The predictor–corrector reuses the RK4 starting values, then leapfrogs forward with only one or two function evaluations per step, getting comparable accuracy more cheaply. Both crush Euler at the same step size.

Why it's a great test. The exact answer is a tidy rational function, so every digit of error is visible — and the nonlinear $y^2$ makes it a real workout, not a toy linear ODE.

▶ Play it

In the ODE solver, type f(x, y) = -2*x*y^2 (here $x$ plays the role of $t$), set the step size near 0.2, and click near the point (0, 1). Watch RK4 (green) ride right on top of the exact curve $y=1/(1+x^2)$ while Euler (red) sags below — then shrink $h$ and watch Euler climb back. Open the ODE solver →

Key takeaways

📄 Further reading

Bjorn Poonen's free MIT 18.03 lecture notes (PDF) cover this material in much more depth — §6 (first-order linear ODEs), §9–13 (second-order & higher-order linear ODEs), and §34 (Euler's method & the midpoint method). §27.2, "Solving an ODE using Fourier series," is a great next step once you've played with the Fourier builder — it drives $x''+x=f(t)$ with a Fourier series instead of a single sine.

EngineeringCandy · Learn · the theory behind the playground