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.
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the second course APMA0330
Return to Mathematica tutorial for the first course APMA0340
Return to the main page for the course APMA0340
Return to the main page for the course APMA0330
Return to Part III of the course APMA0330
Predictor-corrector 4
Consider the initial value problem \( y' = f(x,y) , \quad y(x_0 ) = y_0 . \) The fourth order multistep method is based on the following Adams--Bashforth-Moulton formulas:
Predictor-corrector of order 2:
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 yjis method, use the following commands:
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:
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.
ABM2[f[x, y], {x, 0, 1.0}, {y, 1, Exp[0.1*0.1]}, 10]
Predictor-corrector method of order 2:
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
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++];
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[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, ]
The Adams-Moulton converges when
Fixed Point Iteration
Bracketing Methods
Secant Methods
Euler's Methods
Heun Method
Runge-Kutta Methods
Runge-Kutta Methods of order 2
Runge-Kutta Methods of order 3
Runge-Kutta Methods of order 4
Polynomial Approximations
Error Estimates
Adomian Decomposition Method
Modified Decomposition Method
Multistep Methods
Multistep Methods of order 3
Multistep Methods of order 4
Milne Method
Hamming Method
Return to Mathematica page
Return to the main page (APMA0330)
Return to the Part 1 (Plotting)
Return to the Part 2 (First Order ODEs)
Return to the Part 3 (Numerical Methods)
Return to the Part 4 (Second and Higher Order ODEs)
Return to the Part 5 (Series and Recurrences)
Return to the Part 6 (Laplace Transform)