Second-Order Runge-Kutta Solver for 2nd-Order ODEs

Second-Order Runge-Kutta Solver for 2nd-Order ODEs

Emerging AI Skill PRO

Enter Calculation

Formula

{y′=pp′=F(x,y,p)h=xn−x0nyi+1=yi+hp(xi+h2)pi+1=pi+hF(xi+h2,yi+h2pi,pi+h2Fi)start:y0=y0,p0=p0\begin{gathered} \begin{cases} y^{\prime} = p \\ p^{\prime} = {{\text{F}(x,y,p)}} \end{cases} \qquad h = \dfrac{{{\text{x}_n}} - {{\text{x}_0}}}{\text{n}} \\[1.4em] \begin{aligned} y_{i+1} &= y_i + h\,p\!\left(x_i + \tfrac{h}{2}\right) \\ p_{i+1} &= p_i + h\,\text{F}\!\left(x_i + \tfrac{h}{2},\; y_i + \tfrac{h}{2}p_i,\; p_i + \tfrac{h}{2}{{\text{F}_i}}\right) \end{aligned} \\[1.4em] \text{start:}\quad y_0 = {{\text{y}_0}}, \quad p_0 = {{\text{p}_0}} \end{gathered}
Usage stats

Advertisement

Results

Approximate y at the final x = 1
0.135296935
y' (= p) at that point: -0.1351841876
Method 2nd-order Runge-Kutta (midpoint), local error O(h^3)
Step size h 0.02
Number of steps n 50
x y y' = p
0.0000000000000000 0.0000000000000000 1.0000000000000000
0.020000000000000000 0.019200000000000000 0.92240000000000000
0.040000000000000000 0.036894720000000000 0.84934720000000000
0.060000000000000000 0.053172670463999996 0.78060434278399990
0.080000000000000000 0.068117735709081600 0.71594578469232630
0.10000000000000000 0.081809400586607000 0.65515694969774430
0.12000000000000001 0.094322966500334410 0.59803380843614800
0.14000000000000000 0.10572975724910819 0.54438238107427720
0.16000000000000000 0.11609731515993502 0.49401826294618173
0.18000000000000000 0.12548958795637377 0.44676617193727500
0.19999999999999998 0.13396710678720436 0.40245951663989300
0.21999999999999997 0.14158715582126055 0.36093998434738000
0.23999999999999996 0.14840393379607325 0.32205714799495050
0.25999999999999995 0.15446870789053943 0.28566809119500390
0.27999999999999997 0.15982996027517107 0.25163705055227820
0.30000000000000000 0.16453352767755470 0.21983507448028827
0.32000000000000000 0.16862273428543417 0.19013969777498171
0.34000000000000000 0.17213851829528548 0.16243463123452180
0.36000000000000004 0.17511955240035207 0.13660946564564497
0.38000000000000006 0.17760235849882816 0.11255938948719588
0.40000000000000010 0.17962141689018327 0.090184919730279480
0.42000000000000010 0.18120927021549250 0.069391645142043710
0.44000000000000010 0.18239662238604734 0.050089981526471296
0.46000000000000013 0.18321243273344676 0.032194938360768685
0.48000000000000015 0.18368400560378675 0.015625896310044324
0.50000000000000010 0.18383707560845658 0.00030639512601406127
0.52000000000000010 0.18369588873438927 -0.013836068542494098
0.54000000000000010 0.18328327950738588 -0.026870233878397654
0.56000000000000020 0.18262074439331474 -0.038861259595601230
0.58000000000000020 0.18172851161356454 -0.049870899020389145
0.60000000000000020 0.18062560754308220 -0.059957666948328710
0.62000000000000020 0.17932991985163985 -0.069176998652447110
0.64000000000000020 0.17785825754163154 -0.077581401401623160
0.66000000000000030 0.17622640802868705 -0.085220598832054500
0.68000000000000030 0.17444919140468865 -0.092141668499290240
0.70000000000000030 0.17254051201637852 -0.098389172923625410
0.72000000000000030 0.17051340748663180 -0.10400528442760995
0.74000000000000030 0.16838009529963238 -0.10902990405100074
0.76000000000000030 0.16615201706561347 -0.11350077481565485
0.78000000000000040 0.16383988057550042 -0.11745358960059915
0.80000000000000040 0.16145369975070850 -0.12092209387579109
0.82000000000000040 0.15900283258849274 -0.12393818353188411
0.84000000000000040 0.15649601719860975 -0.12653199803260615
0.86000000000000040 0.15394140602262482 -0.12873200910612914
0.88000000000000040 0.15134659832296904 -0.13056510518203110
0.90000000000000050 0.14871867102481567 -0.13205667177110950
0.92000000000000050 0.14606420798999050 -0.13323066797637725
0.94000000000000050 0.14338932779845207 -0.13410969931504166
0.96000000000000050 0.14069971010936450 -0.13471508702311555
0.98000000000000050 0.13800062067043317 -0.13506693400652098
1.0000000000000004 0.13529693504097162 -0.13518418759510423

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

Schematic of reducing a second-order ODE to two coupled first-order equations
A second-order ODE is rewritten as two coupled first-order equations using \(y' = p\).

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.

Advertisement

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

Diagram showing the RK2 midpoint method estimating a slope at the midpoint of a step
The midpoint RK2 method evaluates an initial slope, then uses the slope at the interval midpoint to advance the solution.

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.

  1. Compute the step size: \(h=\dfrac{x_n-x_0}{n}\). Start from \(x=x_0,\ y=y_0,\ p=p_0\).
  2. Compute the slope increment at the start: \(j_1=h\,F(x,y,p)\).
  3. Compute the position increment at the start: \(k_1=h\,p\).
  4. 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)\).
  5. Compute the position increment at the midpoint: \(k_2=h\left(p+\tfrac{j_1}{2}\right)\).
  6. Update: \(p_{new}=p+j_2\), \(y_{new}=y+k_2\), \(x_{new}=x+h\).
  7. 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.

Advertisement

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.

Last updated:

Most popular in Math and Statistics

View all Math and Statistics calculators