ODE Numerical Solver | Euler, Heun & Runge-Kutta RK4
Numerically solve first-order ordinary differential equations y′ = f(x, y) using three methods side-by-side: Euler's method, Heun's method (improved Euler), and the 4th-order Runge-Kutta (RK4). Shows all intermediate k₁–k₄ values for RK4 and a full step-by-step comparison table.
Use x, y, +, -, *, /, ^, sin, cos, exp, ln, sqrt
What Is the ODE Numerical Solver | Euler, Heun & Runge-Kutta RK4?
Numerical ODE solvers approximate the solution y(x) of an initial value problem by stepping forward in increments of h. Euler's method uses the slope at the start of each interval (first-order, O(h) global error). Heun's method averages the start and end slopes for second-order accuracy O(h²). The 4th-order Runge-Kutta (RK4) computes four intermediate slopes k₁–k₄ and combines them as (k₁ + 2k₂ + 2k₃ + k₄)/6, achieving O(h⁴) error per step — the industry standard for general-purpose ODE solving. All three methods are shown side-by-side so you can directly compare their accuracy.
Formula
y′ = f(x, y), y(x₀) = y₀ — solved step-by-step with Euler, Heun (Improved Euler), and RK4
How to Use
- 1
Enter f(x, y) — e.g. 'y' for exponential growth, '-y' for decay, 'x+y' for a linear ODE
- 2
Set x₀ (starting point) and y₀ (the initial value y(x₀))
- 3
Choose step size h (smaller = more accurate) and the number of steps
- 4
Click a preset to load a classic equation with a known exact solution
- 5
Click "Solve ODE" — the table shows x, Euler, Heun, and RK4 values at each step
- 6
Expand the k-values section to inspect k₁–k₄ at each RK4 step
Enter f(x, y) as a JavaScript expression, set the initial condition and step size, then click Solve to compare all three methods.
Example Calculation
y′ = y, y(0) = 1, h = 0.1, 5 steps. Exact: y = eˣ. At x = 0.5: exact = 1.64872. Euler: 1.61051 (error 2.3%). Heun: 1.64640 (error 0.14%). RK4: 1.64872 (error < 0.001%). RK4 is 2300× more accurate than Euler using only 4× as many function evaluations.
Understanding ODE Numerical | Euler, Heun & Runge-Kutta RK4
Method Comparison
| Method | Order | Function evals / step | Global error | Best used when |
|---|---|---|---|---|
| Euler | 1st | 1 | O(h) | Quick estimates, demonstration |
| Heun | 2nd | 2 | O(h²) | Moderate accuracy, low cost |
| RK4 | 4th | 4 | O(h⁴) | Most practical problems |
Classic ODEs and Their Exact Solutions
| Equation f(x,y) | Exact solution y(x) | Application |
|---|---|---|
| y′ = y | C·eˣ | Exponential growth / radioactive decay |
| y′ = -y | C·e⁻ˣ | Exponential decay / cooling |
| y′ = x | x²/2 + C | Uniformly accelerated motion |
| y′ = y*(1-y) | 1/(1+Ae⁻ˣ) | Logistic population model |
| y′ = x + y | Ce^x - x - 1 | First-order linear ODE |
| y′ = -2*x*y | Ce^(-x²) | Gaussian probability density |
Choosing the Right Step Size
- ›Smaller h always gives more accurate results, but requires more computation steps.
- ›As a rule of thumb for RK4, h ≤ 0.1 gives engineering-grade accuracy for smooth functions.
- ›For Euler, you typically need h ≤ 0.01 to achieve similar accuracy to RK4 at h = 0.1.
- ›Very small h (< 10⁻⁶) can cause floating-point accumulation errors to dominate.
- ›For stiff ODEs (where solutions vary on very different time scales), use implicit solvers not shown here.
Frequently Asked Questions
Which method is most accurate?
RK4 is the most accurate, with O(h⁴) global error. For a step size of h = 0.1 this is typically 10,000× more accurate than Euler (O(h) error) and 100× more accurate than Heun (O(h²)).
What functions can I use in f(x, y)?
You can use x, y, and any standard Math function: sin, cos, exp, log, sqrt, abs, pow(a,b), PI, E, and standard arithmetic operators + - * / ().
What is a stiff ODE?
A stiff ODE has solutions with very different time scales, making explicit methods like Euler and RK4 require very small h for stability. Implicit methods (backward Euler, Crank-Nicolson) are preferred for stiff problems.
Can I solve second-order ODEs?
Not directly. Reduce y″ = g(x, y, y′) to a system by substituting z = y′, so z′ = g(x, y, z). Solve the first-order system for both y and z simultaneously.
What do k₁, k₂, k₃, k₄ mean in RK4?
k₁ is the slope at the step start; k₂ and k₃ are slopes at the midpoint using k₁ and k₂ respectively; k₄ is the slope at the step end using k₃. Their weighted average (1/6, 1/3, 1/3, 1/6) gives a 4th-order accurate step.
You Might Also Like
Explore 360+ Free Calculators
From math and science to finance and everyday life — all free, no account needed.