MATLAB Answers

Gauss Seidel Iretative Method

16 views (last 30 days)
Damian Andreani
Damian Andreani on 8 Mar 2016
Answered: Meysam Mahooti on 29 Nov 2019
Hello,
I am a structural engineer and our matrices consist of many 0s. Below is my code for using the Gauss seidel method to solve my matrix formula but I am having trouble when dividing by A(i,i) when A(i,i) is 0.
s = ft_GaussSeidel(A,b)
clear all; close all; clc;
A = [.707 1 0 0 0 0 0 0 0 0 0 0 0;
-.707 0 1 0 0 0 0 0 0 0 0 0 0;
.7071 0 0 1 0 0 0 0 0 0 0 0 0;
0 -1 0 0 .659 1 0 0 0 0 0 0 0;
0 0 0 -1 -.753 0 0 0 0 0 0 0 0;
0 0 -1 0 -.659 0 1 0 0 0 0 0 0;
0 0 0 0 .753 0 0 1 0 0 0 0 0;
0 0 0 0 0 -1 0 0 .659 1 0 0 0;
0 0 0 0 0 0 0 -1 -.753 0 0 0 0;
0 0 0 0 0 0 -1 0 -.659 0 1 0 0;
0 0 0 0 0 0 0 0 .735 0 0 1 0;
0 0 0 0 0 0 0 0 0 -1 0 0 .707;
0 0 0 0 0 0 0 0 0 0 0 -1 -.707];
b = [0;
2000;
-6229;
0;
600;
0;
0;
0;
800;
0;
2429;
0;
600];
k = 0; % iteration counter
x_prev = ones(size(b)); % temporary x
x = ones(size(b))*2; % x array (start with 2 not 0s)
% table headings output
count = zeros(size(x));
fprintf(' k| ')
for i = 1:size(x)
count(i) = i;
fprintf('%-1c%-10s', 'x', num2str(count(i)))
end
%Calculation code
for k = 1
x_prev = x; % for checking when iteration should end
for i = 1:2 %:length(x) % row loop
%x(i) calculation loops
x(i) = b(i);
for ii = 1:length(x) % column loop
if ii ~= i % does not use column of x(i)
%%%%%%DivBy0Error%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%x(i) = x(i)-A(i,ii)*x(ii)
end
end
x(i) = x(i)/A(i,i);
end
fprintf('\n %2g|',k);
fprintf(' %-10.2e',x);
end
% end
For example the calculation for x2 would be:
x2 = (b2 +.7071*x1)/A(i,i) | A(i,i) = 0
Would my only option be to make a code to get the diagonals of the matrix to be non-zero? Or can I do something to this?

  0 Comments

Sign in to comment.

Answers (1)

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.