# please explain the working of 2nd ,3rd and 4th statements of this coding portion:

2 views (last 30 days)
muteen munawar on 15 Feb 2017
Commented: John Chilleri on 20 Feb 2017
for i = 1:n
j = 1:n;
j(i) = [];
B = abs(A(i,j));
Check(i) = abs(A(i,i)) - sum(B);
if Check(i) < 0
fprintf('The matrix is not strictly diagonally dominant at row %2i\n\n',i)
end
end

John Chilleri on 15 Feb 2017
Hello,
This code checks to see if the diagonal elements of a given matrix A (assuming n x n) are larger in magnitude than the sum of the magnitude of the non-diagonal elements in its row.
Line by line explanation:
The first line loops through all the rows of A.
for i = 1:n
The second line is defining j = [1 2 3 ... n], so a vector containing 1..n,
j = 1:n;
The third line is removing the diagonal element, aka, the ith row and this removes the ith column, so the i,i th element,
j(i) = [];
The fourth line takes the absolute value of all the elements in the ith row of A, excluding the diagonal element: A(i,j) = A(ith row, elements 1,2,...,i-1,i+1,...,n) since we removed the ith element from j previously.
B = abs(A(i,j));
The fifth line computes the absolute value of the diagonal element minus the sum of the absolute value of all the other elements in the row. If Check is positive, we know the magnitude of the diagonal element was greater than the sum of all the magnitudes of the other elements in its row.
Check(i) = abs(A(i,i)) - sum(B);
The sixth line checks if it's less than zero, in which case the diagonal elements magnitude is not greater than or equal to the sum of the magnitudes of the other elements.
if Check(i) < 0
If this is the case, the matrix is not strictly diagonally dominant, which the seventh line prints.
fprintf('The matrix is not strictly diagonally dominant at row %2i\n\n',i)
The eighth and ninth lines end the cycle.
end
end
Sorry if this was too much detail,
Hope this helps!
John Chilleri on 20 Feb 2017
Initializing number of iterations to 0:
iteration = 0;
Continue to iterate until the maximum value of the Error_eval vector is less than 0.001
while max(Error_eval) > 0.001
Increase iteration each time loop commences
iteration = iteration + 1;
Recompute vector X
Z = X;
for i = 1:n
j = 1:n;
j(i) = [];
Xtemp = X;
Xtemp(i) = [];
X(i) = (C(i) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Store the X from this iteration (storing all X's as it approaches a small enough error)
Xsolution(:,iteration) = X;
Compute error of X from iteration (this will determine if while loops continues iterating or not)
Error_eval = sqrt((X - Z).^2);
End of while loop
end
Displaying results
%%Results %%
GaussSeidelTable = [1:iteration;Xsolution]'
MaTrIx = [A X C]
Hope this helps!

### Tags

No tags entered yet.

### Community Treasure Hunt

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

Start Hunting!