Math
Differential equation calculator
Solve an initial value problem. For a general first-order equation y′ = f(x, y) the calculator steps along numerically with RK4 and Euler’s method; for constant-coefficient linear equations it gives the exact solution. Each result says which kind it is.
Use x and y. ^ for powers; sin, cos, exp, ln, sqrt, e and pi work.
━ RK4 ┄ Euler
| x | Euler y | RK4 y |
|---|---|---|
| 0 | 1 | 1 |
| 0.1 | 1.1 | 1.110341667 |
| 0.2 | 1.22 | 1.242805142 |
| 0.3 | 1.362 | 1.399716994 |
| 0.4 | 1.5282 | 1.58364848 |
| 0.5 | 1.72102 | 1.797441277 |
| 0.6 | 1.943122 | 2.044235924 |
| 0.7 | 2.1974342 | 2.327503253 |
| 0.8 | 2.4871776 | 2.651079127 |
| 0.9 | 2.8158954 | 3.019202828 |
| 1 | 3.1874849 | 3.436559488 |
Show the working, step by step
Step size h = (x₁ − x₀) ÷ n.
h = (1 − 0) ÷ 10 = 0.1
Euler's method follows the slope at the start of each step: yₙ₊₁ = yₙ + h·f(xₙ, yₙ). First step:
y₁ = 1 + 0.1·f(0, 1) = 1 + 0.1·1 = 1.1
RK4 averages four slopes across the step, weighting the middle two double: yₙ₊₁ = yₙ + h(k₁ + 2k₂ + 2k₃ + k₄)/6. First step:
k₁ = f(0, 1) = 1 k₂ = f(x₀ + h/2, y₀ + h·k₁/2) = 1.1 k₃ = f(x₀ + h/2, y₀ + h·k₂/2) = 1.105 k₄ = f(x₀ + h, y₀ + h·k₃) = 1.2105 y₁ = 1 + 0.1·(1 + 2·1.1 + 2·1.105 + 1.2105)/6 = 1.110341667
Repeat for all 10 steps.
RK4: y(1) ≈ 3.436559488 Euler: y(1) ≈ 3.18748492
Estimate the RK4 error by repeating with 20 steps; RK4's error falls about 16-fold when h halves.
with 20 steps: 3.436563385; estimated error ≈ 4.2e-6
These are numerical approximations, not an exact formula. Euler's error shrinks in proportion to h; RK4's in proportion to h⁴, which is why it is far more accurate for the same number of steps.
Numerical solution: Euler and RK4
Starting from (x₀, y₀), both methods take steps of size h = (x₁ − x₀)/n.
Euler: yₙ₊₁ = yₙ + h·f(xₙ, yₙ) RK4: 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
A worked example
The default problem is y′ = x + y with y(0) = 1, stepped to x = 1 in 10 steps, so h = 0.1.
- Euler’s first step: y₁ = 1 + 0.1·(0 + 1) = 1.1.
- RK4’s first step: k₁ = 1, k₂ = f(0.05, 1.05) = 1.1, k₃ = f(0.05, 1.055) = 1.105, k₄ = f(0.1, 1.1105) = 1.2105, so y₁ = 1 + 0.1·(1 + 2.2 + 2.21 + 1.2105)/6 = 1.1103417.
- After ten steps, RK4 gives y(1) ≈ 3.4365595 and Euler gives 3.1874849.
This equation happens to have the exact solution y = 2eˣ − x − 1, so y(1) = 2e − 2 = 3.4365637. RK4 is within 0.0000042 of it; Euler is 0.249 short. That gap is typical, and it is why RK4 is the default method in practice.
Second-order linear equations with constant coefficients
For ay″ + by′ + cy = 0, try y = e^(rx). It works when r solves the characteristic equation ar² + br + c = 0.
| b² − 4ac | Roots | General solution |
|---|---|---|
| > 0 | real r₁ ≠ r₂ | y = C₁e^(r₁x) + C₂e^(r₂x) |
| = 0 | repeated r | y = (C₁ + C₂x)e^(rx) |
| < 0 | α ± βi | y = e^(αx)(C₁cos βx + C₂sin βx) |
The initial values y(x₀) and y′(x₀) fix C₁ and C₂. With the default a = 1, b = 3, c = 2 the roots are −1 and −2; with y(0) = 1 and y′(0) = 0 the solution is y = 2e^(−x) − e^(−2x), and y(1) = 0.6004236.
First-order linear equations
For y′ + p·y = q with constants p ≠ 0 and q, multiply by the integrating factor e^(px). The solution is y = q/p + (y₀ − q/p)·e^(−p(x − x₀)). When p > 0 every solution settles to the equilibrium q/p: Newton’s law of cooling and a draining tank both have this form. With the defaults p = 2, q = 4 and y(0) = 1, y = 2 − e^(−2x), and y(1) = 1.8646647.
Common mistakes
- Treating a numerical answer as exact. Always look at the error estimate, or rerun with more steps.
- Stepping through a blow-up. y′ = y² with y(0) = 1 has the solution 1/(1 − x), which is infinite at x = 1; no step size gets past it.
- Forgetting the second initial condition. A second-order equation needs both y(x₀) and y′(x₀).
Common questions
Which answers are exact and which are approximate?
The two linear modes (ay″ + by′ + cy = 0, and y′ + p·y = q with constant p and q) give the exact solution formula. The y′ = f(x, y) mode is numerical: it approximates y at a grid of x values, and the answer carries a small error that the page estimates for you. It does not produce a formula for y.
Why is RK4 so much more accurate than Euler’s method?
Euler’s method uses only the slope at the start of each step, so its error shrinks in proportion to the step size h. RK4 samples the slope four times per step and combines them, and its error shrinks like h⁴. Halving h cuts Euler’s error roughly in half but RK4’s by about 16 times. For y′ = x + y with 10 steps, Euler is off by 0.25 at x = 1 and RK4 by 0.000004.
How do I choose the number of steps?
Start with 10 to 100 and look at the RK4 error estimate, which comes from repeating the calculation with twice as many steps. If it is small enough for your purpose, you are done. If the solution changes quickly (a stiff or rapidly growing equation), use more steps.
What do the three cases of the characteristic equation mean physically?
For a damped spring my″ + cy′ + ky = 0: two real roots is overdamping (it creeps back to rest without oscillating), a repeated root is critical damping (the fastest return without overshoot), and complex roots are underdamping (it oscillates with a shrinking amplitude, at angular frequency β).
Can it solve y″ + y = sin(x) or other non-homogeneous equations?
Not exactly: the exact second-order solver covers the homogeneous case (= 0). A second-order equation can be rewritten as two first-order ones and solved numerically, but this page’s numerical mode handles single first-order equations y′ = f(x, y).
Related calculators
-
Derivative
Check a solution by differentiating it and substituting.
-
Integral
y′ = f(x) is solved by integrating f.
-
Quadratic equation
Solve the characteristic equation ar² + br + c = 0.
-
Partial derivative
Derivatives of functions of several variables.