Complete a working program to iteratively solve a system of linear equations usi
ID: 2995197 • Letter: C
Question
Complete a working program to iteratively solve a system of linear equations using the Gauss-Seidel method.
Use the psuedo-code as a guide for the basic function.
psuedo-code:
function [x,ea, numIter] = myFinalGaussSeidel
es=0.0001;
maxIter=50;
A = [3 -.1 -.2
.1 7 -.3
.3,-.2,10];
bValue = [7.85 -19.3 71.4];
[maxRow,maxCol] = size(A);
x = [0 0 0];
ea = [1 1 1];
numIter = 0;
xTemp = x;
while (max(ea)>es && numIter < maxIter)
for rowNum = 1:maxRow
xTemp(rowNum)=bValue(rowNum);
for colNum=1:maxRow
if colNum~=rowNum
xTemp(rowNum) = xTemp(rowNum) - ...
A(rowNum,colNum)*x(colNum);
end
end
xTemp(rowNum) = xTemp(rowNum)/A(rowNum,rowNum);
if x(rowNum) ~= 0
ea(rowNum) = abs((xTemp(rowNum)-x(rowNum))...
/x(rowNum))*100;
end
x(rowNum) = xTemp(rowNum);
end
numIter = numIter + 1;
end
end
and here is what I got so far:
function MatrixWorkGaussSeidel
myFile = 'dataFile.txt';
[n,a,b]= ReadAFile(myFile);
disp(a);
disp(b);
end
function [n,a,b] = ReadAFile(Equations)
%input data
dataSet = load(Equations);
disp(dataSet);
[n,m] = size(dataSet);
for row= 1:n
for column = 1:m-1
%The data will have m rows and n (= m+1) columns
a(row,column ) = dataSet(row,column);
end
b(row) = dataSet(row,m);
end
% disp(a);
% disp(b);
end
function [xSol] = GaussSeidel(n,a,b)
maxRow = n;
maxCol = n;
epsi = 0.0001;
maxIter = 50;
Explanation / Answer
%add these lines after your code
x = [0 0 0];
ea = [1 1 1];
numIter = 0;
xTemp = x;
while (max(ea)>es && numIter < maxIter)
for rowNum = 1:maxRow
xTemp(rowNum)=b(rowNum);
for colNum=1:maxRow
if colNum~=rowNum
xTemp(rowNum) = xTemp(rowNum) - A(rowNum,colNum)*x(colNum);
end
end
xTemp(rowNum) = xTemp(rowNum)/A(rowNum,rowNum);
if x(rowNum) ~= 0
ea(rowNum) = abs((xTemp(rowNum)-x(rowNum))/x(rowNum))*100;
end
x(rowNum) = xTemp(rowNum);
end
numIter = numIter + 1;
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.