DigitHelm

Differential Equation Solver | ODE

Solve first-order ODEs numerically using Euler, Heun, and RK4. Compare method accuracy step by step.

Use x for the independent variable and y for the state. Supports: sin cos tan exp ln sqrt abs ^ * /, and constants pi, e.

Enter = solve · Esc = reset

What Is the Differential Equation Solver | ODE?

This solver numerically integrates a first-order ordinary differential equation of the form dy/dx = f(x, y) given an initial condition y(x₀) = y₀. It runs three methods side by side, Euler, Heun (Improved Euler), and the classic 4th-order Runge-Kutta (RK4), so you can directly compare their accuracy for the same step size.

  • Three methods simultaneously, compare Euler (O(h)), Heun (O(h²)), and RK4 (O(h⁴)) accuracy without re-entering data.
  • Visual solution chart, all three curves plotted against x with automatic y-axis scaling so differences are visible at a glance.
  • Error comparison bar, shows the deviation of Euler and Heun from RK4 (used as a reference), with a progress bar to visualise relative magnitudes.
  • Step-by-step table, collapsible table showing x, Euler y, Heun y, and RK4 y at every step (or every Nth step for large n).
  • Seven built-in presets, exponential decay/growth, logistic, linear non-autonomous, driven oscillator, separable, and stiff ODE; each loads with one click.
  • localStorage persistence, inputs are saved to your browser automatically; your last ODE is restored when you return.

Formula

Euler Method (1st-order)

yn+1 = yn + h · f(xn, yn)

Local truncation error: O(h²), global error: O(h)

Heun Method / Improved Euler (2nd-order)

k₁ = f(xn, yn)

k₂ = f(xn + h, yn + h·k₁)

yn+1 = yn + (h/2)·(k₁ + k₂)

Local truncation error: O(h³), global error: O(h²)

Runge-Kutta RK4 (4th-order)

k₁ = f(xn, yn)

k₂ = f(xn + h/2, yn + h·k₁/2)

k₃ = f(xn + h/2, yn + h·k₂/2)

k₄ = f(xn + h, yn + h·k₃)

yn+1 = yn + (h/6)·(k₁ + 2k₂ + 2k₃ + k₄)

Local truncation error: O(h⁵), global error: O(h⁴)

SymbolNameMeaning
dy/dx = f(x,y)ODE right-hand sideThe function that defines the rate of change, what you enter into the calculator
xIndependent variableThe variable you integrate over, often time t or distance
yDependent variableThe unknown solution you are solving for
x₀Initial xStarting value of the independent variable
y₀ = y(x₀)Initial conditionThe known value of y at x = x₀; seeds the entire numerical solution
hStep sizeh = (x_end − x₀) / n, smaller h gives higher accuracy but more computation
nNumber of stepsTotal iterations from x₀ to x_end; more steps reduce global error
k₁…k₄RK4 slope estimatesFour slope evaluations at different sub-step points; combined for 4th-order accuracy

Method accuracy comparison

MethodSlope evals / stepGlobal error orderCut h in half →
Euler1O(h)error halves
Heun2O(h²)error quarters
RK44O(h⁴)error drops 16×

How to Use

  1. 1
    Choose a preset (optional): Select one of the seven presets to load a classic first-order ODE. This sets f(x,y), x₀, y₀, x_end, and the step count instantly.
  2. 2
    Enter f(x, y): Type the right-hand side of your ODE. Use x for the independent variable and y for the dependent variable. Example: for dy/dx = −2y + sin(x), enter -2*y + sin(x).
  3. 3
    Set the initial condition: Enter x₀ (where the solution starts) and y₀ = y(x₀) (the known value at that point). Together these form the Initial Value Problem (IVP).
  4. 4
    Set x_end: Enter the point at which you want the solution. The solver integrates from x₀ to x_end.
  5. 5
    Choose step count n: Start with n = 20–50 for a quick overview. Increase n (try 200 or 500) to see how the three methods converge to the same answer as h = (x_end − x₀) / n decreases.
  6. 6
    Press Enter or click Solve ODE: Results appear immediately: a problem summary, final values for all three methods, an error comparison bar, a solution chart, and a step table.
  7. 7
    Read the error comparison: The error bars show |Euler − RK4| and |Heun − RK4|. RK4 is used as the reference. For most smooth ODEs, Heun is significantly more accurate than Euler with the same h.

Example Calculation

Exponential decay: dy/dx = −y,  y(0) = 1

This is the simplest first-order ODE with the exact solution y = e−x. We integrate from x = 0 to x = 1 using h = 0.25 (n = 4 steps) to make the arithmetic visible.

f(x, y) = −y, x₀ = 0, y₀ = 1, x_end = 1, n = 4 → h = 0.25

── Euler ──

y₁ = 1 + 0.25·(−1) = 0.75000

y₂ = 0.75 + 0.25·(−0.75) = 0.56250

y₃ = 0.5625 + 0.25·(−0.5625) = 0.42188

y₄ = 0.42188 + 0.25·(−0.42188) = 0.31641

── RK4 ──

Step 1 at x = 0, y = 1:

k₁ = −1, k₂ = −0.875, k₃ = −0.875, k₄ = −0.75

y₁ = 1 + (0.25/6)·(−1 − 1.75 − 1.75 − 0.75) = 0.77880

(remaining steps omitted for brevity)

y₄ ≈ 0.36789

Exact: y(1) = e⁻¹ ≈ 0.36788

Euler error: |0.31641 − 0.36788| = 0.05147 (14.0%)

RK4 error: |0.36789 − 0.36788| < 0.00001 (<0.003%)

xEulerRK4Exact e^(−x)|RK4 − Exact|
0.001.000001.000001.000000.00000
0.250.750000.778800.77880< 0.00001
0.500.562500.606530.60653< 0.00001
0.750.421880.472370.47237< 0.00001
1.00 ★0.316410.367890.367880.00001

Key insight from this example

With only 4 steps Euler accumulates 14% error. RK4 achieves 5 significant figures with the same step size , because its local error is O(h⁵) rather than O(h²). Doubling n to 8 steps would cut Euler's error roughly in half but would cut RK4's error by a factor of ~32.

Understanding Differential Equation | ODE

What Is a First-Order ODE?

An ordinary differential equation (ODE) relates a function y(x) to its own derivative. A first-order ODE involves only y and y′ = dy/dx, no higher derivatives. Written in standard form it looks like:

dy/dx = f(x, y)

To get a unique solution you also need an initial condition: a known value y(x₀) = y₀ that anchors the solution at a specific point. The combination of an ODE plus an initial condition is called an Initial Value Problem (IVP), which is exactly what this calculator solves.

Not all first-order ODEs have closed-form solutions. For example, dy/dx = sin(y²) cannot be solved with elementary functions. Numerical methods sidestep the requirement for a closed form by approximating the solution one small step at a time.

The Euler Method, Intuition and Limitations

Euler's method (1768) is the simplest possible numerical integrator. At each step it takes a single linear step in the direction of the current slope:

yn+1 = yn + h · f(xn, yn)

The idea is identical to following a tangent line for a short distance. Because the tangent is only correct at the current point, the method drifts away from the true solution. The global error at a fixed endpoint grows proportionally to h, halving the step size halves the error. This 1st-order accuracy makes Euler fast to implement but impractical for precision work without very many steps.

  • Simple: only one function evaluation per step.
  • Conditionally stable: requires h < 2/|λ| for stiff ODEs (e.g. dy/dx = −10y), otherwise diverges.
  • Good for understanding numerical integration; rarely used in production numerical solvers.
  • The "explicit" or "forward" Euler method, there is also an implicit (backward) variant better suited to stiff equations.

Heun's Method, A Cheap 2nd-Order Fix

Heun's method (also called the Improved Euler or Euler-Trapezoidal method) uses two slope evaluations to achieve 2nd-order accuracy at the cost of one extra function call per step:

  • Predictor step: compute a predicted ỹ using Euler.
  • Corrector step: evaluate f at the predicted point to get a second slope estimate.
  • Average the two slopes and step from y_n using the average.
  • Global error scales as O(h²), cutting h in half reduces error 4×.

This makes Heun substantially more accurate than Euler with only one additional f-evaluation. It belongs to the family of Runge-Kutta methods of order 2, RK2, alongside the midpoint method (which evaluates f at the midpoint of the interval instead of the endpoint).

Runge-Kutta RK4, The Workhorse of Numerical ODEs

The classical 4th-order Runge-Kutta method evaluates f at four carefully chosen points within each step and combines the results with weights derived from Simpson's rule:

k₁ = f(xn, yn)

k₂ = f(xn + h/2, yn + h·k₁/2)

k₃ = f(xn + h/2, yn + h·k₂/2)

k₄ = f(xn + h, yn + h·k₃)

yn+1 = yn + (h/6)·(k₁ + 2k₂ + 2k₃ + k₄)

The global error is O(h⁴), cutting h in half reduces error by a factor of 16. For most smooth non-stiff ODEs, RK4 gives excellent accuracy with n = 50–200 steps. It remains the method of choice for one-off integrations because it requires no special setup, handles any smooth f, and the code is straightforward.

Step Size, Accuracy, and Stability

Choosing the right step size h = (x_end − x₀) / n involves a tradeoff:

  • Too large h: truncation error dominates, the numerical solution drifts far from the true curve.
  • Too small h: round-off error accumulates (many additions of small numbers) and computation time grows.
  • Stiff ODEs: a very negative derivative (e.g. dy/dx = −100y) requires h < 0.02 for Euler stability but RK4 can use h = 0.2 safely.
  • Rule of thumb: start with n = 50 and double until the final value stops changing significantly, that is Richardson extrapolation in spirit.

Stiff ODEs and stability

A stiff ODE contains components with very different time scales. The classic example is dy/dx = −λy with large λ, the solution decays quickly but you are integrating over a long interval. Explicit methods like Euler and RK4 require h < 2/λ to remain stable, forcing very many steps. Implicit methods (Backward Euler, Crank-Nicolson, BDF) are unconditionally stable but require solving an algebraic equation at each step. This calculator uses explicit RK4; for highly stiff problems, use MATLAB's ode15s or Python's scipy.integrate.solve_ivp with method='BDF'.

Interpreting the Error Comparison

The calculator displays the absolute difference between each method and RK4 at the final point x_end. This is not a true error (we do not have the exact solution) but it is an excellent proxy when the three methods agree closely, because RK4's 4th-order accuracy makes it far more reliable than Euler or Heun for typical step sizes.

  • If Euler and RK4 differ by less than 0.01%, all three methods have converged, the step size is small enough.
  • If Euler error is large but Heun error is small, the ODE is smooth and Heun is sufficient; RK4 is overkill.
  • If all three methods disagree significantly, reduce h (increase n) until they converge.
  • If results diverge toward ±∞, you have a singular or stiff ODE, check the function near the integration interval.

Real-World Applications

FieldODE modelf(x, y)
Population dynamicsExponential growthk·y
Radioactive decayFirst-order decay−λ·y
EpidemiologySIR model (S equation)−β·y·I (per susceptibles)
Engineering (circuits)RC charging(V₀ − y) / (RC)
PharmacokineticsDrug elimination−ke·y
EcologyLogistic growthr·y·(1 − y/K)
ThermodynamicsNewton cooling−k·(y − T_ambient)
MechanicsVelocity with dragg − (b/m)·y²

Frequently Asked Questions

What type of differential equations can this solver handle?

This solver handles explicit first-order ODEs:

dy/dx = f(x, y),  y(x₀) = y₀

What it can solve:

  • Separable equations: dy/dx = g(x)·h(y), e.g. -y, y*(1-y/K)
  • Linear first-order: dy/dx = p(x)·y + q(x), e.g. -2*y + sin(x)
  • Autonomous equations: dy/dx = f(y) only (no explicit x)
  • Any smooth function of x and y within the supported syntax

What it cannot solve:

  • 2nd-order ODEs (y″ + y = 0), needs system conversion first
  • Boundary value problems (BVPs) where conditions are given at two endpoints
  • Implicit ODEs where y′ is not isolated: F(x, y, y′) = 0
  • Partial differential equations (PDEs)

How do I enter f(x, y) if my ODE uses t instead of x?

The calculator uses x for the independent variable (often called t in physics and engineering). Simply replace t with x in your expression:

  • dy/dt = −0.5y + sin(t) → enter -0.5*y + sin(x)
  • dy/dt = t·y → enter x*y
  • dy/dt = e^(−t)·(1 + y²) → enter exp(-x)*(1 + y^2)

The initial condition fields x₀ and x_end correspond directly to t₀ and t_end in your problem. The variable y always refers to the current value of the dependent variable.

Why does increasing the number of steps improve accuracy?

Numerical ODE methods approximate a continuous curve with discrete steps. The global error at x_end depends on the step size h = (x_end − x₀) / n:

  • Euler: global error ≈ C·h, halving h halves the error.
  • Heun: global error ≈ C·h², halving h quarters the error.
  • RK4: global error ≈ C·h⁴, halving h reduces error 16×.

The constant C depends on the smoothness of f. Experiment: try n = 10, then n = 100, then n = 1000. When the final value stabilises (e.g. to 6 significant figures regardless of n), you have converged to the true answer.

What is the difference between Euler, Heun, and RK4?

All three methods compute yn+1 from yn alone (single-step, explicit):

  • Euler: one slope evaluation at (x_n, y_n). Simple, fast, 1st-order accurate.
  • Heun: two evaluations, one at the start, one at a predicted end point. Averages them. 2nd-order.
  • RK4: four evaluations, start, two midpoints, end, weighted like Simpson's rule. 4th-order.

In practice: use RK4 for most problems. Use Heun when f is expensive to evaluate and moderate accuracy (1% error) is acceptable. Euler is mainly for teaching, it is almost never the right choice in production code.

What happens if the solver diverges or gives Infinity?

Divergence (result → ∞ or NaN) has three common causes:

  • Singularity in f, e.g. dy/dx = 1/y passes through y = 0; avoid integrating through the singularity.
  • Euler instability, for stiff ODEs like dy/dx = −10y, Euler requires h < 0.2 or it oscillates and diverges. Increase n dramatically or switch to RK4.
  • True blow-up, dy/dx = y² with y(0) = 1 has a solution y = 1/(1−x) that diverges at x = 1. The solver will return very large values as x_end → 1.

Remedies: reduce x_end, increase n significantly, check the function for division by zero, and confirm the ODE has a solution in the integration interval.

How is this different from an analytical (exact) solution?

The key differences:

  • Analytical: gives a formula (e.g. y = e^(−x)), exact for any x, no discretisation error.
  • Numerical: gives a list of (x, y) pairs, small error at each point, controlled by step size.
  • Analytical methods only work for special classes of ODE (separable, linear, exact, etc.).
  • Numerical methods work for any f that can be evaluated, but produce approximations, not formulas.
  • For engineering and simulation, numerical solutions are almost always sufficient and far more practical.

Does the calculator save my inputs between sessions?

Yes, all five inputs are persisted to browser localStorage automatically:

  • Function f(x, y), your expression is saved verbatim
  • Initial conditions x₀ and y₀, restored on next visit
  • End point x_end and step count n, restored on next visit
  • Data stays in your browser only, no server communication

Click Reset to wipe the form and clear the localStorage entry for this calculator.

Can I solve a second-order ODE like y″ + 2y′ + y = 0?

Second-order ODEs require a system reduction before using this calculator:

Given: y″ + 2y′ + y = 0, y(0) = 1, y′(0) = 0

Let v = y′. Then v′ = y″ = −2v − y

System: dy/dx = v, dv/dx = −2v − y

This two-equation system cannot be solved by this tool in its current form. For systems, use:

  • Python: scipy.integrate.solve_ivp(fun, t_span, y0), returns arrays for each state variable
  • MATLAB: ode45(@(t,y) [y(2); -2*y(2)-y(1)], [0 5], [1;0])
  • Wolfram Alpha: "solve y'' + 2y' + y = 0, y(0)=1, y'(0)=0"

Related Calculators