Differential Equation Calculator: Euler, Improved Euler and Runge–Kutta 4
By FindMySum · Last reviewed
A differential equation calculator finds the function that satisfies an equation involving its own derivative. This one does it numerically: you give the equation, a starting point and a step size, and it walks the solution forward one step at a time with Euler's method, the improved Euler (Heun) method or the classical fourth-order Runge–Kutta method. It shows every step in a table, draws the solution curve and the slope field, and runs all three methods side by side so you can see how much more accurate the higher-order ones are.
Numeric methods work for almost any equation you can type, including the many that have no solution in closed form. For linear equations with constant coefficients the calculator also finds the exact solution, so the error of each step is visible.
What the equation means
A first-order differential equation y′ = f(x, y) says that at every point (x, y), a solution passing through that point has slope f(x, y). On its own the equation describes a family of curves. An initial condition, y(x₀) = y₀, picks one of them. Together they form an initial-value problem, and that is what this calculator solves.
A second-order equation y″ = f(x, y, y′) involves the second derivative, the curvature or acceleration. It needs two starting values, y(x₀) and y′(x₀). The standard trick is to call the derivative a new unknown, v = y′, which turns one second-order equation into two first-order ones: y′ = v and v′ = f(x, y, v). Every method below then steps the pair (y, v) together.
The three methods
Euler's method is the simplest possible idea. Standing at (xₙ, yₙ), work out the slope f(xₙ, yₙ) and follow it in a straight line for a distance h:
yₙ₊₁ = yₙ + h·f(xₙ, yₙ)
The error made in one step is proportional to h², and there are about 1/h steps, so the error at a fixed x is proportional to h. Euler's method is first order: halving the step halves the error.
Improved Euler (Heun's method) first takes an Euler step to predict where the solution will be, works out the slope there, and then averages the two slopes:
k₁ = f(xₙ, yₙ), k₂ = f(xₙ + h, yₙ + h·k₁), yₙ₊₁ = yₙ + h(k₁ + k₂)/2
It is the trapezoid rule applied to the differential equation, and it is second order: halving h divides the error by about 4.
Classical Runge–Kutta (RK4) samples the slope four times per step, at the start, twice at the midpoint and at the end, and combines them with weights 1, 2, 2, 1:
k₁ = f(xₙ, yₙ), k₂ = f(xₙ + h/2, yₙ + h·k₁/2), k₃ = f(xₙ + h/2, yₙ + h·k₂/2), k₄ = f(xₙ + h, yₙ + h·k₃)
yₙ₊₁ = yₙ + h(k₁ + 2k₂ + 2k₃ + k₄)/6
When f depends only on x, this reduces to Simpson's rule. RK4 is fourth order: halving h divides the error by about 16. It is the workhorse fixed-step method in engineering and physics courses because it balances accuracy against the number of function evaluations.
Worked example: y′ = x + y, y(0) = 1
The calculator opens with this problem, solved to x = 1 with h = 0.1, so it takes 10 steps.
- Exact solution. The equation is linear: y′ − y = x. With the integrating factor e⁻ˣ, (y·e⁻ˣ)′ = x·e⁻ˣ, which integrates to y = −x − 1 + C·eˣ. The condition y(0) = 1 gives C = 2, so y = 2eˣ − x − 1 and y(1) = 2e − 2 ≈ 3.436564.
- Euler's first step. The slope at (0, 1) is 0 + 1 = 1, so y₁ = 1 + 0.1 × 1 = 1.1 at x = 0.1. The exact value there is 1.110342, already 0.010 away.
- After 10 steps. Euler reaches about 3.187485, an error of about −0.249. Heun reaches 3.428162, an error of about −0.0084. RK4 reaches 3.436559, an error of about −4.2 × 10⁻⁶.
That spread, from two decimal places to six with the same step size, is the whole reason higher-order methods exist. To get Euler's error down to RK4's level you would need a step about 60,000 times smaller.
Reading the slope field
For a first-order equation the calculator draws a slope field: a grid of short line segments, each with the slope f(x, y) at its centre. You can see the shape of every solution at once. Where the segments are flat the solutions level off; where they point steeply up or down the solutions change fast. Curves where the segments are all horizontal, such as y = 0 for y′ = −2y, are equilibrium solutions. Your numeric solution is drawn on top and should follow the segments; if it cuts across them, the step size is too large.
Choosing the step size
There is no single right step size. A smaller h gives a more accurate answer but takes more steps, and with very small steps rounding errors eventually start to grow. The practical test is to halve h and compare. If the digits you care about don't change, the step is small enough. The calculator shortens the final step if h doesn't divide the interval exactly, so the solution always ends at the x you asked for.
Some equations punish large steps more than others. A fast-decaying equation such as y′ = −50y makes Euler's method oscillate and grow when h is above 0.04, even though the true solution shrinks to zero. That is numerical instability, and it is the hallmark of stiff problems.
When the solution blows up
Not every solution exists for all x. The equation y′ = y² with y(0) = 1 has the solution y = 1/(1 − x), which goes to infinity at x = 1. No numeric method can step past that point. The calculator stops as soon as y stops being a finite real number, or when f leaves its domain (for example the square root of a negative number), and tells you where it happened.
Second-order equations and the exact check
Second-order linear equations with constant coefficients, y″ = A·y′ + B·y + C, describe springs, pendulums at small angles and RLC circuits. Their exact solutions come from the characteristic equation r² − A·r − B = 0. Two distinct real roots give a sum of exponentials (an overdamped system), a repeated root gives (c₁ + c₂x)·eʳˣ (critically damped), and complex roots α ± βi give e^(αx)(c₁cos βx + c₂sin βx), an oscillation that decays when α is negative. The calculator detects this form by evaluating f at test points and shows the exact solution next to the numeric one.
For y″ = −y with y(0) = 1 and y′(0) = 0, the exact solution is cos x. RK4 with h = 0.1 gives y(1) ≈ 0.5403030 against cos 1 ≈ 0.5403023.
Limits of this calculator
Everything here is a fixed-step, explicit method. Professional solvers adjust the step size automatically from an error estimate, and stiff problems need implicit methods; neither is included. The calculator handles one unknown function, of first or second order, with an initial condition, not boundary-value problems (conditions at two different points) or partial differential equations. For an equation without a closed form, the numeric table is the answer, and halving the step is the check.
More questions
What is an initial-value problem?
A differential equation together with the value of the unknown function at one point, such as y′ = x + y with y(0) = 1. The equation alone has a whole family of solutions; the initial condition picks out one of them. A second-order equation needs two conditions, usually y and y′ at the same starting x.
What does the order of a numeric method mean?
It describes how fast the error shrinks as the step gets smaller. A method of order p has a global error roughly proportional to hᵖ, so halving h divides the error by about 2ᵖ: by 2 for Euler (order 1), 4 for Heun (order 2) and 16 for classical Runge–Kutta (order 4).
Why does Euler's method drift away from the true solution?
Each step follows the slope at the start of the step as if it stayed constant. When the solution curves, the straight step misses a little, and the next step starts from that slightly wrong point. These local errors add up over many steps, which is why Euler's error at a fixed x is proportional to h rather than to h².
What is a stiff differential equation?
One where some part of the solution decays much faster than the part you care about, such as y′ = −1000(y − cos x). Explicit methods like Euler and RK4 then need a tiny step to stay stable, even after the fast part has died away. Stiff problems call for implicit methods such as backward Euler or BDF, which this calculator doesn't include.
Can a differential equation have no solution or more than one?
Yes. If f and its partial derivative with respect to y are continuous near the initial point, the Picard–Lindelöf theorem guarantees exactly one solution nearby. Without that, things can go wrong: y′ = 3y^(2/3) with y(0) = 0 has both y = 0 and y = x³ as solutions. A numeric method will quietly follow one of them.
Is RK4 the same as the method used by MATLAB's ode45?
No. ode45 uses the Dormand–Prince pair, an embedded Runge–Kutta method of orders 4 and 5 that estimates its own error and changes the step size as it goes. Classical RK4 uses a fixed step and gives no error estimate; you check it by halving h.
Sources
- OpenStax Calculus Volume 2, Section 4.2: Direction Fields and Numerical Methods
- OpenStax Calculus Volume 2, Section 4.5: First-order Linear Equations
- OpenStax Calculus Volume 3, Section 7.1: Second-Order Linear Equations
- Butcher, J. C. (2016). Numerical Methods for Ordinary Differential Equations, 3rd ed. Wiley
This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.