What this calculator does
This tool numerically solves a second-order ordinary differential equation written in the form \(y'' = F(x, y, y')\) over an interval [x0, xn], using the second-order Runge-Kutta method (the midpoint/modified-Euler RK2 scheme). You provide the right-hand side F as an expression in x, y and p (where p stands for y'), the initial values y(x0) and y'(x0), the interval endpoints and the number of steps. The result is a table of (x, y, y') values marching across the interval. This is pure numerical analysis, so it applies identically anywhere.
How to use it
Type the forcing function F using x, y and p, for example -4*p-4*y for \(y'' = -4y' - 4y\). Operators + - * / ^ (or **) and functions sin, cos, tan, exp, log, ln, sqrt, abs, pow plus constants pi, e are supported. Set x0, the initial y0 and y'0 = p0, the end point xn, and pick the number of subintervals n. More steps means a smaller step size \(h = (x_n - x_0)/n\) and lower error (global error is \(O(h^2)\)).
The formula explained
The equation is first reduced to a first-order system by letting \(p = y'\), giving \(y' = p\) and \(p' = F(x, y, p)\). Each RK2 step samples the slope at the start and at the midpoint and combines them: \(j_1 = h\cdot F(x,y,p)\), \(k_1 = h\cdot p\), then \(j_2\) and \(k_2\) are evaluated at the midpoint and used to advance p and y. The local truncation error is \(O(h^3)\) per step.
Worked example
With F = -4*p-4*y, x0 = 0, y0 = 0, p0 = 1, xn = 1, n = 50, the step size is \(h = 0.02\). The first step gives \(y_1 = 0.0192\) and \(p_1 = 0.9224\). Iterating to x = 1 yields $$y(1) \approx 0.13533 \quad\text{and}\quad y'(1) \approx -0.13533,$$ matching the exact solution \(y = x\cdot e^{-2x}\) whose value at x = 1 is \(e^{-2} = 0.135335\).
Step-by-Step RK2 Calculation by Hand
Rewrite the second-order ODE \(y''=F(x,y,y')\) as a first-order system by setting \(p=y'\): \(y'=p\) and \(p'=F(x,y,p)\). The midpoint RK2 method then advances both unknowns together.
- Compute the step size: \(h=\dfrac{x_n-x_0}{n}\). Start from \(x=x_0,\ y=y_0,\ p=p_0\).
- Compute the slope increment at the start: \(j_1=h\,F(x,y,p)\).
- Compute the position increment at the start: \(k_1=h\,p\).
- Compute the slope increment at the midpoint: \(j_2=h\,F\left(x+\tfrac{h}{2},\ y+\tfrac{k_1}{2},\ p+\tfrac{j_1}{2}\right)\).
- Compute the position increment at the midpoint: \(k_2=h\left(p+\tfrac{j_1}{2}\right)\).
- Update: \(p_{new}=p+j_2\), \(y_{new}=y+k_2\), \(x_{new}=x+h\).
- Repeat steps 2–6 a total of \(n\) times, recording \((x,y,p)\) after each pass.
First step of an example: \(F=-4p-4y\), \(x_0=0\), \(y_0=0\), \(p_0=1\), \(h=0.02\).
- \(j_1=0.02\cdot(-4\cdot1-4\cdot0)=0.02\cdot(-4)=-0.08\)
- \(k_1=0.02\cdot1=0.02\)
- Midpoint state: \(x=0.01,\ y=0+0.01=0.01,\ p=1-0.04=0.96\)
- \(j_2=0.02\cdot(-4\cdot0.96-4\cdot0.01)=0.02\cdot(-3.88)=-0.0776\)
- \(k_2=0.02\cdot0.96=0.0192\)
- \(p_1=1+(-0.0776)=0.9224\)
- \(y_1=0+0.0192=0.0192\)
- \(x_1=0.02\)
This reproduces \(y_1=0.0192\) and \(p_1=0.9224\). The exact solution \(y=xe^{-2x}\) gives \(y(0.02)\approx0.019215\), so the first step is already close.
Effect of Step Count on Accuracy
Test problem: \(y''=-4y'-4y\), \(y(0)=0\), \(y'(0)=1\) on \([0,1]\). The exact solution is \(y=xe^{-2x}\), so \(y(1)=e^{-2}\approx0.135335\). For this linear problem the midpoint RK2 result has a closed form: with \(h=1/n\), \(y(1)=(1-2h+2h^2)^{\,n-1}(1-2h)\). The values below come from that formula.
| Steps n | Step size h | RK2 y(1) | Absolute error |
|---|---|---|---|
| 5 | 0.2 | 0.128288 | 0.007047 |
| 10 | 0.1 | 0.134096 | 0.001240 |
| 25 | 0.04 | 0.135171 | 0.000164 |
| 50 | 0.02 | 0.135297 | 0.0000383 |
| 100 | 0.01 | 0.135326 | 0.0000093 |
RK2 is a second-order method, so the global error scales like \(h^2\). Doubling n cuts the error by roughly 4×: from n=25 to 50 the ratio is about 4.3, and from n=50 to 100 about 4.1. At very coarse steps (n=5 to 10) the ratio is larger, about 5.7, because the asymptotic regime has not yet been reached.
Definitions & Glossary
- Second-order ODE
- A differential equation whose highest derivative is the second, written here as \(y''=F(x,y,y')\).
- Initial conditions
- The values \(y(x_0)=y_0\) and \(y'(x_0)=p_0\) at the starting point. A second-order equation needs two of them.
- p = y'
- An auxiliary variable for the first derivative. It turns one second-order equation into two first-order equations: \(y'=p\) and \(p'=F(x,y,p)\).
- Step size h
- The spacing between computed points, \(h=(x_n-x_0)/n\). Smaller h gives higher accuracy but needs more steps.
- Midpoint (modified Euler) RK2
- A two-stage Runge-Kutta method. It estimates slopes at the start of the step, uses them to step to the midpoint, and then applies the midpoint slopes across the whole step.
- j1, j2, k1, k2
- The increments of one step. j1 and j2 are the changes in p (start and midpoint). k1 and k2 are the changes in y (start and midpoint). The final update uses j2 and k2.
- Local truncation error O(h³)
- The error introduced in a single step, proportional to \(h^3\).
- Global error O(h²)
- The accumulated error over the whole interval, proportional to \(h^2\), because about \(1/h\) steps each add an \(O(h^3)\) error.
- Initial value problem
- A differential equation together with the initial conditions that select one particular solution.
- Stiffness / stability
- For equations with rapidly decaying components, an explicit method like RK2 needs a small enough h to stay stable. If h is too large the numerical solution can oscillate or blow up even when the true solution decays. Reduce h or use an implicit method in such cases.
FAQ
Is this RK2 or RK4? This is second-order Runge-Kutta (midpoint rule), not the classical fourth-order method, so it is only second-order accurate globally.
Can xn be less than x0? Yes. The step size simply becomes negative and integration proceeds backward, which is mathematically valid.
Why did I get an error row? Evaluation problems such as division by zero, log of a non-positive number or sqrt of a negative number inside F stop the integration and report the location.