Numerical ODE Solver Back
Numerical Analysis

Numerical ODE Solver Comparison

Compare Euler, Heun, RK2, RK4, and RK45 methods on the same problem. Visualize accuracy and stability differences. Presets: logistic growth, Van der Pol oscillator, Lorenz equations.

Equation
Methods
Step Size & Time Span
Step size h
End time T
s
Results
Steps
Order
Ode
Move the step size slider to observe how method accuracy changes. Try large h to see instability.
Theory & Key Formulas
Euler: O(h) 1st order
Heun: O(h²) 2nd order
RK4: O(h⁴) 4th order
Halving h → RK4
error × 1/16

What is Numerical ODE Solving?

🙋
What exactly is a numerical ODE solver? Why can't we just use the exact math formulas?
🎓
Great question! Basically, most ordinary differential equations (ODEs) that describe real-world physics—like population growth, pendulum swings, or circuit behavior—don't have neat, closed-form solutions. A numerical solver is a step-by-step recipe that approximates the solution. In this simulator, you can compare different "recipes," like Euler and RK4, to see which gives a better approximation for the same problem.
🙋
Wait, really? So they all give different answers? What makes one method "better" than another?
🎓
Exactly! The key trade-offs are accuracy and computational cost. A simple method like Euler (1st order) is fast per step but can drift far from the true solution. A higher-order method like RK4 does more calculations per step but can take much larger, more accurate steps. Try moving the "Step size h" slider to a large value like 0.5 on the logistic growth problem—you'll see Euler's line diverge wildly, while RK4 stays much closer to the true curve.
🙋
That makes sense. But what's this "RK45" method? And why does the simulator show both error and stability?
🎓
RK45 is an adaptive method—it's the smart one! It automatically adjusts its step size while solving. If the solution is changing slowly, it takes big steps; if things get wild (like in the Van der Pol oscillator), it takes tiny steps to maintain accuracy. The "stability" plot shows a different, crucial concept: whether small errors blow up over time. Change the problem to "Exponential Decay" and set a very large step size—some methods will produce a crazy, oscillating result instead of a smooth decay. That's numerical instability in action.

Physical Model & Key Equations

The core of any ODE solver is approximating the derivative $dy/dt$ at a point to predict the next value. The general first-order ODE is:

$$ \frac{dy}{dt}= f(t, y), \quad y(t_0) = y_0 $$

Here, $f(t, y)$ is the rate function (e.g., growth rate), $t$ is time, $y$ is the state we're solving for (like population), and $y_0$ is the initial condition. The solver's job is to find $y_1, y_2, ...$ at times $t_1 = t_0 + h, t_2 = t_0 + 2h$, etc., where $h$ is the step size you control in the simulator.

The methods differ in how they estimate the "average slope" across a step. For example, Euler's method (1st order) simply uses the slope at the beginning of the step:

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

Meanwhile, the classic 4th-order Runge-Kutta (RK4) method takes a weighted average of four slope estimates within the step:

$$ \begin{align*}k_1 &= f(t_n, y_n) \\ k_2 &= f(t_n + h/2, y_n + h k_1/2) \\ k_3 &= f(t_n + h/2, y_n + h k_2/2) \\ k_4 &= f(t_n + h, y_n + h k_3) \\ y_{n+1}&= y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4) \end{align*} $$

This extra computation gives a much better estimate of the true average slope, which is why RK4's error shrinks as $O(h^4)$. Halving the step size reduces its error by a factor of $2^4 = 16$, as noted in the theory panel.

Frequently Asked Questions

The Euler method is the simplest and least accurate; if the step size is not small enough, errors become large. Heun's method and RK2 are improved versions, RK4 offers high accuracy and stability, and RK45 is an adaptive solver that automatically adjusts the step size. They differ in the balance between accuracy and computational cost.
A larger step size makes calculations faster but increases error and may lead to instability. A smaller step size improves accuracy but increases computation time. Especially with the Euler method, a step size that is too large can cause divergence, so try appropriate values and compare.
Logistic growth simulates population or resource increase and saturation, the Van der Pol oscillator simulates nonlinear self-excited oscillations (e.g., in electronic circuits or biological rhythms), and the Lorenz equations simulate a chaotic weather model. With each model, you can observe differences in solver stability and accuracy.
The main cause is a step size that is too large. Particularly with the Euler method or explicit solvers, divergence occurs when stability conditions (such as the CFL condition) are not met. Reducing the step size or choosing an adaptive step size method like RK45 can improve the situation.

Real-World Applications

CAE & Multibody Dynamics: When simulating the motion of a car's suspension or a robotic arm, the equations of motion are complex, coupled ODEs. Solvers like RK4 are the workhorses behind the scenes, calculating positions and velocities at each time step to generate accurate animations and force data.

Circuit Simulation (SPICE): Simulating voltage and current over time in an electronic circuit involves solving ODEs defined by capacitors and inductors. Adaptive methods like RK45 are crucial here to handle both fast switching transients and long, steady-state periods efficiently.

Epidemiology & Population Modeling: Models like logistic growth or SIR (Susceptible-Infected-Recovered) are systems of ODEs. Public health officials use numerical solvers to project infection curves and test intervention strategies, where stability of the solution is critical for reliable forecasts.

Aerospace & Trajectory Control: Calculating a spacecraft's flight path involves integrating equations for position, velocity, and attitude. High-fidelity simulations use advanced, adaptive solvers to minimize fuel consumption errors over long journeys while ensuring numerical stability.

Common Misconceptions and Points to Note

First, understand that "higher-order does not always equal better". It's true that RK4 is vastly more accurate than Euler's method. However, there are cases, like in real-time simulations for control systems where calculation speed is paramount and some error is absorbed within the feedback loop, where Euler's method might be chosen. Try selecting "Logistic Growth" in this tool and comparing the methods with a step size of h=1.0. The results for RK4 and Euler will differ significantly, but if you refine the step size to h=0.1, even Euler's method approaches a solution usable in practice. In other words, there is a trade-off relationship between "step size" and "order".

Next, don't overlook "stability". For example, if you run Euler's method on the "Van der Pol Oscillator" with a very large step size like h=2.0, the solution may diverge and the graph can blow up. This is a phenomenon called numerical instability, and the maximum allowable step size differs by method. In practical work, it's essential to estimate a stable step size in advance to prevent the calculation from failing.

Finally, don't overtrust the "automatic step size control" of RK45. It's not a universal tool; you need to set a target accuracy—the tolerance (the "Tolerance" parameter in the tool). Setting a loose tolerance (e.g., 1e-3) makes the calculation fast but yields a coarse solution, while a strict tolerance (e.g., 1e-9) yields high precision but skyrockets computational cost. Always determine parameters by working backward from the requirement: "What level of accuracy is needed?"

How to Use

  1. Select an ODE problem from the dropdown: logistic growth (dy/dt = 0.5y(1-y/100)), Van der Pol oscillator (μ=5), or Stiff equation (dy/dt = -1000y + 1000cos(t)).
  2. Set integration parameters: step size h (0.001 to 0.1), time span T (0.1 to 100), and initial condition y0.
  3. Enable solver methods using checkboxes (Euler, Heun, RK4, RK45 adaptive). Click "Solve" to compute trajectories and display error metrics relative to exact solution.
  4. Observe convergence rates and stability regions in the phase-plane or time-series plot.

Worked Example

For logistic growth with y0=10, T=20, h=0.01: Euler (O(h)) requires 2000 steps and produces 3.2% error; Heun (O(h²)) uses 2000 steps with 0.15% error; RK4 (O(h⁴)) achieves 0.0008% error. RK45 adaptive starts with h=0.01, reduces to h=0.002 near rapid changes, completing in 1847 accepted steps. For the stiff equation with h=0.1, Euler diverges unstably, while RK4 remains stable but requires h≤0.0005 for accuracy—adaptive RK45 auto-selects safe steps.

Practical Notes

  1. Euler fails on stiff systems (e.g., chemical kinetics with rate constants >1000 s⁻¹); use RK4 with h≤0.001 or adaptive RK45.
  2. Heun balances speed and accuracy for non-stiff oscillators; typical h=0.01 gives engineering tolerances (0.1–1% error) in half the RK4 evaluations.
  3. RK45 adaptive is essential when solution features vary (Van der Pol limit cycles: fast near transitions, slow on stable manifolds); it auto-refines h without user guessing.
  4. Truncation error scales as O(h^p) per step; for Euler at h=0.01 vs h=0.001, expect 10× error reduction; RK4 shows 10,000× reduction.