In the iteration of Jacobi, we choose a matrix Q that is diagonal and whose diagonal elements are the same as those of matrix A. The matrix Q takes the form:
And the general equation can be written as
Qx(k) = (Q-A)x(k-1) + b
Si denominamos R a la matriz A-Q:
If we call R to the matrix A-Q:
Qx(k) = -Rx(k-1) + b
The product of the matrix Q by the column vector x(k) will be a column vector. Similarly, the product of the matrix R by the column vector x(k-1) will also be a column vector. The above expression, which is a vector equation, can be expressed by scalar nections (one for each vector component). In this way, we can write, for an element any, and taking into account that it is a matrix-vector product:
If we take into account that in the matrix Q all the elements outside the diagonal are zero, in the first member the only non-zero term of the summation is the one containing the diagonal element qii, which is precisely aii. Moreover, the elements of the diagonal of Rson zero, so we can eliminate the term i=j n the summation of the second member. Accordingly, the above expression can be rewritten as:
From where by clearing xi(k) we obtain:
Which is the expression given by the new components of the vector x(k) as a function of the previous vector x(k-1) in the Jacobi iteration. An algorithm for the Jacobi method is presented in the figure.
Figure: Implementación del método de Jacobi.
Jacobi's method is based on writing the system of equations in the form:
We start from an initial approximation for the solutions to the system of equations and substitute these values in the equation. In this way, a new approach to the system solution is generated, which in certain conditions is better than the initial approximation. This new approach can be replaced again on the right side of the equation and so on until convergence is achieved.
function iterjacobi
clc
clear
format short
disp(' ');
disp(' METODO DE JACOBI: ');
disp(' ');
disp(' METODO ITERATIVO UTILIZADO PARA SOLUCIONAR UN SISTEMA DE ');
disp(' ECUACIONES LINEALES. EN CADA ECUACION TENDREMOS VALORES ');
disp(' INICIALES PARA HALLAR NUEVOS VALORES Y ASI MISMO CON ESTOS ');
disp(' CALCULAR UNA NUEVA ITERACION. ');
disp(' ');
A=input(' INGRESE LA MATRIZ DE COEFICIENTES A= ');
b=input(' INGRESE LA MATRIZ DE TERMINOS INDEPENDIENTES b= ');
x=input(' INGRESE EL VECTOR (EN COLUMNAS) CON LAS APROXIMACIONES INICIALES= ');
tole=input(' INGRESE EL VALOR DE LA TOLERANCIA ');
detA=det(A);
if detA==0
disp(' EL DETERMINANTE ES CERO, EL SISTEMA NO TIENE SOLUCION UNICA ')
end
n=length(b);
D=diag(diag(A));
L=-tril(A,-1);
U=-triu(A,1);
fprintf('\n A= \n');
disp(' ');
disp(A)
disp(' ');
fprintf('\n LA MATRIZ D CALCULADA ES IGUAL A: \n')
disp(' ');
fprintf('\n D= \n');
disp(' ');
disp(D)
fprintf('\n LA MATRIZ L CALCULADA ES IGUAL A: \n')
disp(' ');
fprintf('\n L= \n');
disp(' ');
disp(L)
disp(' ');
fprintf('\n LA MATRIZ U CALCULADA ES IGUAL A: \n')
disp(' ');
fprintf('\n U= \n');
disp(' ');
disp(U)
disp(' ');
fprintf('\n SOLUCION: \n')
fprintf('\n LA MATRIZ DE TRANSICION DE JACOBI ES :\n')
Tj=inv(D)*(L+U);
disp(' ');
fprintf('\n Tj \n');
disp(' ');
disp(Tj);
respec=max(abs(eig(Tj)));
disp(' ');
fprintf(' EL RADIO ESPECTRAL ES IGUAL A %g \n',respec)
disp(' ');
if respec>1
disp(' EL RADIO ESPECTRAL ES MAYOR QUE 1 ')
disp(' EL METODO NO CONVERGE ')
return
end
fprintf('\n EL VECTOR DE ITERACION DE JACOBI ES: \n')
Cj=inv(D)*b;
disp(' ');
fprintf('\n Cj \n');
disp(' ');
disp(Cj);
i=0;
err=tole+1;
while err>tole
xinic=Tj*x+Cj;
err=norm(xinic-x);
x=xinic;
i=i+1;
end
fprintf('\n SOLUCION HALLADA EN %g ITERACIONES: \n',i)
disp(' ');
for in=1:n
fprintf(' X%g=%g\n',in,xinic(in))
end
disp(' ');
disp(' CON UN ERROR DE ');
disp(' ');
fprintf(' %g \n',err);
return
end