comparison toolbox/gj.m @ 2:c124219d7bfa draft

Re-add the 1995 toolbox after noticing the statement in the ~higham/mctoolbox/ webpage.
author Antonio Pino Robles <data.script93@gmail.com>
date Thu, 07 May 2015 18:36:24 +0200
parents 8f23314345f4
children
comparison
equal deleted inserted replaced
1:e471a92d17be 2:c124219d7bfa
1 function x = gj(A, b, piv)
2 %GJ Gauss-Jordan elimination to solve Ax = b.
3 % x = GJ(A, b, PIV) solves Ax = b by Gauss-Jordan elimination,
4 % where A is a square, nonsingular matrix.
5 % PIV determines the form of pivoting:
6 % PIV = 0: no pivoting,
7 % PIV = 1 (default): partial pivoting.
8
9 n = max(size(A));
10 if nargin < 3, piv = 1; end
11
12 for k=1:n
13 if piv
14 % Partial pivoting (below the diagonal).
15 [colmax, i] = max( abs(A(k:n, k)) );
16 i = k+i-1;
17 if i ~= k
18 A( [k, i], : ) = A( [i, k], : );
19 b( [k, i] ) = b( [i, k] );
20 end
21 end
22
23 irange = [1:k-1 k+1:n];
24 jrange = k:n;
25 mult = A(irange,k)/A(k,k); % Multipliers.
26 A(irange, jrange) = A(irange, jrange) - mult*A(k, jrange);
27 b(irange) = b(irange) - mult*b(k);
28
29 end
30
31 x = diag(diag(A))\b;