# Preface

This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0330. It is primarily for students who have very little experience or have never used Mathematica before and would like to learn more of the basics for this computer algebra system. As a friendly reminder, don't forget to clear variables in use and/or the kernel.

Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in regular fonts. This means that you can copy and paste all comamnds into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts to your needs for learning how to use the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.

# Predictor-corrector 3

Consider the initial value problem $$y' = f(x,y) , \quad y(x_0 ) = y_0 .$$ The third order multistep method is based on the following Adams--Bashforth-Moulton formulas:

$\begin{split} p_{n+1} &= y_{n} + \frac{h}{12} \left[ 23\, f_n -16\, f_{n-1} + 5\, f_{n-2} \right] , \\ y_{n+1} &= y_n + \frac{h}{12}\left[ 5\,f\left( x_{n+1} , p_{n+1} \right) + 8\,f\left( x_{n} , y_{n} \right) - f\left( x_{n-1} , y_{n-1} \right) \right] , \end{split}$
where as usual $$f_n = f \left( x_{n} , y_{n} \right) .$$ Recall that we work with a uniform mesh of points $$x_n = x_0 + n\,h , \quad n=0,1,2,\ldots ;$$ with fixed step size h.

There is known another third order multistep method, called Hermite predictor-corrector method:

$\begin{split} p_{n+1} &= 5\, y_{n-1} - 4\,y_{n} + h \left[ 4\, f_n -2\, f_{n-1} \right] , \\ y_{n+1} &= y_{n-1} + \frac{h}{3}\left[ f\left( x_{n+1} , p_{n+1} \right) + 4\,f\left( x_{n} , y_{n} \right) + f\left( x_{n-1} , y_{n-1} \right) \right] , \end{split}$

Predictor-corrector of order 2:

ABM2[f_, {x_, x0_, xn_}, {y_, y0_, y1_}, steps_] :=
Block[{xold = x0, yold = y0, ynew = y1, sollist = {{x0, y0}}, h},
h = N[(xn - x0)/steps]; sollist = Append[sollist, {x0 + h, y1}];
Do[xnew = xold + h;
xnew2 = xold + h*2;
k1 = f /. {x -> xold, y -> yold};
k2 = f /. {x -> xnew, y -> ynew};
abnew = ynew + (3*h/2)*k2 - h*k1/2;
k3 = f /. {x -> xnew2, y -> abnew};
bm = ynew + h*k2/2 + h*k3/2;
sollist = Append[sollist, {xnew2, bm}];
xold = xnew;
yold = ynew; ynew = bm, {steps}];
Return[sollist[[steps + 1]]]]

To apply this method, use the following commands:

f[x_, y_] := x^2 + y^2;
ABM2[f[x, y], {x, 0, 1.0}, {y, 1, Exp[0.1*0.1]}, 10]

If you want to see the whole table of values:

ABM2[f_, {x_, x0_, xn_}, {y_, y0_, y1_}, steps_] :=
Block[{xold = x0, yold = y0, ynew = y1, sollist = {{x0, y0}}, h},
h = N[(xn - x0)/steps]; sollist = Append[sollist, {x0 + h, y1}];
Do[xnew = xold + h;
xnew2 = xold + h*2;
k1 = f /. {x -> xold, y -> yold};
k2 = f /. {x -> xnew, y -> ynew};
abnew = ynew + (3*h/2)*k2 - h*k1/2;
k3 = f /. {x -> xnew2, y -> abnew};
bm = ynew + h*k2/2 + h*k3/2;
sollist = Append[sollist, {xnew2, bm}];
xold = xnew;
yold = ynew; ynew = bm, {steps}];
Return[sollist]]

Example.

f[x_, y_] := 2*x*y;
ABM2[f[x, y], {x, 0, 1.0}, {y, 1, Exp[0.1*0.1]}, 10]
Out[3]= {1., 2.73005} {{0, 1}, x, y, {0.1, 1.01005}, {0.2, 1.04096}, {0.3, 1.09458}, {0.4, 1.1743}, {0.5, 1.2854}, {0.6, 1.43554}, {0.7, 1.63575}, {0.8, 1.9017}, {0.9, 2.25576}, {1., 2.73005}, {1.1, 3.37112}}

Predictor-corrector method of order 2:

x[0] = 0; y[0] = 1;   (* initial conditions *)
Nsteps = 20;           (* number of steps *)
h=0.1;                     (* step size *)
f[u_, v_] :=                                      (* define the slope function *)
Array[x, Nsteps]; Array[y, Nsteps];
i=1;
While[i < Nsteps + 1, x[i] = x[i - 1] + h; i++]; (* Generate uniform grid of points *)
y[1]=        (* the value at the first mesh point must be specified *)
k=1;
While[k < Nsteps + 1,
predictor =
y[k] + 3*h/2*f[x[k], y[k]] - h*f[x[k - 1], y[k - 1]]/2 ;
y[k + 1] = y[k] + h*f[x[k], y[k]]/2 + h*f[x[k + 1], predictor]/2;
k++];
y[20]

or using Function command

While[k < Nsteps + 1,
predictor =
y[k] + 3*h*Function[{u, v}, f[u, v]][x[k], y[k]]/2 -
h*Function[{u, v}, f[u, v]][x[k - 1], y[k - 1]]/2;
y[k + 1] =
y[k] + h*Function[{u, v}, f[u, v]][x[k], y[k]]/2 +
h*Function[{u, v}, f[u, v]][x[k + 1], predictor]/2;
k++];

Predictor-corrector of order 3:

Preprocessing includes:
Nsteps = number of steps
h = step size
f[u_,v_] = slope function
Initial values:
x[0] =
y[0] =
Generate first values using a single-step numerical method or actual solution:
y[1] =
y[2] =
Arrays of independent and dependent variables corresponding to uniform grid:
Array[x, Nsteps]; Array[y, Nsteps];
i=1;
While[i < Nsteps + 1, x[i] = x[i - 1] + h; i++];

k=2
While[k < Nsteps + 1,
predictor =
y[k] + 23*h*Function[{u, v}, f[u, v]][x[k], y[k]]/12 -
4*h*Function[{u, v}, f[u, v]][x[k - 1], y[k - 1]]/3 + 5*h*Function[{u, v}, f[u, v]][x[k - 2], y[k - 2]]/12;
y[k + 1] =
y[k] + 2*h*Function[{u, v}, f[u, v]][x[k], y[k]]/3 +
5*h*Function[{u, v}, f[u, v]][x[k + 1], predictor]/12 - h*Function[{u, v}, f[u, v]][x[k - 1], y[k - 1]]/12;
k++];

Variable coefficient recurence, corresponding to explicit Adams' method of the second order where the first value is determined from the actual solution (however, it can be found using any single-step method, say obtained by classical Runge-Kutta algorithm) can be solved numerically:

a[0] = 1; h = 0.1
a[1] = Exp[h*h]     (* this value was determined analytically *)
a[1] == 1 + (8*h*h + 5*h^4 + 2*h^6)/12}, a, {n, 1, 30}] (* or using Runge-Kutta-4 *)
RecurrenceTable[{a[n + 1] ==
a[n] + 3*h*h*n*a[n] - h*h*(n - 1)*a[n - 1], a[0] == 1, ]