Preface


This tutorial is made solely for the purpose of education and it is designed for students taking Applied Math 0340. It is primarily for students who have some experience using Mathematica. If you have never used Mathematica before and would like to learn more of the basics for this computer algebra system, it is strongly recommended looking at the APMA 0330 tutorial. 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 commands 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 first course APMA0330
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the first course APMA0330
Return to Mathematica tutorial for the second course APMA0340
Return to the main page for the course APMA0340
Return to the main page for the course APMA0330

 


Part IV. Numerical Methods

General numerical methods
Euler's algorithms
Polynomial approximations
Runge-Kutta methods
Multistep methods
Numerical methods for boundary value problems
a. Shooting method
b. Weighted residual method
c. Finite difference method


The abundant literature on the subject of numerical solution of ordinary differential equations is on the one hand, a result of the tremendous variety of actual systems in the physical and biological sciences and engineering disciplines that are described by ordinary differential equations and, on the other hand, a result of the fact that the subject is currently active. The existence of a large number of methods, each having special advantages, has been a source of confusion as to what methods are best for certain classes of problems.

 

2.4.1. General Numerical Methods


 

 

 

2.4.2. Euler's Algorithms


Euler method for systems of differential equations

euler[F_, a_, Y0_, b_, Steps_] :=
Module[{t, Y, h = (b - a)/Steps//N, i},
t[0] = a; Y[0] = Y0;
Do[
t[i] = a + i h;
Y[i] = Y[i - 1] + h F[t[i - 1], Y[i - 1]],
{i, 1, n}
];
Table[{t[i], Y[i]}, {i, 0, n}]
]

Using for simplicity the initial point to be the origin. we present another way to implement the Euler rule:

h = .1;                (* step size *)
TimeMax = 3.5;
maxN = TimeMax/h;
plotmax = 5;
plotmin = -5;
fx[x_, y_] = y + x
Out[8]= x+y
fy[x_, y_] = -2 x + 3 y
Out[9]= -2 x + 3 y
xxo = -1.5;
yyo = -0.5;
xx[0] := xxo
yy[0] := yyo
xx[n_] := xx[n] = xx[n - 1] + h*fx[xx[n - 1], yy[n - 1]]
yy[n_] := yy[n] = yy[n - 1] + h*fy[xx[n - 1], yy[n - 1]]
MatrixForm[Table[{h*n, xx[n], yy[n]}, {n, 0, maxN}]]
Out[15]//MatrixForm=

We can make several different types of plots. Here we plot on the phase plane using dots and then using line segments

eulerdotplot =
ListPlot[Table[{xx[n], yy[n]}, {n, 0, maxN}],
PlotStyle -> {PointSize[Large], Red}]
eulerlineplot =
ListLinePlot[Table[{xx[n], yy[n]}, {n, 0, maxN}],
PlotStyle -> {Thick, Green}]

Here we create the associated streamplot

streamplot =
StreamPlot[{fx[x, y], fy[x, y]}, {x, plotmin, plotmax}, {y, plotmin,
plotmax}]

 

We plot x-component versus t

xPlot = ListLinePlot[Table[{h*n, xx[n]}, {n, 0, maxN}],
PlotStyle -> {Thick, Blue}]

We plot y-component versus t

yPlot = ListLinePlot[Table[{h*n, yy[n]}, {n, 0, maxN}],
PlotStyle -> {Thick, Green}]

Finally, we mash them all together

Show[streamplot, eulerlineplot]




 

 


II. Basic Properties of Matrices


2.4.3. Polynomial Approximations


 

 

 

2.4.4. Runge-Kutta Methods


 

 

 

2.4.5. Multistep Methods

 

 

 

IV. Solving Systems of Non-linear Differential Equations



F[x_, y_] := -y^3;
G[x_, y_] := x^3;
sol = NDSolve[{x'[t] == F[x[t], y[t]], y'[t] == G[x[t], y[t]],
x[0] == 1, y[0] == 0}, {x, y}, {t, 0, 3*Pi},
WorkingPrecision -> 20]
ParametricPlot[Evaluate[{x[t], y[t]}] /. sol, {t, 0, 3*Pi}]

X[t_] := Evaluate[x[t] /. sol]
Y[t_] := Evaluate[y[t] /. sol]
fns[t_] := {X[t], Y[t]};
len := Length[fns[t]];
Plot[Evaluate[fns[t]], {t, 0, 3*Pi}]

 

 

V. van der Pol Equation


vdot[t_] = mu (1 - x[t]^2)*x'[t] - x[t]
Out[1]= -x[t] + mu (1 - x[t]^2) Derivative[1][x][t]
M = {{0, 1}, {D[vdot[t], x[t]], D[vdot[t], x'[t]]}}
Out[2]= {{0, 1}, {-1 - 2 mu x[t] Derivative[1][x][t], mu (1 - x[t]^2)}}
MO = M /. {x[t] -> 0, x'[t] -> 0}
Out[3]= {{0, 1}, {-1, mu}}
Eigenvalues[MO]
Out[3]= {1/2 (mu - Sqrt[-4 + mu^2]), 1/2 (mu + Sqrt[-4 + mu^2])}
MO // MatrixForm
Out[4]=
{"0", "1"},
{"1, "mu"}

vdot05[t_] = vdot[t] /. mu -> 0.5
Out[6]= -x[t] + 0.5 (1 - x[t]^2) Derivative[1][x][t]
Orbit1 = NDSolve[{v'[t] == vdot05[t], x'[t] == v[t], x[0] == 0.01,  v[0] == 0}, {x, v}, {t, 0, 40}]
Out[7]= {{x->InterpolatingFunction[{0.,40.}},<>],
v->InterpolatingFunction[{0.,40.}},<>]}}

plot05 = ParametricPlot[{x[t] /. Orbit1[[1]],
v[t] /. Orbit1[[1]]}, {t, 0, 40}, Frame -> True,
PlotLabel -> "van der Pol solution for mu =0.5"]

Out[6]=

Orbit2 = NDSolve[{v'[t] == vdot05[t], x'[t] == v[t], x[0] == 4.0, v[0] == 0}, {x, v}, {t, 0, 40}]
plot054 =
ParametricPlot[{x[t] /. Orbit2[[1]], v[t] /. Orbit2[[1]]}, {t, 0, 40}]
Out[8]=
Show[{plot05, plot054}, Frame -> True]

vdot3[t_] = vdot[t] /. mu -> 3.0
Orbit3 = NDSolve[{v'[t] == vdot3[t], x'[t] == v[t], x[0] == 0.01,
v[0] == 0}, {x, v}, {t, 0, 40}]
plot305 =

ParametricPlot[{x[t] /. Orbit3[[1]], v[t] /. Orbit3[[1]]}, {t, 0,
40}, Frame -> True, AspectRatio -> 1.0, PlotRange -> 5.1,
PlotLabel -> "van der Pol solution for mu =3"]
Out[11]=

plot305 =
ParametricPlot[{x[t] /. Orbit3[[1]], v[t] /. Orbit3[[1]]}, {t, 0,
40}, Frame -> True, AspectRatio -> 1.0, PlotRange -> 5.1,
PlotLabel -> "van der Pol solution for mu =3"]

Orbit33 =
NDSolve[{v'[t] == vdot3[t], x'[t] == v[t], x[0] == 3.0,
v[0] == 0}, {x, v}, {t, 0, 40}]
plot33 = ParametricPlot[{x[t] /. Orbit33[[1]],
v[t] /. Orbit33[[1]]}, {t, 0, 40}, Frame -> True,
AspectRatio -> 1.0, PlotRange -> 5.1,
PlotLabel -> "van der Pol solution for mu =3"]
Out[13]=

ParametricPlot[
Evaluate[Table[{x[t], x'[t]} /.
NDSolve[{x''[t] - 3 (1 - (x[t])^2) x'[t] + x[t] == 0, x[0] == c,
x'[0] == 0}, {x[t], x'[t]}, {t, 0, 50}], {c, -1, 1, 0.1}]], {t,
0, 50}, PlotRange -> {{-3, 3}, {-5.1, 5.1}}, PlotStyle -> Purple]

 

Plot with colors (Orlando):

Show[Table[
ParametricPlot[
Evaluate[{x[t], x'[t]} /.
NDSolve[{x''[t] - 3 (1 - (x[t])^2) x'[t] + x[t] == 0, x[0] == c,
x'[0] == 0}, {x[t], x'[t]}, {t, 0, 50}]], {t, 0, 50},
PlotRange -> {{-3, 3}, {-5.1, 5.1}},
PlotStyle -> Hue[(c + 1)/2]], {c, -1, 1, 0.1}]]

with two colors:

black = ParametricPlot[
Evaluate[Table[{x[t], x'[t]} /.
NDSolve[{x''[t] - 3 (1 - (x[t])^2) x'[t] + x[t] == 0, x[0] == c,
x'[0] == 0}, {x[t], x'[t]}, {t, 0, 50}], {c, -1, 0, 0.1}]], {t,
0, 50}, PlotRange -> {{-2.5, 2.5}, {-5.1, 5.1}},
PlotStyle -> Black]

blue = ParametricPlot[
Evaluate[Table[{x[t], x'[t]} /.
NDSolve[{x''[t] - 3 (1 - (x[t])^2) x'[t] + x[t] == 0, x[0] == c,
x'[0] == 0}, {x[t], x'[t]}, {t, 0, 50}], {c, 0, 1, 0.1}]], {t,
0, 50}, PlotRange -> {{-2.5, 2.5}, {-5.1, 5.1}}, PlotStyle -> Blue]

Show[black, blue]

With direction field:

field = VectorPlot[{y, 1*(1 - x^2)*y - x}, {x, -4.2, 4.2}, {y, -4.2,
4.2}, VectorScale -> {Small, 1}]

de1 = x'[t] == y[t];
de2 = y'[t] == 4*(1 - (x[t])^2)*y[t] - x[t];
soln1 = NDSolve[{de1, de2, x[0] == 0.5, y[0] == 0}, {x, y}, {t, 0,
40}]
soln2 = NDSolve[{de1, de2, x[0] == 2.5, y[0] == 0}, {x, y}, {t, 0,
40}]
plot1 = ParametricPlot[Evaluate[{x[t], y[t]} /. soln1], {t, 0, 40},
PlotStyle -> {RGBColor[0, 0, 1], Thickness[0.005]},
DisplayFunction -> Identity, PlotRange -> 6.6]
plot2 = ParametricPlot[Evaluate[{x[t], y[t]} /. soln2], {t, 0, 40},
PlotStyle -> {Black, Thickness[0.006]}, DisplayFunction -> Identity]
Show[plot1, plot2, field,
PlotLabel -> "Van der Pol System with mu = 4",
AxesLabel -> {"x", "y"}, DisplayFunction -> $DisplayFunction]

Kplot = Plot[Evaluate[x[t] /. soln2[[1]][[1]]], {t, 0, 40},
PlotStyle -> RGBColor[0, 0, 1], DisplayFunction -> Identity]

Vplot = Plot[Evaluate[y[t] /. soln2[[1]][[2]]], {t, 0, 40},
PlotStyle -> Black, DisplayFunction -> Identity]

Show[Kplot, Vplot, PlotLabel -> "Current (x) and Voltage (y)",
AxesLabel -> {"t", "x,y"}, DisplayFunction -> $DisplayFunction]

Instead of Vectorplot, sometimes StreamPlot presents the solution better:

StreamPlot[{y, 1*(1 - x^2)*y - x}, {x, -4.2, 4.2}, {y, -4.2, 4.2},
VectorScale -> {Small, 1}]

 

 

 

 

2. Solving Euler Systems of Equations


Euler systems of equation are of the form: x y' = A y
where y=y[x] is unknown vector column
A is a square matrix