Library: | nummath |
See also: | nmlevmarq |
Quantlet: | nmGJelim | |
Description: | Gauss-Jordan elimination with full pivoting |
Usage: | res = nmGJelim(a,b,{n,{m}}) | |
Input: | ||
a | n x n matrix, input matrix, coefficients of the linear system to be solved | |
b | n x m matrix, right-hand-side(s) of the linear system to be solved | |
n | (optional) positive scalar, order of the matrix a | |
m | (optional) positive scalar, number of right-hand sides | |
Output: | ||
ainv | n x n matrix, matrix inverse of a | |
x | n x m matrix, set of solution vectors |
Similarly, if m is given and matrix b has more than m columns, only its first m columns will be taken into account.
If n,m are not given (or they are not positive scalars), they will be defined as n = rows(a), m = cols(b); in this case, a has to be a square matrix.
If a is a singular matrix, the quantlet will produce an error message.
; solving the following linear system: ; 8 x2 + 3 x3 = 5 ; 4 x1 + x2 + 2 x3 = 16 ; 5 x1 - 7 x2 - 3 x3 = -2 library("nummath") a=#(0,4,5)~#(8,1,-7)~#(3,2,-3) b=#(5,16,-2) a b res=nmGJelim(a,b) res
Contents of a [1,] 0 8 3 [2,] 4 1 2 [3,] 5 -7 -3 Contents of b [1,] 5 [2,] 16 [3,] -2 Contents of res.ainv [1,] 0.14286 0.038961 0.16883 [2,] 0.28571 -0.19481 0.15584 [3,] -0.42857 0.51948 -0.41558 Contents of res.x [1,] 1 [2,] -2 [3,] 7
; solving the following linear systems: ; 2 x1 + x2 = 3; or = 1; or = 20 ; 4 x1 + 3 x2 = 7; or = 1; or = -14 library("nummath") a=#(2,4)~#(1,3)~#(500000,NaN) b=#(3,7)~#(1,1)~#(20,-14)~#(NaN,0) n=2 m=3 a b res=nmGJelim(a,b,n,m) res ; check the results: res.ainv * a[1:n,1:n] ; should be I a[1:n,1:n] * res.x ; should be b[,1:m]
Contents of a [1,] 2 1 5e+05 [2,] 4 3 +NAN Contents of b [1,] 3 1 20 +NAN [2,] 7 1 -14 0 Contents of res.ainv [1,] 1.5 -0.5 [2,] -2 1 Contents of res.x [1,] 1 1 37 [2,] 1 -1 -54 Contents of _tmp [1,] 1 0 [2,] 0 1 Contents of _tmp [1,] 3 1 20 [2,] 7 1 -14
; solving the following linear systems: ; 2 x1 + x2 = 3 ; 4 x1 + 2 x2 = 7 library("nummath") a=#(2,4)~#(1,2) b=#(3,7) a b res=nmGJelim(a,b)
Produces the error message "nmGJelim: Singular matrix (pivot is zero)!" and the following output: Contents of a [1,] 2 1 [2,] 4 2 Contents of b [1,] 3 [2,] 7 Error in assignment: Variable res not changed