Gauss-Seidel Method in MATLAB

조회 수: 1,779 (최근 30일)
Eric
Eric 2013년 9월 1일
댓글: Imran Hossain 2021년 3월 13일
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
Imran Hossain
Imran Hossain 2021년 3월 13일
Mathlab report question amader sikher jonno khub valo lage

댓글을 달려면 로그인하십시오.

답변 (3개)

Meysam Mahooti
Meysam Mahooti 2019년 11월 29일
The Gauss–Seidel method is an iterative technique for solving a square system of n linear equations with unknown x.
  댓글 수: 1
Van Thuan Hoang
Van Thuan Hoang 2020년 9월 15일
편집: Van Thuan Hoang 2020년 9월 15일
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
Walter Roberson 2013년 9월 1일
The line
A = input(strA);
overwrites all of A each time through the loop, so A is going to end up as a scalar.

Maria Clara Carvalho Rameiro
Maria Clara Carvalho Rameiro 2018년 2월 21일
I get the same error and I don't know how to fix it.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by