Gauss-Seidel Method in MATLAB
1.746 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have to write two separate codes for the Jacobi method and Gauss-Seidel
The question exactly is: "Write a computer program to perform jacobi iteration for the system of equations given. Use x1=x2=x3=0 as the starting solution. The program should prompt the user to input the convergence criteria value, number of equations and the max number of iterations allowed and should output the solution along with the number of iterations it took for the solution to convergence to the user specified value."
Gauss-Seidel:
%*************************Eric Douglas*****************************%
%***********************August 30th, 2013**************************%
%This code is used to compute the Jacobi Method of a certain matrix.%
%Input:
% C_n = convergence criteria value
% N = number of equations in the matrix
% Imax = the maximum number of iterations
%Output:
% S = the solution( M x 1 matrix ; jacobi approximation)
% j = the number of iterations it took to
% converge to the user inputed value
% R = Residual Values
%establishes the variables needed
%B is an M x 1 matrix
%A is an M x M matrix
%P is the initial M x 1 matrix
%Z = remembering matrix
%Ask the user for each input statement required
Imax = input('What do you want the maximum iteration to be? ');
N = input('How many equations do you want? ');
C_n = input('What number do you want to converge to? ');
%Assigns the values inputed by the user into the matrices
for x=1:1:N
for y=1:1:N
strA = ['What do you desire your numbers in the matrix to be? ' num2str(x) 'Row: ' num2str(y) 'Column: '];
A = input(strA);
end
end
for l=1:1:N
strB = ('What do you desire the Solution matrix to be? ');
B = input(strB);
end
n = length(B);
X = zeros(n,1);
e = ones(n,1);
%%Check if the matrix A is diagonally dominant
for i = 1:n
j = 1:n;
j(i) = [];
C = abs(A(i,j));
Check(i,1) = abs(A(i,i)) - sum(B); % Is the diagonal value greater than the remaining row values combined?
if Check(i) < 0
fprintf('The matrix is not strictly diagonally dominant at row %2i\n\n',i)
end
end
Main function for Gauss-Seidel
iteration = 0;
while max(e) > C_n%check error
iteration = iteration + 1;
Z = X; % save current values to calculate error later
for i = 1:N
j = 1:N; % define an array of the coefficients' elements
j(i) = []; % eliminate the unknow's coefficient from the remaining coefficients
Xtemp = X; % copy the unknows to a new variable
Xtemp(i) = []; % eliminate the unknown under question from the set of values
X(i,1) = (B(i,1) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xs = X;
e = sqrt((X - Z).^2);
end
%%Display Results
Xs
iteration
There is an error at the line ("X(i,1) = (B(i,1) - sum(A(i,j) * Xtemp)) / A(i,i);") and it says "exceeds matrix index". I dont understand how to change this.
Also, if you see anything else wrong then I am open to comments/hints.
1 comentario
Respuestas (3)
Meysam Mahooti
el 29 de Nov. de 2019
The Gauss–Seidel method is an iterative technique for solving a square system of n linear equations with unknown x.
1 comentario
Van Thuan Hoang
el 15 de Sept. de 2020
Editada: Van Thuan Hoang
el 15 de Sept. de 2020
1. This command line
sum(abs(err) >= tol) ~= zeros(na,1)
may become
sum(abs(err) >= tol) ~= 0
to be more accurate.
2. In some cases, the while loop will never stop even the covergence condition is checked. It could be better to add some commands as following:
kmax=1000;
while sum(abs(err) >= tol)~=0 && k<=kmax
x(:,k+1) = -inv(D+L)*(U)*x(:,k) + inv(D+L)*b;% Gauss-Seidel formula
err = x(:,k+1) - x(:, k);% finding error
k = k + 1;
end
3. Your code may be improved to speed up the calculation such as:
U = triu(A)- diag(diag(A));
Linv = inv(A-U);
AM = -Linv*U;
B = Linv*b;
kmax=1000;
while sum(abs(err) >= tol)~=0 && k<=kmax
x(:,k+1) = AM*x(:,k) + B;% Gauss-Seidel formula
err = x(:,k+1) - x(:, k);% finding error
k = k + 1;
end
The speed improvement may need to be able to apply this code for very large matrix equation.
Regards,
Walter Roberson
el 1 de Sept. de 2013
The line
A = input(strA);
overwrites all of A each time through the loop, so A is going to end up as a scalar.
0 comentarios
Maria Clara Carvalho Rameiro
el 21 de Feb. de 2018
I get the same error and I don't know how to fix it.
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!