You are on page 1of 4

Program

Example2_3.m
% Example2-3 .m
% Solution to Example 2.3. This program solves a set of
% linear algebraic equations by the Jacobi iterative
% method, using the function JACOBI.M, to find the
% concentrations of a seres of CSTRs.
clc
clear

% Input data
print(' Solution of set of linear algebraic equations by the
Jacobi method\n\n)
n = input(' Number of equations = ');
for k=l: n
fprintf(\n Coefficients of eq. %2d =',k)

A(k,l:n)= input(' );
fprintf(' constant of eq. %2d =',k)
c(k) = input(' );
end

disp( ' )
tol = input(' Convergence criterion = ');
trace = input(' Show step-by-step path to results (0/1) ? ');

redo = 1;

while redo
disp( ' )
guess = input(' Vector of initial guess = ,];

ca = Jacobi(A, c, guess, tol, trace);


fprintf('\n\n Results :\n')

for k=l :n
fprintf (' CA(%2d) = %6.4g\n,k ,ca(k))
end

disp(' ')
redo=input(' Repeat the calculations with another guess (0/1)? '):
disp( ' ' )
end

Jacobi.m
function x = Jacobi(A, c, xO, tol, trace)
%JACOB1 Solves a set of linear algebraic equations by the
% Jacobi iterative method.
%
% JACOBI(A,C,XO) finds unknowns of a set of linear algebraic
% equations. A is the matrix of coefficients, C is the vector
% of constants and XO is the vector of initial guesses.
P;
8 JACOBI(A,C,XO,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.
%
% See also GAUSS, JORDAN
% by N. Mostoufi & A. Constantinides
% January 1. 1999
% Initialization
if nargin < 4 | isempty(tol)
tol = le-6;
end
if nargin >= 4 & tol == 0
tol = le-6;
end
if nargin < 5 | isempty(trace)
trace = 0;
end

if trace
fprintf('\n Initial guess :\n)
fprintf('%8.6g ',xO)
end

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


x0 = (xO(:).')'; % 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
k 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;
xO = x + 2 * to1;
iter = 0;

% Substitution procedure

while max(abs(x - x0)) >= to1

xo = x;
x = c0 - a0 * x0;
if trace
iter = iter + 1;
fprintf('\n Iteration no. %3d\n8,iter)
fprintf('%8.6g ' ,x)
end
end

Input and Results

Solution of set of linear algebraic equations by the Jacobi method


Number of equations = 4

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


Constant of eq. 1 = 1000
Coefficients of eq. 2 = [1000, -1400, 100, 0]

Constant of eq. 2 = 0
Coefficients of eq. 3 = [O, 1100, 1240, 100]
Constant of eq. 3 = 0

Coefficients of eq. 4 = [0, 0, 1100, -1250]


Constant of eq. 4 = 0

Convergence criterion = le-5

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

Vector of initial guess = 0.6*ones(1,4)


Initial guess:

0.6 0.6 0.6 0.6


Iteration no. 1
0.909091 0.471429 0.580645 0.528
Iteration no. 2
0.909091 0.690825 0,460783 0.510968
Iteration no. 3
0.909091 0.682264 0.654036 0.405489
Iteration no. 4
0,909091 0.696068 0.637935 0.575552
Iteration no. 5
0.909091 0.694917 0.663895 0.561383
Iteration no. 6
0.909091 0.696772 0.661732 0.584227
Iteration no. 7
0,909091 0.696617 0.665219 0.582324
Iteration no. 8
0.909091 0.696866 0.664928 0.585393
Iteration no. 9
0.909091 0.696846 0.665397 0.585137
Iteration no. 10
0,909091 0.696879 0,665358 0.585549
Iteration no. 11
0.909091 0.696876 0.665421 0,585515
Iteration no. 12
0.909091 0.696881 0.665416 0.58557
Iteration no. 13
0.909091 0,69688 0.665424 0.585566

Results :
CA(1) = 0.9091
CA( 2) = 0.6969
CA(3) = 0.6654
CA( 4) = 0.5856

Repeat the calculations with another guess (0/1) ? 1


Vector of initial guess = 100*ones(1,4)
Initial guess :
100 100 100 100
Iteration no. 1
0.909091 78.5714 96.7742 8 8
Iteration no. 2
0.909091 7.56179 76.7972 85.1613
Iteration no. 3
0.909091 6.13487 13.5759 67.5816
Iteration no. 4
0.909091 1.61906 10.8923 11.9468
Iteration no. 5
0.909091 1.42738 2.39971 9.58527
Iteration no. 6
0.909091 0,820759 2.03923 2.11175
Iteration no. 7
0.909031 0.79501 0.898394 1.79452
Iteration no. 8
0.909091 0.713522 0.84997 0.790587
Iteration no. 9
0.909091 0,710063 0,69672 0.747973
Iteration no. 10
0.309091 0,699116 0.690215 0,613113
Iteration no. 11
0.909091 0.698652 0.669628 0.607389
Iteration no. 12
0.909091 0.697181 0.668755 0.589273
Iteration no. 13
0.909091 0,697119 0.665989 0,588504
Iteration no. 14
0.909091 0.696921 0,665872 0.586071
Iteration no. 15
0.909091 0.696913 0.6655 0,585967
Iteration no. 16
0.909091 0.696886 0.665485 0.58564
Iteration no. 17
0.909091 0,696885 0,665435 0.585626
Iteration no. 18
0.909091 0.696882 0.665433 0.585583
Iteration no. 19
0.909091 0,696882 0.665426 0.585581

Results :

CA( 1) = 0.9091
CA( 2) = 0.6969
CA( 3) = 0.6654
CA( 4) = 0.5856

Repeat the calculations with another guess (0/1)? 0

You might also like