Skip to content

Differential Equation Calculator (Numeric ODE Solver)

Solve y′ = f(x, y) and second-order ODEs numerically with Euler's method, improved Euler (Heun) or Runge–Kutta 4: step table, solution curve and slope field.

Calculator Details

y at the end:…See results ↓

First order y′ = f(x, y), or second order y″ = f(x, y, y′), solved as a system of two first-order equations

Use x, y (and y′, typed y' or yp, for second order); + − * / ^, sin, cos, exp, ln, sqrt, pi, e. 2y means 2·y

Euler (first order accurate), improved Euler / Heun (second order) or classical Runge–Kutta (fourth order)

Where the initial condition is given

The value of y at x₀

Width of each step; the last step is shortened to land on the end (at most 10,000 steps)

The x where you want y; it can be left of x₀ (the steps then run backwards)

Your Results

y at the end: …

y at the end

…

Loading the solver…

Compare scenarios

Save these inputs as a scenario, change them, and compare the results side by side.

Loading, available in a moment

Detailed Breakdown

How This Calculator Works

How it's calculated

A first-order initial-value problem y′ = f(x, y), y(x₀) = y₀ gives the slope of the solution at every point. A numeric method starts at (x₀, y₀) and takes steps of width h. Euler's method follows the slope at the start of the step: y₁ = y₀ + h·f(x₀, y₀). Improved Euler (Heun) averages the slope at the start, k₁, with the slope at the Euler prediction, k₂ = f(x₀ + h, y₀ + h·k₁): y₁ = y₀ + h(k₁ + k₂)/2. Runge–Kutta 4 takes four slopes, at the start, twice at the midpoint and at the end, and weights them 1, 2, 2, 1: y₁ = y₀ + h(k₁ + 2k₂ + 2k₃ + k₄)/6. Their errors at a fixed x shrink like h, h² and h⁴.

A second-order equation y″ = f(x, y, y′) becomes two first-order ones, y′ = v and v′ = f(x, y, v), stepped together with the same method. When f is linear with constant coefficients the calculator also solves the equation exactly (an integrating factor for first order, the characteristic equation r² − A·r − B = 0 for second order) and compares.

Frequently Asked Questions

Does this calculator solve differential equations exactly?

It solves them numerically: it steps from the initial condition in small increments and gives a table of approximate values. For constant-coefficient linear equations it also finds the exact solution and shows the error of each step; for y′ = x + y, y(0) = 1 that is y = 2·e^(x) − x − 1. Other equations get no formula, only the numeric solution, which is often all that exists.

What is Euler's method?

The simplest numeric method: from a point (x, y), follow the slope f(x, y) for one step, y₁ = y₀ + h·f(x₀, y₀), and repeat. Its error at a fixed x is roughly proportional to h. For y′ = x + y from y(0) = 1 to x = 1, h = 0.1 gives 3.18748492 against the exact 3.436563657; halving h to 0.05 gives 3.30659541, about half the error.

Which method should I use?

Runge–Kutta 4 for accuracy: it samples the slope four times per step and its error falls with h⁴. On the same problem with h = 0.1, Euler is off by 2.5e−1, improved Euler (Heun) by 8.4e−3 and RK4 by 4.2e−6. Euler and Heun are there because courses teach them and to show how the error shrinks.

How do I choose the step size h?

Smaller steps are more accurate but take more of them. A good check is to halve h and see whether the answer changes in the digits you care about. With RK4 on y′ = x + y, h = 0.1 gives 3.436559488 and h = 0.05 gives 3.436563385: the error drops by a factor of about 16. If h does not divide the interval evenly, the last step is shortened so the solution lands exactly on the end point.

How do I enter a second-order equation?

Choose "Second order" and type the right-hand side of y″ = f(x, y, y′), writing y′ as y' or yp, then give y(x₀) and y′(x₀). The calculator rewrites it as two first-order equations, y′ = v and v′ = f(x, y, v), and steps both together. For y″ = −y with y(0) = 1, y′(0) = 0, RK4 gives y(1) ≈ 0.5403029671; the exact solution is cos x, and cos 1 = 0.5403023059.

What is a slope field?

A slope field (direction field) draws a short line at each point of a grid with slope f(x, y): the direction a solution through that point would take. Solution curves follow the lines, so the field shows the behaviour of every solution at once, including equilibria and where solutions grow or decay. The calculator draws it for first-order equations with your numeric solution on top.

Why does the calculator say the solution blows up?

Some solutions become infinite at a finite x. y′ = y² with y(0) = 1 has the solution y = 1/(1 − x), which is infinite at x = 1, so no step size can carry it past there. The calculator stops when y is no longer a finite real number (or leaves the domain of a function such as sqrt or ln) and says where.

Can I solve backwards from the initial condition?

Yes: set "Solve to x" left of x₀. The steps then run with a negative h. Some equations that are stable forwards are unstable backwards (and the other way round), so check the result by halving h.

Numeric solutions only: Euler, improved Euler (Heun) and classical fourth-order Runge–Kutta with a fixed step, in double precision, shown to 10 significant figures. The exact column appears only for constant-coefficient linear equations (y′ = a·y + b·x + c, or y″ = A·y′ + B·y + C), found by testing f at sample points. Trig is in radians; log means base 10 and ln the natural log. Not included: symbolic (closed-form) solutions of other equations, adaptive step-size control, stiff solvers, boundary-value problems, systems of more than one unknown function, partial differential equations, and equations above second order.

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.

  1. 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.
  2. 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.
  3. 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

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.

Spotted a wrong result or a bug? Report a problem

May we count your visits with Invoker Analytics? It keeps a random visitor number in your browser. Your calculator inputs are never sent. Privacy policy