RK4 Second-Order ODE Calculator

RK4 Second-Order ODE Calculator

Emerging AI Skill PRO

Enter Calculation

Right-hand side of y'' = F(x, y, y'). Use x, y, p and + - * / ^, sin, cos, exp, log, sqrt, pi, e.

Results

y at xn
0.135335274181834636
value of y at the end point
y' at xn
-0.135335259156935755
value of y' (= p) at the end point

Method: classic 4th-order Runge-Kutta (RK4) · steps n = 50 · step size h = 0.02

i x y = f(x) y' = p
0 0 0 1
1 0.0200000000000000004 0.0192157866666666649 0.922357866666666637
2 0.0400000000000000008 0.0369246498212522645 0.849267048373009037
3 0.0599999999999999978 0.0532152204359356007 0.780489998192799184
4 0.0800000000000000017 0.0681714957894921647 0.715800800394514436
5 0.100000000000000006 0.0818730665794356605 0.654984623530822985
6 0.119999999999999996 0.0943953333479264356 0.597837198534814762
7 0.140000000000000013 0.105809712710295489 0.544164320702777649
8 0.160000000000000003 0.116183833853126500 0.493781374492720526
9 0.179999999999999993 0.125581725747898248 0.446512880115091604
10 0.200000000000000011 0.134063995506174150 0.402192060937331175
11 0.220000000000000001 0.141687998283193534 0.360660430767116402
12 0.239999999999999991 0.148507999118433237 0.321767400120490987
13 0.260000000000000009 0.154575327084229969 0.285369900620602468
14 0.280000000000000027 0.159938522096849084 0.251332026710574419
15 0.299999999999999989 0.164643474728420641 0.219524693900195078
16 0.320000000000000007 0.168733559342904987 0.189825312800676715
17 0.340000000000000024 0.172249760864668727 0.162117478234802465
18 0.359999999999999987 0.175230795474316581 0.136290672741392865
19 0.380000000000000004 0.177713225513107781 0.112239983823256881
20 0.400000000000000022 0.179731568864560570 0.0898658343166999773
21 0.419999999999999984 0.181318403069687717 0.0690737252883046882
22 0.440000000000000002 0.182504464420687129 0.0497739908911289108
23 0.460000000000000020 0.183318742266808804 0.0318815646377390433
24 0.479999999999999982 0.183788568755511195 0.0153157565716578320
25 0.5 0.183939704221884465 4.08419086883604621E-8
26 0.520000000000000018 0.183796418429634401 -0.0141381467925742240
27 0.540000000000000036 0.183381567857668515 -0.0271676019807839605
28 0.560000000000000053 0.182716669217487304 -0.0391535357516522645
29 0.579999999999999960 0.181821969378139686 -0.0501577498528595900
30 0.599999999999999978 0.180716511867434454 -0.0602388038506180140
31 0.619999999999999996 0.179418200110394555 -0.0694521743669628405
32 0.640000000000000013 0.177943857558579527 -0.0778504068142278072
33 0.660000000000000031 0.176309284856870752 -0.0854832599702608359
34 0.680000000000000049 0.174529314187597984 -0.0923978437225306121
35 0.700000000000000067 0.172617860925471212 -0.0986387502945474809
36 0.719999999999999973 0.170587972730655124 -0.104248179253945611
37 0.739999999999999991 0.168451876201472006 -0.109266056588119206
38 0.760000000000000009 0.166221021202630853 -0.113730148120445823
39 0.780000000000000027 0.163906122979543406 -0.117676167527839340
40 0.800000000000000044 0.161517202164191437 -0.121137879208628521
41 0.820000000000000062 0.159063622773142788 -0.124147196238530849
42 0.839999999999999969 0.156554128293666411 -0.126734273641762762
43 0.859999999999999987 0.153996875949458961 -0.128927597194074095
44 0.880000000000000004 0.151399469233258754 -0.130754067964697240
45 0.900000000000000022 0.148768988789577172 -0.132239082794837953
46 0.920000000000000040 0.146112021726915342 -0.133406610901388389
47 0.940000000000000058 0.143434689435146284 -0.134279266785993751
48 0.959999999999999964 0.140742673980222749 -0.134878379621434813
49 0.979999999999999982 0.138041243145009956 -0.135224059279484138
50 1 0.135335274181834636 -0.135335259156935755

Formula

k1=hpil1=hF(xi,yi,pi)k2=h(pi+l12)l2=hF(xi+h2,yi+k12,pi+l12)k3=h(pi+l22)l3=hF(xi+h2,yi+k22,pi+l22)k4=h(pi+l3)l4=hF(xi+h,yi+k3,pi+l3)yi+1=yi+16(k1+2k2+2k3+k4),pi+1=pi+16(l1+2l2+2l3+l4)where{F=F(x,y,p)h=xn−x0nx0=x0,y0=y0,p0=p0\begin{gathered} \begin{aligned} k_1 &= h\,p_i & l_1 &= h\,F(x_i,\,y_i,\,p_i) \\ k_2 &= h\left(p_i+\tfrac{l_1}{2}\right) & l_2 &= h\,F\!\left(x_i+\tfrac{h}{2},\,y_i+\tfrac{k_1}{2},\,p_i+\tfrac{l_1}{2}\right) \\ k_3 &= h\left(p_i+\tfrac{l_2}{2}\right) & l_3 &= h\,F\!\left(x_i+\tfrac{h}{2},\,y_i+\tfrac{k_2}{2},\,p_i+\tfrac{l_2}{2}\right) \\ k_4 &= h\left(p_i+l_3\right) & l_4 &= h\,F\!\left(x_i+h,\,y_i+k_3,\,p_i+l_3\right) \end{aligned} \\[1em] y_{i+1} = y_i + \tfrac{1}{6}\left(k_1+2k_2+2k_3+k_4\right), \quad p_{i+1} = p_i + \tfrac{1}{6}\left(l_1+2l_2+2l_3+l_4\right) \\[1.4em] \text{where}\quad \left\{ \begin{aligned} F &= {F(x,\,y,\,p)} \\ h &= \dfrac{\text{xn} - \text{x0}}{\text{n}} \\ x_0 &= \text{x0},\; y_0 = \text{y0},\; p_0 = \text{p0} \end{aligned} \right. \end{gathered}
Usage stats

Advertisement

Report an error on this page

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.

Schematic showing y''=F reduced to the system y'=p and p'=F(x,y,p)
The second-order ODE is rewritten as a coupled first-order system in \(y\) and \(p = y'\).

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}.$$

Advertisement

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.

Diagram of four RK4 slope estimates k1 to k4 across one step from x to x+h
RK4 combines four slope estimates per step to advance the solution from \(x\) to \(x+h\).

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'\).

  1. Set \(p = y'\). The equation becomes the system \(y' = p\), \(p' = F(x, y, p)\).
  2. Compute the step size \(h = \dfrac{x_n - x_0}{n}\).
  3. First slopes: \(k_1 = p\), \(j_1 = F(x, y, p)\).
  4. 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)\).
  5. 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)\).
  6. Fourth slopes: \(k_4 = p + h\,j_3\), \(j_4 = F\left(x + h,\; y + h\,k_3,\; p + h\,j_3\right)\).
  7. 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)\).
  8. Advance \(x \leftarrow x + h\), then repeat steps 3 to 7 until \(x = x_n\), which takes n steps in total.
Advertisement

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.

Last updated:

Most popular in Math and Statistics

View all Math and Statistics calculators