summaryrefslogtreecommitdiff
path: root/content/know/concept/runge-kutta-method/index.pdc
blob: ac2eabfe492002f464459bc7a7f5071518f490ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
---
title: "Runge-Kutta method"
firstLetter: "R"
publishDate: 2022-03-10
categories:
- Mathematics
- Numerical methods

date: 2022-03-07T14:10:18+01:00
draft: false
markup: pandoc
---

# Runge-Kutta method

A **Runge-Kutta method** (RKM) is a popular approach
to numerically solving systems of ordinary differential equations.
Let $\vb{x}(t)$ be the vector we want to find,
governed by $\vb{f}(t, \vb{x})$:

$$\begin{aligned}
    \vb{x}'(t)
    = \vb{f}\big(t, \vb{x}(t)\big)
\end{aligned}$$

Like in all numerical methods, the $t$-axis is split into discrete steps.
If a step has size $h$, then as long as $h$ is small enough,
we can make the following approximation:

$$\begin{aligned}
    \vb{x}'(t) + a h \vb{x}''(t)
    &\approx \vb{x}'(t \!+\! a h)
    \\
    &\approx \vb{f}\big(t \!+\! a h,\, \vb{x}(t \!+\! a h)\big)
    \\
    &\approx \vb{f}\big(t \!+\! a h,\, \vb{x}(t) \!+\! a h \vb{x}'(t) \big)
\end{aligned}$$

For sufficiently small $h$,
higher-order derivates can also be included,
albeit still at $t \!+\! a h$:

$$\begin{aligned}
    \vb{x}'(t) + a h \vb{x}''(t) + b h^2 \vb{x}'''(t)
    &\approx \vb{f}\big(t \!+\! a h,\, \vb{x}(t) \!+\! a h \vb{x}'(t) \!+\! b h^2 \vb{x}''(t) \big)
\end{aligned}$$

Although these approximations might seem innocent,
they actually make it quite complicated to determine the error order of a given RKM.

Now, consider a Taylor expansion around the current $t$,
truncated at a chosen order $n$:

$$\begin{aligned}
    \vb{x}(t \!+\! h)
    &= \vb{x}(t) + h \vb{x}'(t) + \frac{h^2}{2} \vb{x}''(t) + \frac{h^3}{6} \vb{x}'''(t) + \:...\, + \frac{h^n}{n!} \vb{x}^{(n)}(t)
    \\
    &= \vb{x}(t) + h \bigg[ \vb{x}'(t) + \frac{h}{2} \vb{x}''(t) + \frac{h^2}{6} \vb{x}'''(t) + \:...\, + \frac{h^{n-1}}{n!} \vb{x}^{(n)}(t) \bigg]
\end{aligned}$$

We are free to split the terms as follows,
choosing real factors $\omega_{mj}$ subject to $\sum_{j} \omega_{mj} = 1$:

$$\begin{aligned}
    \vb{x}(t \!+\! h)
    &= \vb{x} + h \bigg[ \sum_{j = 1}^{N_1} \omega_{1j} \, \vb{x}'
    + \frac{h}{2} \sum_{j = 1}^{N_2} \omega_{2j} \, \vb{x}''
    + \:...\, + \frac{h^{n-1}}{n!} \sum_{j = 1}^{N_n} \omega_{nj} \, \vb{x}^{(n)} \bigg]
\end{aligned}$$

Where the integers $N_1,...,N_n$ are also free to choose,
but for reasons that will become clear later,
the most general choice for an RKM is $N_1 = n$, $N_n = 1$, and:

$$\begin{aligned}
    N_{n-1}
    = N_n \!+\! 2
    ,\quad
    \cdots
    ,\quad
    N_{n-m}
    = N_{n-m+1} \!+\! m \!+\! 1
    ,\quad
    \cdots
    ,\quad
    N_{2}
    = N_3 \!+\! n \!-\! 1
\end{aligned}$$

In other words, $N_{n-m}$ is the $m$th triangular number.
This is not so important,
since this is not a practical way to describe RKMs,
but it is helpful to understand how they work.


## Example derivation

For example, let us truncate at $n = 3$,
such that $N_1 = 3$, $N_2 = 3$ and $N_3 = 1$.
The following derivation is very general,
except it requires all $\alpha_j \neq 0$.
Renaming $\omega_{mj}$, we start from:

$$\begin{aligned}
    \vb{x}(t \!+\! h)
    &= \vb{x} + h \bigg[ (\alpha_1 + \alpha_2 + \alpha_3) \, \vb{x}'
    + \frac{h}{2} (\beta_2 + \beta_{31} + \beta_{32}) \, \vb{x}''
    + \frac{h^2}{6} \gamma_3 \, \vb{x}''' \bigg]
    \\
    &= \vb{x} + h \bigg[ \alpha_1 \vb{x}'
    + \Big( \alpha_2 \vb{x}' + \frac{h}{2} \beta_2 \vb{x}'' \Big)
    + \Big( \alpha_3 \vb{x}' + \frac{h}{2} (\beta_{31} + \beta_{32}) \vb{x}'' + \frac{h^2}{6} \gamma_3 \vb{x}''' \Big) \bigg]
\end{aligned}$$

As discussed earlier, the parenthesized expressions
can be approximately rewritten with $\vb{f}$:

$$\begin{aligned}
    \vb{x}(t \!+\! h)
    = \vb{x} + h &\bigg[ \alpha_1 \vb{f}(t, \vb{x})
    + \alpha_2 \vb{f}\Big( t \!+\! \frac{h \beta_2}{2 \alpha_2}, \;
    \vb{x} \!+\! \frac{h \beta_2}{2 \alpha_2} \vb{x}' \Big)
    \\
    & + \alpha_3 \vb{f}\Big( t \!+\! \frac{h (\beta_{31} \!\!+\!\! \beta_{32})}{2 \alpha_3}, \;
    \vb{x} \!+\! \frac{h \beta_{31}}{2 \alpha_3} \vb{x}' \!+\! \frac{h \beta_{32}}{2 \alpha_3} \vb{x}'
    \!+\! \frac{h^2 \gamma_3}{6 \alpha_3} \vb{x}'' \Big) \bigg]
    \\
    = \vb{x} + h &\bigg[ \alpha_1 \vb{k}_1
    + \alpha_2 \vb{f}\Big( t \!+\! \frac{h \beta_2}{2 \alpha_2}, \;
    \vb{x} \!+\! \frac{h \beta_2}{2 \alpha_2} \vb{k}_1 \!\Big)
    \\
    & + \alpha_3 \vb{f}\Big( t \!+\! \frac{h (\beta_{31} \!\!+\!\! \beta_{32})}{2 \alpha_3}, \;
    \vb{x} \!+\! \frac{h \beta_{31}}{2 \alpha_3} \vb{k}_1 \!+\! \frac{h \beta_{32}}{2 \alpha_3}
    \vb{f}\Big( t \!+\! \frac{h \gamma_3}{3 \beta_{32}}, \;
    \vb{x} \!+\! \frac{h \gamma_3}{3 \beta_{32}} \vb{k}_1 \!\Big) \!\Big) \bigg]
\end{aligned}$$

Here, we can see an opportunity to save some computational time
by reusing an evaluation of $\vb{f}$.
Technically, this is optional, but it would be madness not to,
so we choose:

$$\begin{aligned}
    \frac{\beta_2}{2 \alpha_2}
    = \frac{\gamma_3}{3 \beta_{32}}
\end{aligned}$$

Such that the next step of $\vb{x}$'s numerical solution is as follows,
recalling that $\sum_{j} \alpha_j = 1$:

$$\begin{aligned}
    \boxed{
        \vb{x}(t \!+\! h)
        = \vb{x}(t) + h \Big( \alpha_1 \vb{k}_1 + \alpha_2 \vb{k}_2 + \alpha_3 \vb{k}_3 \Big)
    }
\end{aligned}$$

Where $\vb{k}_1$, $\vb{k}_2$ and $\vb{k}_3$ are different estimates
of the average slope $\vb{x}'$ between $t$ and $t \!+\! h$,
whose weighted average is used to make the $t$-step.
They are given by:

$$\begin{aligned}
    \boxed{
        \begin{aligned}
            \vb{k}_1
            &\equiv \vb{f}(t, \vb{x})
            \\
            \vb{k}_2
            &\equiv \vb{f}\bigg( t + \frac{h \beta_2}{2 \alpha_2}, \;
            \vb{x} + \frac{h \beta_2}{2 \alpha_2} \vb{k}_1 \bigg)
            \\
            \vb{k}_3
            &\equiv \vb{f}\bigg( t + \frac{h (\beta_{31} \!\!+\!\! \beta_{32})}{2 \alpha_3}, \;
            \vb{x} + \frac{h \beta_{31}}{2 \alpha_3} \vb{k}_1 + \frac{h \beta_{32}}{2 \alpha_3} \vb{k}_2 \bigg)
        \end{aligned}
    }
\end{aligned}$$

Despite the contraints on $\alpha_j$ and $\beta_j$,
there is an enormous freedom of choice here,
all leading to valid RKMs, although not necessarily good ones.


## General form

A more practical description goes as follows:
in an $s$-stage RKM, a weighted average is taken
of up to $s$ slope estimates $\vb{k}_j$ with weights $b_j$.
Let $\sum_{j} b_j = 1$, then:

$$\begin{aligned}
    \boxed{
        \vb{x}(t \!+\! h)
        = \vb{x}(t) + h \sum_{j = 1}^{s} b_j \vb{k}_j
    }
\end{aligned}$$

Where the estimates $\vb{k}_1, ..., \vb{k}_s$
depend on each other, and are calculated one by one as:

$$\begin{aligned}
    \boxed{
        \vb{k}_m
        = \vb{f}\bigg( t + h c_m,\; \vb{x} + h \sum_{j = 1}^{m - 1} a_{mj} \vb{k}_j \bigg)
    }
\end{aligned}$$

With $c_1 = 1$ and $\sum_{j = 1} a_{mj} = c_m$.
Writing this out for the first few $m$, the pattern is clear:

$$\begin{aligned}
    \vb{k}_1
    &= \vb{f}(t, \vb{x})
    \\
    \vb{k}_2
    &= \vb{f}\big( t + h c_2,\; \vb{x} + h a_{21} \vb{k}_1 \big)
    \\
    \vb{k}_3
    &= \vb{f}\big( t + h c_3,\; \vb{x} + h (a_{31} \vb{k}_1 + a_{32} \vb{k}_2) \big)
    \\
    \vb{k}_4
    &= \:...
\end{aligned}$$

The coefficients of a given RKM are usually
compactly represented in a **Butcher tableau**:

$$\begin{aligned}
    \begin{array}{c|ccc}
        0 \\
        c_2 & a_{21} \\
        c_3 & a_{31} & a_{32} \\
        \vdots & \vdots & \vdots & \ddots \\
        c_s & a_{s1} & a_{s2} & \cdots & a_{s,s-1} \\
        \hline
        & b_1 & b_2 & \cdots & b_{s-1} & b_s
    \end{array}
\end{aligned}$$

Each RKM has an **order** $p$,
such that the global truncation error is $\mathcal{O}(h^p)$,
i.e. the accumulated difference between the numerical
and the exact solutions is proportional to $h^p$.

The surprise is that $p$ need not be equal to the Taylor expansion order $n$,
nor the stage count $s$.
Typically, $s = n$ for computational efficiency, but $s \ge n$ is possible in theory.

The order $p$ of a given RKM is determined by
a complicated set of equations on the coefficients,
and the lowest possible $s$ for a desired $p$
is in fact only partially known.
For $p \le 4$ the bound is $s \ge p$,
whereas for $p \ge 5$ the only proven bound is $s \ge p \!+\! 1$,
but for $p \ge 7$ no such efficient methods have been found so far.

If you need an RKM with a certain order, look it up.
There exist many efficient methods for $p \le 4$ where $s = p$,
and although less popular, higher $p$ are also available.



## References
1.  J.C. Butcher,
    *Numerical methods for ordinary differential equations*, 3rd edition,
    Wiley.