Professional Documents
Culture Documents
Metodo Jacoby
Metodo Jacoby
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 = ,];
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
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
xo = x;
x = c0 - a0 * x0;
if trace
iter = iter + 1;
fprintf('\n Iteration no. %3d\n8,iter)
fprintf('%8.6g ' ,x)
end
end
Constant of eq. 2 = 0
Coefficients of eq. 3 = [O, 1100, 1240, 100]
Constant of eq. 3 = 0
Results :
CA(1) = 0.9091
CA( 2) = 0.6969
CA(3) = 0.6654
CA( 4) = 0.5856
Results :
CA( 1) = 0.9091
CA( 2) = 0.6969
CA( 3) = 0.6654
CA( 4) = 0.5856