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 \([x_0, x_n]\), using the classic fourth-order Runge-Kutta (RK4) method. You supply the right-hand side \(F\) as a math expression in \(x\), \(y\) and \(p\) (where \(p\) stands for \(y'\)), the initial conditions \(y(x_0)\) and \(y'(x_0)\), the interval endpoints, and the number of steps. The calculator returns a full table of \(x\), \(y\) and \(y'\) values plus the endpoint values. It is pure numerical analysis and applies universally.
How to use it
Enter \(F(x, y, p)\) — for example -4*p - 4*y for \(y'' + 4y' + 4y = 0\). Set \(x_0\), \(y_0 = f(x_0)\), and \(p_0 = y'(x_0)\). Set the end point \(x_n\) and choose the number of subintervals \(n\) (more steps means a smaller step size \(h = (x_n - x_0)/n\) and higher accuracy). Pick how many significant digits to display. The error of RK4 is \(O(h^4)\) globally, so doubling \(n\) roughly cuts the error by a factor of 16.
The formula explained
A second-order ODE is reduced to a system of two first-order equations by setting \(p = y'\): then \(y' = p\) and \(p' = F(x, y, p)\). RK4 advances both unknowns each step using four weighted slope evaluations (\(k\) for \(y\), \(j\) for \(p\)), combining them as $$\frac{k_1 + 2k_2 + 2k_3 + k_4}{6} \quad\text{and}\quad \frac{j_1 + 2j_2 + 2j_3 + j_4}{6}.$$
Worked example
Take \(y'' = -4p - 4y\) with \(x_0 = 0\), \(y_0 = 0\), \(p_0 = 1\), \(x_n = 1\) and a single coarse step (\(n = 1\), \(h = 1\)). The four stages give \(k_1=1\), \(k_2=-1\), \(k_3=2\), \(k_4=-5\) and \(j_1=-4\), \(j_2=2\), \(j_3=-6\), \(j_4=12\). Then $$y(1) = \frac{1 - 2 + 4 - 5}{6} = -\frac{1}{3} = -0.3333$$ and $$p(1) = 1 + \frac{-4 + 4 - 12 + 12}{6} = 1.$$ The exact solution is \(y = x e^{-2x}\), so \(y(1) = e^{-2} = 0.1353\); one giant step of \(h = 1\) is far too coarse. Using \(n = 50\) or \(100\) converges right onto the exact value.
Step-by-Step RK4 Procedure for a Second-Order ODE
RK4 handles a second-order equation by rewriting it as a first-order system in \(y\) and \(p = y'\).
- Set \(p = y'\). The equation becomes the system \(y' = p\), \(p' = F(x, y, p)\).
- Compute the step size \(h = \dfrac{x_n - x_0}{n}\).
- First slopes: \(k_1 = p\), \(j_1 = F(x, y, p)\).
- Second slopes: \(k_2 = p + h\,j_1/2\), \(j_2 = F\left(x + h/2,\; y + h\,k_1/2,\; p + h\,j_1/2\right)\).
- Third slopes: \(k_3 = p + h\,j_2/2\), \(j_3 = F\left(x + h/2,\; y + h\,k_2/2,\; p + h\,j_2/2\right)\).
- Fourth slopes: \(k_4 = p + h\,j_3\), \(j_4 = F\left(x + h,\; y + h\,k_3,\; p + h\,j_3\right)\).
- Update both unknowns: \(y_{\text{next}} = y + \dfrac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)\) and \(p_{\text{next}} = p + \dfrac{h}{6}(j_1 + 2j_2 + 2j_3 + j_4)\).
- Advance \(x \leftarrow x + h\), then repeat steps 3 to 7 until \(x = x_n\), which takes n steps in total.
Interpreting the Output Table
Each row of the table is one grid point. The x column holds \(x_0, x_0+h, \dots, x_n\). The y column is the numerical approximation of the solution \(y(x)\). The y' column is the approximation of the first derivative \(p = y'(x)\). The first row simply repeats your initial conditions \(y_0\) and \(p_0\).
Checking convergence
- Run the problem with n, then with 2n (for example 10, 20, 50, 100), and compare the rows at the same x values.
- When the leading digits stop changing between runs, those digits are probably reliable.
- For a smooth problem, RK4 error should fall roughly 16-fold each time n doubles. If it does not, something else is wrong.
- Raising the precision digits only reduces rounding error. It does not reduce the truncation error that comes from h.
Warning signs
- Values that grow without bound, or oscillations whose amplitude keeps increasing, when the true solution should be smooth or decaying. This suggests h is too large or the equation is stiff.
- Results that change a lot when n changes.
- Overflow or undefined values partway through the table.
Limits of fixed-step RK4
- For stiff equations, such as those with widely separated decay rates, the stability limit forces a very small h. Fixed-step RK4 then needs many steps or fails, and an implicit method is usually a better choice.
- If F has a singularity inside the interval, for example division by x at x = 0 or a term like \(1/(x-1)\), the method cannot handle it. Results near the singularity are meaningless, so avoid it or split the interval.
- The step size is fixed. The method does not adapt to regions where the solution changes quickly.
FAQ
Can I integrate backward? Yes — set \(x_n\) less than \(x_0\) and the step size \(h\) becomes negative, integrating from \(x_0\) down to \(x_n\).
Why does my answer look wrong with few steps? RK4 accuracy depends on a small step size. Increase \(n\) until successive results stop changing.
What functions can F contain? + - * / ^, parentheses, and sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, exp, log/ln, log10, sqrt, abs, plus the constants pi and e.