MATLAB Answers

Gauss-Seidel Method in MATLAB

1.398 views (last 30 days)
Eric
Eric on 1 Sep 2013
Answered: Meysam Mahooti on 29 Nov 2019
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 Comment

MOHAMMED AL-THAMTHAMI
MOHAMMED AL-THAMTHAMI on 19 Apr 2018
|how to slove
A=[-4,1,0,1,0,0,0,0,0;1,-4,1,0,1,0,0,0,0;0,1,-4,1,0,1,0,0,0;1,0,0,-4,1,0,1,0,0;0,1,0,1,-4,1,0,1,0;0,0,1,0,1,-4,1,0,1;0,0,0,1,0,1,-4,1,0;0,0,0,0,1,0,-4,1;0,0,0,0,0,1,0,-4]; b=[0;0;-100;0;0;-100;0;0;-100]; xx=[-100:10;-100:10;-100:10;-100:10;-100:10;-100:10;-100:10;-100:10;-100:10];
NumIters=10;
[xx]=gauss_seidel(A,b,tol,xx)
|

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 1 Sep 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 Comments

Sign in to comment.


Maria Clara Carvalho Rameiro
I get the same error and I don't know how to fix it.

  0 Comments

Sign in to comment.


Meysam Mahooti
Meysam Mahooti on 29 Nov 2019
The Gauss–Seidel method is an iterative technique for solving a square system of n linear equations with unknown x.

  0 Comments

Sign in to comment.