NumericalMethods[MatrixIterate] - approximates solution of a system of linear equations by iteration
Calling Sequence
MatrixIterate(A, b, xinit=v, method=m, tolerance=t, options)
MatrixIterate(A, method=m, output=matrix)
Parameters
A - Matrix; a square numerical matrix of the system
b - Vector; right-hand side of the system
v - Vector; initial approximate eigenvector of appropriate dimension
m - name; method to be used
t - positive; tolerance for the stopping condition
options - (optional) parameters of the form keyword = value, where keyword is one of stoppingcriterion, output, maxiterations, maxnumber, digits.
Description
The MatrixIterate command approximates the solution of a system of linear equations
There is no default method or default tolerance, they must be user specified.
It is assumed that the package LinearAlgebra has ben loaded before this procedure is read.
This command is part of the NumericalMethods package, so it can be used in the form MatrixIterate(..) only after executing the command with(NumericalMethods). However, it can always be accessed through the long form of the command by using NumericalMethods[MatrixIterate](..).
Options
method = jacobi, gaussseidel, or [sor,numeric]
- jacobi: the Jacobi iteration method (JIM):
- gaussseidel: the Gauss-Seidel iteration method (GSM):
- [sor,omega], where omega = numeric: the Successive over-relaxation method (SOR):
The options argument can be one or more of the following equations.
stoppingcriterion = relative, absolute, or residual
The criterion that the iteration must meet to stop (max-norm is used).
- relative: |xk - xk-1| / |xk| < tolerance
- absolute: |xk - xk-1| < tolerance
- residual: |b - Axk| < tolerance
The default value is stoppingcriterion = relative.
output = information, iterates, value, matrix, or matrices
Specifies what information is given by the procedure. In all cases, the iteration index k is printed at every iteration step, so that the user can see the progress and knows how many iterations it took to produce the final approximation.
- information returns the final numerical approximation of the solution. During iterations, data are shown at each step: the generated solution approximation, the residual of the current approximation and value test used in stoppingcriterion to determine whether the algorithm should stop.
- iterates returns the final numerical approximation of the solution. During iterations, generated solution approximations are shown.
- value returns the final numerical approximation of the solution.
- matrix: returns the matrix B corresponding to the iterative formula xk+1 = Bxk + c of the chosen method. For this output option, a shortened form of a procedure call is provided so that irrelevant data need not be inputed.
- matrices: returns the matrix B and vector c corresponding to the iterative formula
The default value is output = information if matrix dimension is less than 5 for real valued approximations and less than 3 for complex valued approximations, and output = iterates if matrix dimension is less than 7 for real valued approximations and less than 4 for complex valued approximations. Otherwise the default is output = value.
maxiterations = posint
The maximum number of iterations to perform. If it is reached, the algorithm is interrupted and a message is displayed. The return as defined by output is still produced.
The default value is maxiterations = 50.
maxnumber = positive
The maximal number that is not to be exceeded by vector entries during iterations. If it does happen, the algorithm is interrupted and a message is displayed. The return as defined by output is still produced.
The default value is maxnumber = 1000000.
digits = posint
Specifies how many decimal places should be shown when output = information or output = iterates is specified.
If digits is set to a value greater than Digits, the latter is set to digits for the duration of this procedure.
The default value is digits = Digits.
Examples
> with(LinearAlgebra):
A:=<<4,-1,1>|<0,5,-1>|<2,1,3>>;
b:=A.<1,-1,2>;
The matrix is strictly diagonally dominant, so iteration should yield the solution [1,-1,2]. We start with the Jacobi iteration method. Since for such small matrices the default output is information, we restrict the number of digits shown.
> MatrixIterate(A, b, xinit=<1.3,1.4,-2.3>, method=jacobi, tolerance=0.001, digits=5);
The Gauss-Seidel iteration should be faster. We restrict output to iterates. Note that the residual of the last approximation above is larger than tolerance, so we try to use it as stoppingcriterion.
> MatrixIterate(A, b, xinit=<1.3,1.4,-2.3>, method=gaussseidel, tolerance=0.001, stoppingcriterion=residual, output=iterates);
Of course, for badly conditioned matrices, residual is not a good guide.
> A:=<<1,-1,1,0,1>|<1,-1,3,-1,1>|<-1,1,1,2,1>|<1,-1,0,1,-1>|<1,5,-1,1,3>>:
b:=A.<1,-1,0,-1,1>:
For systems with dimension 5 and 6, output = iterates is the default.
> MatrixIterate(A, b, xinit=<1.,-1.3,1.4,1.3,-1.4>, method=gaussseidel, tolerance=0.001);
k=05 Numbers in iteration too large.
This does not look good, let's check on this iteration closer. We add output parameter to get the iteration matrix.
> B:=MatrixIterate(A, b, xinit=<1.,-1.3,1.4,1.3,-1.4>, method=gaussseidel, tolerance=0.001, output=matrix);
SpectralRadius(B);
The spectral radius larger than 1, which explains the divergence. The shorter form would work as well.
> B:=MatrixIterate(A, method=gaussseidel, output=matrix);
Let's go back to the first matrix.
> A:=<<4,-1,1>|<0,5,-1>|<2,1,3>>:
b:=A.<1,-1,2>:
The Gauss-Seidel method required five iterations. A bit of experimentation shows that SOR can improve on this. We try another output option.
> MatrixIterate(A, b, xinit=<1.3,1.4,-2.3>, method=[sor,21/20], tolerance=0.001, output=value);
k=00 k=01 k=02 k=03 k=04
Now we will try it manually.
> (B,c):=MatrixIterate(A, b, xinit=<1.3,1.4,-2.3>, method=[sor,21/20], tolerance=0.001, output=matrices);
> y:=<1.3,1.4,-2.3>:
for i from 1 to 4 do
y:=evalf(B.y+c):
end do:
y;
This matches our previous result quite well. Note that matrix calculations are automatically done with higher precision than the one specified by Digits when the LinearAlgebra package is used.
The last example will show that this procedure can handle complex iterations as well. For 3x3 matrices and complex iteration, the default output is iterates. We will ask for a smaller tolerance and adjust digits to it.
> MatrixIterate(A, b, xinit=<I,-1.3,1.4-I>, method=gaussseidel, tolerance=0.00001, digits=5);