Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

H* 19-21st October 2009

- facultative -

Task: Generate a random 4 X 4 real matrix and a dimension 4 real column


vector. Solve the set of equations described by the generated matrices
using Gauss elimination. As the matrix of the system is randomly generated,
it is safer to involove total pivoting. *L
Clear@"`*"D;
n = 4;
H* Initial definition of matrices: *L
X = Table@xi , 8i, 1, n<D; sol = Table@0, 8n<D;
A = Table@Random@Real, 8-10, 10<D, 8i, 1, n<, 8j, 1, n<D;H*matrix of the system*L
b = Table@Random@Real, 8-10, 10<D, 8i, 1, n<D;H*the non-homogeneous terms*L
Ae = Table@0, 8n<, 8n + 1<D;
For@i = 1, i £ n, i ++, Ae@@iDD = Join@A@@iDD, 8b@@iDD<DD;H*the augmented matrix*L
Print@"A= ", A  MatrixFormD
Print@"b= ", b  MatrixFormD
Print@"Ae ", Ae  MatrixFormD
H* Solution given by Mathematica: *L
xmath = LinearSolve@A, bD;
Print@"The solution of the system given by Mathematica's function 'LinearSolve' is ",
MatrixForm@xmathDD;

H* Obtaining the upper triangular matrix using total pivoting: *L


H* Ordering@list,-1D gives the position of the largest element in the list.
pozcol and pozclin state the position of the largest
element of the column and of the list, respectively. *L
ForBk = 1, k £ n - 1, k ++, lin = Table@Ae@@k, iDD, 8i, k, n<D; col = Table@Ae@@i, kDD, 8i, k, n<D;
If@Max@Abs@linDD £ Max@Abs@colDD, pozlin = k - 1 + Ordering@Abs@colD, -1D@@1DD;
c = Ae@@kDD; Ae@@kDD = Ae@@pozlinDD; Ae@@pozlinDD = c,
pozcol = k - 1 + Ordering@Abs@linD, -1D@@1DD; B = Transpose@AeD;
d = B@@kDD; B@@kDD = B@@pozcolDD; B@@pozcolDD = d; Ae = Transpose@BD;
e = X@@pozcolDD; X@@pozcolDD = X@@kDD; X@@kDD = eD;

* Ae@@kDDF; Print@Ae  MatrixFormDF


Ae@@i, kDD
ForBi = k + 1, i £ n, i ++, Ae@@iDD = Ae@@iDD -
Ae@@k, kDD
Ae  MatrixForm H*the form of the augmented upper triangular matrix*L

Ae@@n, n + 1DD
sol@@nDD = ;
Ae@@n, nDD
Ae@@i, n + 1DD - Únj=i+1 Ae@@i, jDD * sol@@jDD
ForBi = n - 1, i ³ 1, i --, sol@@iDD = F;
Ae@@i, iDD
Print@"The solution of the system: ", X, "=", solD

You might also like