Linear Algebraic Equations
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 normal font. 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 for your needs to learn 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 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
This chapter is devoted to methods for solving linear algebraic equations. While they originated in the early nineteen century (by the work of Gauss), the field has seen an explosion of activity spurred by demand due to extraordinary technological advances in engineering and sciences. The reviews of the methods could be found in
http://www-users.cs.umn.edu/~saad/PDF/umsi-99-152.pdf
Vector Space
We recall the definition of vector space.
A vector space is a set \( V \) together with an inner operation called addition
(a rule for adding two elements of \( V \) to obtain a third element of \( V \) )
and another outer operation called scalar multiplication (a rule for multiplying a real or complex number times an
element of \( V \) to obtain a second element of \( V \) ) on which the following properties hold:
- \( {\bf x} + {\bf y} = {\bf y} + {\bf x} \)
- \( {\bf x} + ({\bf y} + {\bf z}) = ({\bf x} + {\bf y}) + {\bf z} \)
- There is an element 0 of V so that \( {\bf 0} + {\bf y} = {\bf y} + {\bf 0} = {\bf y}\) for every \( {\bf y} \in V \)
- There is an element \( -{\bf x} \) in V such that \( {\bf x} - {\bf x} = -{\bf x} + {\bf x} = {\bf 0}\)
- \( a({\bf x} + {\bf y}) = a{\bf y} + a{\bf x} \)
- \( (a+b)\,{\bf x} = a\,{\bf x} + b\,{\bf x} \)
- \( (ab){\bf x} = a(b {\bf x} )\)
- \( 1\,{\bf x} = {\bf x} \)
2.4.2. Euler's Algorithms
Euler method for systems of differential equations
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:
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}]]
We can make several different types of plots. Here we plot on the phase plane using dots and then using line segments
ListPlot[Table[{xx[n], yy[n]}, {n, 0, maxN}],
PlotStyle -> {PointSize[Large], Red}]

ListLinePlot[Table[{xx[n], yy[n]}, {n, 0, maxN}],
PlotStyle -> {Thick, Green}]

Here we create the associated streamplot
StreamPlot[{fx[x, y], fy[x, y]}, {x, plotmin, plotmax}, {y, plotmin,
plotmax}]

We plot x-component versus t
PlotStyle -> {Thick, Blue}]

We plot y-component versus t
PlotStyle -> {Thick, Green}]

Finally, we mash them all together

II. Basic Properties of Matrices
2.4.3. LU Factorization
The matrix A is a global variable and elements are changed when the LUfactor subroutine is executed. Hence, it is important to save a copy of A in the variable A0. Use one of the following two versions of the LUfactor subroutine and the SolveLU subroutine. The first version of LUfactor uses parallel programming and "row operations" instead of loops.
r = Table[j, {j,n}];
For[ p=1, p<= n-1, p++ ,
For[ j=p+1 , j<= n, j++ ,
If Abs[ A[[ r[[j]] , p ]] ] > Abs[ A[[ r[[p]] , p ]] ] ,
r[[{j,p}]] = r[[{p,j}]] ]; ];
For[ k=p+1, k<=n, k++,
A[[ r[[k]] , p ]] = A[[ r[[k]] , p ]] / A[[ r[[p]] , p ]] ;
A[[ r[[k]] , Range[p+1,n] ]] = A[[ r[[k]] , Range[p+1,n] ]] - A[[ r[[k]] , p ]] A[[ r[[p]] , Range[p+1,n] ]] ;];];
L = P = IdentityMatrix[n];
Z = Table[0, {j,n}];
P = P[[r]];
U = A[[r]];
For[ i = 1, i <=n, i++,
L[[ i, Range[1, i-1] ]] = A[[ r[[i]] , Range[1, i-1] ]] ;
U[[ i, Range[1, i-1] ]] = Z[[ Range[1, i-1] ]] ; ];
r = Table[j, {j,n}];
For[ p=1, p<= n-1, p++ ,
For[ j=p+1 , j<= n, j++ ,
If Abs[ A[[ r[[j]] , p ]] ] > Abs[ A[[ r[[p]] , p ]] ] ,
r[[{j,p}]] = r[[{p,j}]] ]; ];
For[ k=p+1, k<=n, k++,
A[[ r[[k]] , p ]] = A[[ r[[k]] , p ]] / A[[ r[[p]] , p ]] ;
For[ c =p+1, c <= n, c++ ,
A[[ r[[k]], c ]] = A[[ r[[k]], c ]] - A[[ r[[k]], p ]] A[[ r[[p]], c ]] ;];];]; L = P = IdentityMatrix[n];
Z = Table[0, {j,n}];
P = P[[r]];
U = A[[r]];
For[ i = 1, i <=n, i++,
For[ j = 1, j <=i-1, j++,
L[[ i, j ]] = A[[ r[[i]] , j ]] ;
U[[ i, j ]] = 0 ; ];];];
Module[ {k}, X = Y = Table[0, {n}] ;
For[ k=1, k<=n, k++ ,
Y[[k]] = (B[[ r[[k]] ]] - Sum[A[[ r[[k]], j ]] Y[[j]], {j, 1, k-1}]);];
For[ k =n, 1 <= k, k-- ,
X[[k]] = (Y[[k]] - Sum[A[[r[[k]], j]] X[[j]] , {j,k+1, n }])/ A[[r[[k]], k]] ];];
2.4.4. Runge-Kutta Methods
2.4.5. Multistep Methods