Maple-assisted Runge-Kutta derivation
$$\def\quad{\;\;\;\;}$$
1 Introduction
In Runge-Kutta test 21, examples were given of the use of the Maple procedure test, to verify the order of, and assist with the derivation of, some low order Runge-Kutta methods
Thanks to the power of the algebraic manipulation tools of Maple, it is possible to derive methods in a much simpler way than Kutta could have even dreamt of. For example, for a four stage explicit method with tableau
$$\begin{array}{c|cccc} 0\\ c_2 & c_2\\ c_3 & c_3-a_{32} & a_{32}\\ c_4 & c_4-a_{42}-a_{43} & a_{42} & a_{43}\\ \hline &b_1&b_2&b_3&b_4 \end{array},$$
the conditions for order 4 are
$$\begin{align*} b_1+b_2+b_3+b_4 &=1,\\ b_2c_2+b_3c_3+b_4c_4 &=\frac12,\\ b_2c_2^2+b_3c_3 b_3 a_{32}c_2 + b_4 a_{42} c_2 + b_4 a_{43} c_3 &=\frac16,\\ b_2c_2^3+b_3c_3 b_3 c_3 a_{32}c_2 + b_4 c_4a_{42} c_2 + b_4 c_4a_{43} c_3 &=\frac18,\\ b_3 a_{32}c_2^2 + b_4 a_{42} c_2^2 + b_4 a_{43} c_3^2 &=\frac1{12},\\ b_4 a_{43} a_{32} c_2 &=\frac1{24}. \end{align*}$$
These equations are easily solved using solve
restart:
solve(
{b[1]+b[2]+b[3]+b[4]-1,
b[2]*c[2]+b[3]*c[3]+b[4]*c[4]-1/2,
b[2]*c[2]^2+b[3]*c[3]^2+b[4]*c[4]^2-1/3,
b[3]*a[3,2]*c[2]+b[4]*a[4,2]*c[2]
+b[4]*a[4,3]*c[3]-1/6,
b[2]*c[2]^3+b[3]*c[3]^3+b[4]*c[4]^3-1/4,
b[3]*c[3]*a[3,2]*c[2]
+b[4]*c[4]*a[4,2]*c[2]
+b[4]*c[4]*a[4,3]*c[3]-1/8,
b[3]*a[3,2]*c[2]^2
+b[4]*a[4,2]*c[2]^2
+b[4]*a[4,3]*c[3]^2-1/12,
b[4]*a[4,3]*a[3,2]*c[2]-1/24},
{b[1],b[2],b[3],b[4],
a[3,2],a[4,2],a[4,3],c[4]}
)
giving the result $$\begin{align*} b[1]&= \frac{6 c[2] c[3] - 2 c[2] - 2 c[3] + 1 }{12 c[2] c[3] }\\ b[2]&= \frac{2 c[3] - 1}{12 c[2] (c[2] - 1) (c[2] - c[3]) }\\ b[3]&= \frac{-2 c[2] + 1 }{12 c[3] (c[3] - 1) (c[2] - c[3]) }\\ b[4]&= \frac{6 c[2] c[3] - 4 c[2] - 4 c[3] + 3 }{12 (c[3] - 1) (c[2] - 1) }\\ a[3, 2]&= \frac{c[3] (c[2] - c[3]) }{2 c[2] (2 c[2] - 1) }\\ a[4, 2]&= \frac{(c[2] - 1)(-4 c[3]^2 + c[2] + 5 c[3] - 2)}{(2 (6 c[2] c[3] - 4 c[2] - 4 c[3] + 3) (c[2] - c[3]) c[2] }\\ a[4, 3]&= \frac{(2 c[2] - 1) (c[2] - 1) (c[3] - 1) }{c[3] (6 c[2] c[3] - 4 c[2] - 4 c[3] + 3) (c[2] - c[3]) }\\ c[4] &=1 \end{align*}$$
This remarkable solution required the assumption that there would be some restriction on possible values of $c_2, c_3, c_4$, but going even further, that the constraint would involve $c_4$. Throughout this page, we will avoid this sort of assumption and let the solutions arise in a natural and systematic way.
2 Inner order of trees
The order of a tree satisfies the recursive definition $$\begin{align*} |\tau| &=1,\\ |t * t'| &= |t| + |t'|. \end{align*}$$ Define the inner-order by $$\begin{align*} o(\tau) &=0,\\ o(\tau*t') &=1 + o(t'),\\ o(t * t') &= o(t) + o(t'), \quad t\ne \tau. \end{align*}$$ Some examples of order and inner order, together with corresponding elementary weights are $$\begin{alignat*}4 t&=\tau, & |t| &=1, & o(t) &= 0, & \Phi(t) &= \sum b_i,\\ t&=[\tau], & |t| &=2, & o(t) &= 1, & \Phi(t) &= \sum b_i,c_i,\\ t&=[\tau^2], & |t| &=3, & o(t) &= 1, & \Phi(t) &= \sum b_i,c_i^2,\\ t&=[[\tau]], & |t| &=3, & o(t) &= 2, & \Phi(t) &= \sum b_i a_{ij} c_j,\\ t&=[\tau^3], & |t| &=4, & o(t) &= 1, & \Phi(t) &= \sum b_i,c_i^3,\\ t&=[\tau[\tau]], & |t| &=4, & o(t) &= 2, & \Phi(t) &= \sum b_i c_ia_{ij} c_j,\\ t&=[[\tau^2]], & |t| &=4, & o(t) &= 2, & \Phi(t) &= \sum b_i a_{ij} c_j^2,\\ t&=[[[\tau]]], \quad & |t| &=4, \quad & o(t) &= 3, \quad & \Phi(t) &= \sum b_i a_{ij} a_{jk}c_k. \end{alignat*}$$ Note that for $t\ne \tau$, $o(t)-1$ is the degree of $a_{ij}$ products and powers in $\Phi(t)$. Hence it will be useful to modify the procedure test from Runge-Kutta test 21 so that results are given only for limited values of $o(t)$.
3 Modified test procedure
In introducing the new version of test, we list this procedure along with some related lines of code:
- # reset
- # test
This version of test differs from the version in Runge-Kutta test 21 in four ways
- The additional parameter q sets a bound on the inner order of trees that are analysed by the procedure
- The number of stages s for the method under study becomes a global variable. This is for the convenience of other procedures used in association with test.
- A vector of nom-zero error terms is provided as the result of a call to the procedure.
- An additional parameter phi determines the format of the computed errors.
# helpers
The definitions of some additional quantities and procedures needed to make the use of test as convenient as possible
# problem
The presentation of a specific type of tableau, usually with free parameters, which need to be found
# solution
The calculation which evaluates the missing parameters to derive a method in full.
- In the listing which follows, the single example of a three stage, third order Runge-Kutta will be derived in the special case that
$c_3=0$. To solve other Runge-Kutta derivation problems, only # problem and # solve are altered
# reset
restart:
with(LinearAlgebra):
# test
test := proc(A,b,c,p,q,phi)
local n, k, r, l, i, fact, Psi, pt,
qt, first, last, one, R, compt,
ctest, ttest, err;
global s;
one := Vector(s,1);
ctest:=phi~(A.one-c);
compt := 0;
for i from 1 to s do
if ctest[i] <> 0 then
compt := compt + 1;
err[compt] := ctest[i];
fi;
od;
ttest:=phi(b.one-1);
if ttest <> 0 then
compt := compt + 1;
err[compt] := ttest;
fi;
first[1] := 1;
last[1] := 1;
fact[1] := 1;
pt[1] := 1;
qt[1] := 0;
Psi[1] := one;
n := 1;
for k from 2 to p do
first[k] := n + 1 :
for r from 1 to last[k-1] do
for l from first[k-pt[r]]
to last[k-pt[r]] do
if (l = 1 or r <= R[l]) then
n := n + 1 :
pt[n] := k;
if l = 1 then
qt[n] := 1+qt[r]
else
qt[n] := qt[l]+qt[r]
fi;
if qt[n] > q then
n := n-1;
else
R[n]:=r;
fact[n]:=
fact[l]*fact[r]*k/(pt[l]);
Psi[n] := phi~(DiagonalMatrix(
Psi[l]).(A.Psi[r]));
ttest:=phi(b.Psi[n]-1/fact[n]);
if ttest <> 0 then
compt := compt + 1;
err[compt] := ttest;
fi
fi
fi
od
od;
last[k] := n :
od;
Vector(compt,i->err[i])
end:
# helpers
tester:= proc(p, q:=p)
global A, b, c, e, f, N;
e:=test(A, b, c, p, q, phi);
f:=e;
if numelems(f) = 0 then
print('no*output');
fi;
Matrix(min(numelems(f),N),2,
(i,j) -> if j=1 then i else f[i] fi)
end:
show := proc(n:=numelems(f))
global f;
local i;
for i from 1 to min(numelems(f),n) do
printf("%g: %a\n", i, f[i])
od
end:
rootout := proc()
global e,f;
local i, u, v, w;
for i from 1 to numelems(e) do
if type(numer(e[i]), integer) then
else
v := convert(factors(e[i])[2], Matrix);
u := v.Vector([1, 0]);
if numelems(u) = 1 and v[1,2] >1 then
e[i]:= v[1, 1]
fi
fi
od;
f:=e;
Matrix(min(numelems(f),N),2,
(i,j) -> if j=1 then i else f[i] fi)
end:
filter := proc(x)
global e, f;
local temp, i, n, a, b;
n := numelems(e);
if type(x,numeric) then
f := Vector([e[x]]);
elif type(x,algebraic) then
temp := convert(e,set);
for i from 1 to n do
if coeff(e[i],x,1) = 0 then
temp := temp minus {e[i]}
fi
od;
f := Vector(convert(temp,list))
elif type(x,list) then
f := Vector(numelems(
Vector(x)),i->e[x[i]])
elif type(x, range) then
temp:= convert(x,list);
a:=temp[1]-1; b:=temp[2];
f:=Vector(b-a, i->e[i+a])
elif type(x,set) then
temp := Vector(convert(x,list));
f :=Vector(numelems(
Vector(temp)),i->e[temp[i]]);
fi;
Matrix(min(numelems(f),N),2,
(i,j) -> if j=1 then i else f[i] fi)
end:
clean := proc(n)
global soln;
local temp, i, m;
m := numelems({soln});
if 1 < m then
print([n, m], 'multiple solutions');
soln := {soln}[n];
fi;
temp := soln;
for i to numelems(soln) do
if soln[i] then
temp :=temp minus{soln[i]}
fi
od;
soln:=temp
end:
solver:=proc(variables, n := 1)
global soln, f;
soln:=solve(convert(f,set),variables);
if soln<>NULL then
clean(n)
else
'no solution'
fi;
end:
fix := proc(first::uneval, second::uneval, $)
global A, b, c, soln, phi;
A := phi~(subs(soln, A));
b := phi~(subs(soln, b));
c := phi~(subs(soln, c));
if nargs=1 then
return eval(first)
elif nargs=2 then
return eval(first),eval(second)
else
return eval(A),eval(b),eval(c);
fi;
end:
setA := proc(which:='explicit')
global s, A, AA, AAij,Aij, all, row, col;
if which = 'explicit' then
A := Matrix(s, s, (i, j) ->
if j < i then a[i, j] else 0 fi)
elif which = 'implicit' then
A := Matrix(s, s, (i, j) -> a[i, j])
elif which = 'semi' then
A := Matrix(s, s, (i, j) ->
if j <= i then a[i, j] else 0 fi)
elif which = 'dirk' then
A := Matrix(s, s, (i, j) ->
if j < i then a[i, j]
elif i < j then 0 else lambda fi);
fi;
AA := Matrix(s, s, (i, j) -> a[i, j]);
AAij := r -> SubMatrix(
AA, r[1] .. r[2], r[3] .. r[4]);
Aij := r -> convert(AAij(r), set);
all := Aij([1, s, 1, s]);
row := i -> Aij([i, i, 1, s]);
col := j -> Aij([1, s, j, j]);
end:
consist := proc(ss:=s-1)
global s, col;
tester(1, 0);
filter(1..ss);
solver(col(1));
fix();
end:
C1 := proc()
local i, w;
global s, A, b, c;
for i to s do
A[i, 1] := 0
od;
w := phi~(c-(A.(Vector(s, 1))));
for i to s do
A[i, 1] := w[i]
od
end:
C2 := proc()
local i, w;
global s, A, b, c;
for i from 3 to s do
A[i, 2] := 0
od;
w := phi~((1/2*map(t->t^2,c) - (A . c))/c[2]);
for i from 3 to s do
A[i, 2] := w[i];
od;
C1()
end:
D1 := proc()
local i, w;
global s, A, b, c;
for i to s do
A[s, i] := 0;
od;
w := phi~((b-(b . A)-(b.(DiagonalMatrix(c))))/b[s]);
for i to s do
A[s, i] := w[i];
od
end:
C2D1 := proc()
C2(); D1()
end:
N := 100:
# problem
s := 3:
b := Vector[row]([b1,b2,b3]):
c := Vector([0,c2,0]):
setA():
phi := simplify:
# solution
tester(3,0):solver({a[2,1],a[3,1],b1}):fix(A,b):# (1)
tester(3,1):solver({b2,c2}):fix(b,c): # (2)
tester(3,2):solver(a[3,2]):fix(A): # (3)
tester(3): # (4)
Even though the solution block of statements is given in a static form, in practice the work is done under control of the user. For example, in line #(1) , the statement tester(3,0); is first obeyed. This gives a result $$\left[\begin{array}{l c } 1 & a_{21} - c2\\ 2 & a_{31} + a_{32}\\ 3 & b1+b2+b3-1 \end{array}\right]$$ which suggests that solver({a[2,1],a[3,1],b1}) should be obeyed, to give the result $$\{b1 = -b2 - b3 + 1, a_{21} = c2, a_{31} = -a_{32}\}.$$ Finally, on the line #(1), fix() is obeyed and colons are used to terminate the statements on the line, so that it becomes as shown in the code.
For explict methods, it is convenient to use consist() as a short-cut to force the row-sum of A to have the correct value before other steps in solution are carried out. For example, we could use
# problem
s := 3:
b := Vector[row]([b1,b2,b3]):
c := Vector([0,c2,0]):
setA():
phi := simplify:
# solution
consist():
tester(3,1):solver({b1,b2,c2}):fix(b,c):
tester(3,2):solver(a[3,2]):fix(A):
tester(3):
4 Methods with order 4
In Section 1 it was found that $c_4=1$ is a consequence of a 4 stage method having order 4. We will repeat this calculation in a way that gives more insight, and uses the power of test to the full.
# problem
s := 4;
b := Vector[row]([b1,b2,b3,b4]):
c := Vector([0,c2,c3,c4]):
setA():
phi := simplify:
# solution
consist():
tester(4,1):solver({b1,b2,b3,b4}):fix(b):
tester(4,2):solver(all):fix(A):
tester(4,3):solver(c4):fix(c):
tester(4)
It looks as though the choices of $c$ and therefor $b$ are crucial. We will now explore some special cases related to Simpson's rule.
# problem Case 1
s := 4:
b := Vector[row]([b1,b2,b3,b4]):
c := Vector([0,1,1/2,1]):
setA():
phi := simplify:
# solution
consist():
tester(4, 1):solver({b1, b2, b3}):fix(b):
tester(4,3):filter(1..2):solver(all):fix(A):
tester(4,3):solver(all):fix(A):
tester(4):
with solution $$\begin{array}{c|cccc} 0 &\\ 1 & 1 &\\ \frac12 & \frac38 & \frac18\\ 1 & \frac{4b4-1}{4b4} & -\frac{1}{12b4}& \frac1{3b4}\\ \hline & \frac16 & \frac16-b4 & \frac23 & b4\\ \end{array}$$ In the exploration step fix(); , the values of $A$, $b$ and $c$ are printed out. For convenience the options fix('A'); , fix('b'); , and fix('c'); are also available to print only a specific matrix or vector.
# problem Case 2
s := 4:
b := Vector[row]([b1,b2,b3,b4]):
c := Vector([0,1/2,0,1]):
setA():
phi := simplify:
# solution
consist();
tester(4,1):solver({b1,b2,b4}):fix(b):
tester(4,3):solver(all):fix(A):
tester(4):
with solution $$\begin{array}{c|cccc} 0 &\\ \frac12 & \frac12 &\\ 0 & -\frac{1}{12b3} & \frac{1}{12b3}\\ 1 & -\frac12-6b3 & \frac32 & 6b3\\ \hline & \frac16-b3 & \frac23 & b3 & \frac16 \\ \end{array}$$
# problem Case 3
s := 4:
b := Vector[row]([b1,b2,b3,b4]):
c := Vector([0,c2,1/2,1]):
setA():
phi := simplify:
# solution
consist():
tester(4,1):solver({b1,b2,b3,b4}):fix(b):
tester(4,3):solver(all):fix(A):
tester(4):
with solution $$\begin{array}{c|cccc} 0 &\\ c2 & c2 &\\ \frac12 & \frac{4c2-1}{8c2} & \frac{1}{8c2}\\ 1 & \frac{1-2c2}{2c2} & -\frac{1}{2c2} & 2\\ \hline & \frac16 & 0 & \frac23 & \frac16\\ \end{array}$$
# problem Case 4
s := 4:
b := Vector[row]([b1,b2,b3,b4]):
c := Vector([0,1/2,1/2,1]):
setA():
phi := simplify:
# solution
consist():
tester(4,1):solver({b1,b3,b4}):fix(b):
tester(4,3):solver(all):fix(A):
tester(4):
with solution $$\begin{array}{c|cccc} 0 &\\ \frac12 & \frac12 &\\ \frac12 & \frac{3b2-1}{6b2-4} & \frac{1}{6b2-4}\\ 1 & 0 & 3b2-1 & 2-3b2\\ \hline & \frac16 & b2 & \frac23-b2 & \frac16\\ \end{array}$$
A famous example of this final case is found by the substitution $b2=\frac13$.
$$\begin{array}{c|cccc} 0 &\\ \frac12 & \frac12 &\\ \frac12 & 0& \frac12\\ 1 & 0 & 0 & 1\\ \hline & \frac16 & \frac13 & \frac13 & \frac16 \end{array}$$
It was shown by S. Gill that the working space required to implement a method in the final case is reduced if $$\det\left(\left[ \begin{array}{ccc} 1 & a_{31} & a_{32}\\ 1 & 0 & a_{42}\\ 1 & b_1 & b_2 \end{array}\right] \right)= 0,$$ leading to the method $$\begin{array}{c|cccc} 0 &\\ \frac12 & \frac12 &\\ \frac12 &1+\frac12\sqrt3 & -\frac12-\frac12\sqrt3\\ 1 & 0 &\frac12 + \frac12\sqrt3 & \frac12 -\frac12\sqrt3 \\ \hline & \frac16 & \frac12-\frac16\sqrt3 & \frac16+ \frac16\sqrt3 & \frac16\\ \end{array}.$$
5 Methods with order 5
This is just a preliminary look at fifth order methods; further information is available in specialised text-books. It has been proved that for order 5, $s>5$, amd We will assume that $s=6$. For simplicity we will experiment with equally spaced $c$ values. During this experiment, we will illustrate a new feature of solver.
# problem: First fifth order experiment
s := 6;
b := Vector[row]([b1,b2,b3,b4,b5,b6]);
c := Vector(6,[0,1/5,2/5,3/5,4/5,1]);
setA();
phi := simplify;
# solution
consist():
tester(5,1);solver({b1,b3,b4,b5,b6});
fix(b);
tester(5,2):solver(all):fix(A):
tester(5,3):filter(1):solver(a[6,5]):fix(A):
tester(5,3):filter(1):solver(a[5,4]):fix(A):
tester(5,3):solver(a[3,2]):fix(A): # (1)
tester(5):
During the step in this experment,where solver(a[3,2]); in (1) is evaluated. an output [1,2], multiple solutions indicates that that there are actually 2 solutions and that the first is printed, by default. To obtain the second solution, # problem and # solution are run again but with (1) replaced by tester(5,3):solver(a[3,2],2):fix(A):. The two solutions produced, using either of the two possible values of $a[3,2]$ are both very complicated and, as a means of obtaing a simplification, the two solutions are again evaluated but with $b2=0$. In this case, tester(5,3):solver(a[3,2]):fix(A): gives only a single duplicated solution, with tableau
$$\begin{array}{c|cccccc} 0&\\ \frac15&\frac{1}{5}\\ \frac25&0&\frac{2}{5}\\ \frac35&-\frac{3}{10}+a_{43}&\frac{9}{10}-2a_{43}&a_{43}\\ \frac45&\frac{1}{5}+\frac{4}{3}a_{43}&-\frac{2}{5} -\frac{8}{3}a_{43}&\frac{4}{3}a_{43}+1\\ 1&-\frac{13}{2}-25a_{43}&\frac{35}{2}+50a_{43}&-25a_{43}-\frac{15}{2}&-10&\frac{15}{2}\\ \hline &\frac{17}{144}&0&\frac{25}{36}&-\frac{25}{72}&\frac{25}{48}&\frac1{72} \end{array}$$
We complete this section on fifth order methods by choosing $b$ and $c$ so to obtain a more symmetric quadrature.
# problem:
s := 6:
b := Vector[row]([b1,0,b3,b4,b5,b6]):
c := Vector(6,[0,1/4,1/4,1/2,3/4,1]):
setA():
phi := simplify:
# solution
consist():
tester(5,1):solver({b1,b3,b4,b5,b6}):fix(b):
tester(5,2):solver(all):fix(A):
tester(5,3):rootout():filter(3):solver(a[5,3]):fix(A):
tester(5,3):solver({a[5,2],a[6,3]}):fix(A):
tester(5):
with tableau $$\begin{array}{c|cccccc} 0\\ \frac14&\frac14\\ \frac14&\frac18&\frac18\\ \frac12&0&\frac12-a_{43}&a_{43}\\ \frac34&\frac3{16}&\frac34 a_{43}-\frac34 &\frac34-\frac34a_{43}&\frac9{16}\\ 1&-\frac37&2-\frac{12}7a_{43}&\frac{12}7 a_{43}&-\frac{12}7&\frac87\\ \hline &\frac7{90}&0&\frac{16}{45}&\frac2{15}&\frac{16}{45}&\frac7{90} \end{array}$$