Gauss Seidel Iretative Method

23 Ansichten (letzte 30 Tage)
Damian Andreani
Damian Andreani am 8 Mär. 2016
Beantwortet: Meysam Mahooti am 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?

Antworten (1)

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

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by