Euler Method vs RK4 Back
Numerical Analysis

Euler Method vs RK4 Comparison

Solve ODEs with three numerical methods and compare against exact solutions. Adjust step size h to see O(h), O(h²), and O(h⁴) accuracy differences on a live log-log error plot.

Parameters
ODE Selection
Initial value y₀
x₀ (start)
xₙ (end)
Step size h

While paused, move the sliders to update the result instantly.

Euler's Method Integrating Live (vs Exact Solution)
Live Readouts
Step size h
Current x
Euler estimate y
Error vs exact
Theory & Key Formulas

Forward Euler

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

RK4 Formula

$$k_1 = f(x_n,\,y_n),\quad k_2 = f\!\left(x_n+\tfrac{h}{2},\,y_n+\tfrac{h}{2}k_1\right)$$ $$k_3 = f\!\left(x_n+\tfrac{h}{2},\,y_n+\tfrac{h}{2}k_2\right),\quad k_4 = f(x_n+h,\,y_n+h\,k_3)$$ $$y_{n+1}= y_n + \frac{h}{6}(k_1+2k_2+2k_3+k_4)$$

What is Numerical Integration for ODEs?

🙋
What exactly is the difference between Euler and RK4? They both seem to just step forward to solve an equation.
🎓
Basically, it's a difference in "how carefully" they step. Euler is like walking blindfolded: you take one step based only on the slope where you're standing. RK4 is like checking the slope four times per step—at the start, middle, and end—to get a much better average direction. In practice, this makes RK4 far more accurate for the same step size.
🙋
Wait, really? So if I make the step size h really small, does Euler become just as good as RK4?
🎓
That's a great instinct, and you can test it right here! Try setting the "Step size h" slider to a very small value, like 0.01. You'll see the lines get closer. But here's the catch: to match RK4's accuracy, Euler needs a much smaller h, which means thousands more calculations. For instance, RK4 with h=0.1 might be more accurate than Euler with h=0.001. That's the efficiency trade-off.
🙋
So the "order" O(h), O(h⁴) we see... that's about how the error shrinks?
🎓
Exactly! The "order" tells you the power of h in the error. If you halve the step size (h → h/2), Euler's error roughly halves (O(h)), but RK4's error shrinks by a factor of 16 (O(h⁴)). Try it: solve a simple ODE with h=0.5, then change it to h=0.25. Watch how much closer the RK4 curve gets to the exact solution compared to Euler's. That's the power of a higher-order method.

Physical Model & Key Equations

The core problem is solving a first-order Ordinary Differential Equation (ODE) of the form $dy/dx = f(x, y)$, starting from an initial condition $y(x_0) = y_0$. We want to find $y$ at future points $x$. Since we can't always solve this analytically, we use numerical methods to approximate the solution step-by-step.

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

Here, $f(x, y)$ is the rate of change (the "slope"), $x$ is the independent variable (often time or position), and $y$ is the dependent variable we want to find (like temperature, position, or concentration).

The Forward Euler method approximates the next value using only the current slope. It's simple but accumulates error quickly.

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

$y_n$ : Approximate solution at step $n$. $h$ : Step size ($x_{n+1}- x_n$). $f(x_n, y_n)$ : Slope at the beginning of the step. The method's global error is proportional to $h$, hence it's a first-order method.

The Fourth-Order Runge-Kutta (RK4) method uses a weighted average of four slope estimates within a single step, capturing the behavior of the function much more accurately.

$$ \begin{aligned}k_1 &= f(x_n,\,y_n) \\ k_2 &= f\!\left(x_n+\tfrac{h}{2},\,y_n+\tfrac{h}{2}k_1\right) \\ k_3 &= f\!\left(x_n+\tfrac{h}{2},\,y_n+\tfrac{h}{2}k_2\right) \\ k_4 &= f\!\left(x_n+h,\,y_n+h\,k_3\right) \\ y_{n+1}&= y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4) \end{aligned} $$

$k_1$ : Slope at the start. $k_2, k_3$ : Slopes at the midpoint (using $k_1$ and $k_2$ respectively). $k_4$ : Slope at the end of the step (using $k_3$). The final update is a clever weighted average of these four slopes. Its global error is proportional to $h^4$, making it a fourth-order method.

Frequently Asked Questions

The calculation accuracy improves, but the number of calculation steps increases, leading to longer processing time. Additionally, the accumulation of floating-point errors may become non-negligible, potentially increasing the error instead. In practice, choose an appropriate h within the range where the error converges.
When the step size h is halved, the global error of the forward Euler method decreases by about half, while that of the RK4 method decreases by about 1/16. For example, with h = 0.1, the RK4 method can achieve errors several orders of magnitude smaller than the Euler method, making it suitable for applications requiring high accuracy.
For initial value problems of first-order ordinary differential equations, any function f(x, y) can be set. However, for stiff equations where the solution changes rapidly, the error may become large even with the RK4 method unless the step size is made extremely small. Implicit methods are more suitable for such problems.
Even if the exact solution is unknown, you can evaluate the accuracy by comparing calculation results with different step sizes h. For example, if halving h yields almost the same result, the solution can be considered converged. It is also possible to estimate the error from the difference between the higher-order RK4 method and the lower-order Euler method.

Real-World Applications

Orbital Mechanics & Satellite Trajectories: Calculating the path of a satellite involves solving Newton's laws of motion and gravity, which are ODEs. RK4 is a workhorse in flight dynamics software because it provides excellent accuracy for predicting positions over long periods without requiring impossibly small time steps, which would slow down simulations.

Circuit Simulation (SPICE-like software): The voltage and current in circuits with capacitors and inductors are described by ODEs. Simulators use methods like RK4 to accurately model transient responses, such as how a circuit behaves when a switch is flipped, ensuring electronic designs are stable before manufacturing.

Computational Fluid Dynamics (CFD): While the full Navier-Stokes equations are complex PDEs, the simulation often involves solving ODEs for particle trajectories or chemical reactions within the flow. RK4's accuracy is crucial for tracking pollutants or fuel mixing in engines without numerical diffusion (excessive smoothing of results).

Epidemiological Modeling: Models like SIR (Susceptible-Infected-Recovered) are systems of ODEs predicting disease spread. Public health officials use numerical solvers to forecast scenarios. Using a stable, accurate method like RK4 helps ensure predictions about infection peaks are reliable for planning hospital capacity.

Common Misconceptions and Points to Note

First, while you might think "RK4 is always better than Euler's method," that's not necessarily true. While its accuracy is indeed far superior, it requires calculating the function $f(x, y)$ four times per step, making the computational cost about four times higher. For instance, when simulating a simple oscillatory phenomenon for a short duration, using Euler's method with a sufficiently small step size can sometimes achieve accuracy comparable to RK4 in less computation time. Always consider the balance between accuracy vs. computation time.

Next, the idea that "the smaller the step size $h$, the better" is also a pitfall. While the error does decrease as theory predicts, the number of computational steps increases explosively, and round-off error begins to accumulate. Particularly with Euler's method, even if you make $h$ extremely small, you may observe a phenomenon where the error actually starts to increase beyond a certain point. If you try setting $h$ to extremely small values like 0.001 or 0.0001 in this simulator, you should be able to observe parts where the theoretical and measured results don't perfectly match.

Finally, don't limit your thinking to "this comparison only applies to one-dimensional ordinary differential equations." In practical CAE problems, such as structural vibration (systems of differential equations) or thermal convection in fluids (time evolution of partial differential equations), these methods are embedded as fundamental building blocks for time integration. The concepts behind RK4 are extended to higher-dimensional and more complex systems.