You are on page 1of 16

Solution of set of linear algebraic equations by the Jacobi method

Number of equations = 5

Coefficients of eq. 1 = [1 0 0 0 0]

Constant of eq. 1 = 1

Coefficients of eq. 2 = [0 5 0 0 0]

Constant of eq. 2 = 4

Coefficients of eq. 3 = [0 0 10 0 0 ]

Constant of eq. 3 = 5

Coefficients of eq. 4 = [0 0 0 7 0]

Constant of eq. 4 = 8

Coefficients of eq. 5 = [0 0 0 0 2]

Constant of eq. 5 = 12

Convergence criterion = 1e-3

Show step-by-step path to results (0/1) ? 1

Vector of initial guess = 0.5*ones(1,5)

Initial guess :

0.5 0.5 0.5 0.5 0.5


Iteration no. 1

1 0.8 0.5 1.14286 6

Iteration no. 2

1 0.8 0.5 1.14286 6

Results :

CA( 1) = 1

CA( 2) = 0.8

CA( 3) = 0.5

CA( 4) = 1.143

CA( 5) = 6

function x = Jacobi(A, c, x0, tol, trace)


%JACOBI Solves a set of linear algebraic equations by the
% Jacobi iterative method.
%
% JACOBI(A,C,X0) finds unknowns of a set of linear algebraic
% equations. A is the matrix of coefficients, C is the
vector
% of constants and X0 is the vector of initial guesses.
%
% JACOBI(A,C,X0,TOL,TRACE) finds unknowns of a set of linear
% algebraic equations and uses TOL as the convergence test.
% A nonzero value for TRACE results in showing calculated
% unknowns at the end of each iteration.
%
%
% Initialization
if nargin < 4 | isempty(tol)
tol = 1e-6;
end
if nargin >= 4 & tol == 0
tol = 1e-6;
end
if nargin < 5 | isempty(trace)
trace = 0;
end
if trace
fprintf('\n Initial guess :\n')
fprintf('%8.6g ',x0)
end

c = (c(:).')'; % Make sure it's a column vector


x0 = (x0(:).')'; % Make sure it's a column vector

n = length(c);
[nr nc] = size(A);

% Check coefficient matrix, vector of constants and


% vector of unknowns
if nr ~= nc
error('Coefficient matrix is not square.')
end
if nr ~= n
error('Coefficient matrix and vector of constants do not
have the same length.')
end
if length(x0) ~= n
error('Vector of unknowns and vector of constants do not
have the same length.')
end

% Check if the coefficient matrix is singular


if det(A) == 0
fprintf('\n Rank = %7.3g\n',rank(A))
error('The coefficient matrix is singular.')
end

% Building modified coefficient matrix and modified


% vector of coefficients
D = diag(diag(A)); % The diagonal matrix
a0 = inv(D)*A - eye(n); % Modified matrix of coefficients
c0 = inv(D)*c; % Modified vector of constants

x = x0;
x0 = x + 2 * tol;
iter = 0;

% Substitution procedure
while max(abs(x - x0)) >= tol
x0 = x;
x = c0 - a0 * x0;
if trace
iter = iter + 1;
fprintf('\n Iteration no. %3d\n',iter)
fprintf('%8.6g ',x)
end
end
FORWARD
DIFFERENC
E TABLE:
x y Dyo D2yo D3yo D4yo D5yo D6y0 D7yo D8yo D9y0 D10yo
0 0                    
    106                  
20 106   1388                
    1494   -1482              
40 1600   -94   2986            
    1400   1504   -5920          
60 3000   1410   -2934   8344        
    2810   -1430   2424   -5668      
80 5810   -20   -510   2676   -12228    
    2790   -1940   5100   -17896   66074  
-
100 8600   -1960   4590   -15220   53846  
194060
    830   2650   -10120   35950 0 -127986  
120 9430   690 0 -5530   20730   -74140    
    1520   -2880   10610   -38190      
140 10950   -2190   5080   -17460        
    -670   2200   -6850          
160 10280   10   -1770            
    -660   430              
180 9620   440                
    -220                  
200 9400 0
f(1) f(2)= f(3)= f(4)= f(5) f(6) f(7) f(8) f(9) f(10)
- - - -
x= 50 265 2602.5 -69.375 16.7926 55.45151 105.8601
463.125 116.641 40.7422 13.8379
f(x0)= 0
h= 20
q= 2.5
BACKWARD
DIFFERENCE
TABLE
Dyo D2yo D3yo D4yo D5yo D6y0 D7yo D8yo D9y0

106                
                 
1494 1388              
                 
1400 -94 -1482            
                 
2810 1410 1504 2986          
                 
2790 -20 -1430 -2934 -5920        
                 
830 -1960 -1940 -510 2424 8344      
                 
1520 690 2650 4590 5100 2676 -5668    
                 
-670 -2190 -2880 -5530 -10120 -15220 -17896 -12228  
                 
-660 10 2200 5080 10610 20730 35950 53846 66074
                 
-220 440 430 -1770 -6850 -17460 -38190 -74140 -12798
A=[1 1

02

-1 2];

>> rank(A)

ans =

2
>> rank(A'*A)

ans =

>> rank(A*A')

ans =

>> inv(A'*A)

ans =

0.5294 0.0588

0.0588 0.1176

%BY GAUSS ELIMINATION METHOD:

A=[2 1 0 0 0;
1 6 2 0 0;
0 2 6 1 0;
0 0 2 4 1;
0 0 0 2 3];

c = [1 4 5 8 12]';

x = Gauss (A , c)

function x = Gauss (A , c)
%GAUSS Solves a set of linear algebraic equations by the Gauss
% elimination method.
%
% GAUSS(A,C) finds unknowns of a set of linear algebraic
% equations. A is the matrix of coefficients and C is the
% vector of constants.
%
%

c = (c(:).')'; % Make sure it's a column vector

n = length(c);
[nr nc] = size(A);

% Check coefficient matrix and vector of constants


if nr ~= nc
error('Coefficient matrix is not square.')
end
if nr ~= n
error('Coefficient matrix and vector of constants do not
have the same length.')
end

% Check if the coefficient matrix is singular


if det(A) == 0
fprintf('\n Rank = %7.3g\n',rank(A))
error('The coefficient matrix is singular.')
end

unit = diag(ones( 1 , n)); % Unit matrix


order = [1 : n]; % Order of unknowns
aug = [A c]; % Augmented matrix

% Gauss elimination

for k = 1 : n - 1
pivot = abs(aug(k , k));
prow = k;
pcol = k;

% Locating the maximum pivot element


for row = k : n
for col = k : n
if abs(aug(row , col)) > pivot
pivot = abs(aug(row , col));
prow = row;
pcol = col;
end
end
end

% Interchanging the rows


pr = unit;
tmp = pr(k , :);
pr(k , : ) = pr(prow , : );
pr(prow , : ) = tmp;
aug = pr * aug;

% Interchanging the columns


pc = unit;
tmp = pc(k , : );
pc(k , : ) = pc(pcol , : );
pc(pcol , : ) = tmp;
aug(1 : n , 1 : n) = aug(1 : n , 1 : n) * pc;
order = order * pc; % Keep track of the column
interchanges

% Reducing the elements below diagonal to zero in the


column k
lk = unit;
for m = k + 1 : n
lk(m , k) = - aug(m , k) / aug(k , k);
end
aug = lk * aug;
end

x = zeros(n , 1);

% Back substitution
t(n) = aug(n , n + 1) / aug(n , n);
x(order(n)) = t(n);
for k = n - 1 : -1 : 1
t(k) = (aug(k,n+1) - sum(aug(k,k+1:n) .* t(k+1:n))) /
aug(k,k);
x (order(k)) = t(k);
end

OUTPUT:
x =

0.2802
0.4397
0.5409
0.8755
3.4163
% BY GAUSS JORDAN METHOD:

A=[1 1 0 0 0;
1 2 5 0 0;
0 2 3 1 0;
0 0 2 1 1;
0 0 0 5 4];
c = [1 4 5 8 12]';

x = Jordan (A , c)

function x = Jordan (A , c)
%JORDAN Solves a set of linear algebraic equations by the
% Gauss-Jordan method.
%
% JORDAN(A,C) finds unknowns of a set of linear algebraic
% equations. A is the matrix of coefficients and C is the
% vector of constants.
%
%
c = (c(:).')'; % Make sure it's a column vector

n = length(c);
[nr nc] = size(A);

% Check coefficient matrix and vector of constants


if nr ~= nc
error('Coefficient matrix is not square.')
end
if nr ~= n
error('Coefficient matrix and vector of constants do not
have the same length.')
end

% Check if the coefficient matrix is singular


if det(A) == 0
fprintf('\n Rank = %7.3g\n',rank(A))
error('The coefficient matrix is singular.')
end

unit = diag(ones( 1 , n)); % Unit matrix


order = [1 : n]; % Order of unknowns
aug = [A c]; % Augmented matrix

% Gauss - Jordan algorithm


for k = 1 : n
pivot = abs(aug(k , k));
prow = k;
pcol = k;
% Locating the maximum pivot element
for row = k : n
for col = k : n
if abs(aug(row , col)) > pivot
pivot = abs(aug(row , col));
prow = row;
pcol = col;
end
end
end

% Interchanging the rows


pr = unit;
tmp = pr(k , :);
pr(k , : ) = pr(prow , : );
pr(prow , : ) = tmp;
aug = pr * aug;

% Interchanging the columns


pc = unit;
tmp = pc(k , : );
pc(k , : ) = pc(pcol , : );
pc(pcol , : ) = tmp;
aug(1 : n , 1 : n) = aug(1 : n , 1 : n) * pc;
order = order * pc; % Keep track of the column
interchanges

% Reducing the elements above and below diagonal to zero


lk = unit;
for m = 1 : n
if m == k
lk(m , k) = 1 / aug(k , k);
else
lk(m , k) = - aug(m , k) / aug(k , k);
end
end
aug = lk * aug;
end

x = zeros(n , 1);

% Solution
for k = 1 : n
x(order(k)) = aug(k , n + 1);
end

OUTPUT:
x =

93.0000
-92.0000
19.0000
132.0000
-162.0000

% BY GAUSS JORDAN METHOD:

A=[12 8 -4 3 1;
1 4 5 8 10;
0 2 6 4 2;
6 7 2 3 1;
1 1 2 4 6];

c = [1 4 5 8 12]';

x = Jordan (A , c)

OUTPUT:

x =

7.5136
-3.0472
8.1724
-13.1261
7.2822
function gaussseidel(A, b, N)

%Gauss_seidel(A, b, N) solve iteratively a system of linear


equations
%whereby A is the coefficient matrix, and b is the right-hand
side column vector.
%N is the maximum number of iterations.
%The method implemented is the Gauss-Seidel iterative.
%The starting vector is the null vector, but can be adjusted
to one's needs.
%The iterative form is based on the Gauss-Seidel
transition/iteration matrix
%Tg = inv(D-L)*U and the constant vector cg = inv(D-L)*b.
%The output is the solution vector x.

n = size(A,1);
%splitting matrix A into the three matrices L, U and D
D = diag(diag(A));
L = tril(-A,-1);
U = triu(-A,1);

%transition matrix and constant vector used for iterations


Tg = inv(D-L)*U;
cg = inv(D-L)*b;

tol = 1e-05;
k = 1;
x = zeros(n,1); %starting vector

while k <= N
x(:,k+1) = Tg*x(:,k) + cg;
if norm(x(:,k+1)-x(:,k)) < tol
disp('The procedure was successful')
disp('Condition ||x^(k+1) - x^(k)|| < tol was met after
k iterations')
disp(k); disp('x = ');disp(x(:,k+1));
break
end
k = k+1;
end

if norm(x(:,k+1)- x(:,k)) > tol || k > N


disp('Maximum number of iterations reached without
satisfying condition:')
disp('||x^(k+1) - x^(k)|| < tol'); disp(tol);
disp('Please, examine the sequence of iterates')
disp('In case you observe convergence, then increase the
maximum number of iterations')
disp('In case of divergence, the matrix may not be
diagonally dominant')
disp(x');
end

A=[1 0 0 0 0 ;
0 5 0 0 0;
0 0 10 0 0;
0 0 0 7 0;
0 0 0 0 2];

b=[1
4
5
8
12];
n=10;
gaussseidel(A,b,n)

OUTPUT:
x =
1.0000
0.8000
0.5000
1.1429
6.0000

You might also like