using matlab you are requested to build your own code, if a matrix is diagonally
ID: 3908874 • Letter: U
Question
using matlab
you are requested to build your own code, if a matrix is diagonally dominated! If it is not diagonally dominated, your code will re-arrange the matrix itself to make it diagonally dominated
The given formula for diagonally dominted is |a_ii| >= sum(|a_ij|) where i~=j.
A helpful code
---------------------------------------
% swap i row with all its columns to j with all its columns
A([j i],:) = A([i j],:); % swap
b([j i]) = b([i j]); % swap i row with j row
---------------------------------------
If you are using GaussSeidel_matlab2.m, your code should be placed between the following
if m ~= n % if m Not equal n, error
error('Matrix A must be square');
end
% Start of checking if diagonally dominant
---------------------------------------
Here
---------------------------------------
% end of checking if diagonally dominant
C = A; % copy matrix A into C
Explanation / Answer
Hello Student!
I am happy to help you!
*************** The code goes here ********************
Save it as : dominantMatirx.m
function ans1 = dominantMatirx(Mat)
% Size of matrix
n = size(Mat,1);
% Initialization of variable
xIndex = 1;
yIndex = 1;
% The loop for the row number
for i = 1:n
% The sum value
sum = 0;
% Temp maxi value initialized
maxi = -1000000000;
% The loop for the column number
for j=1:n
% Sum value
sum = sum + abs(Mat(i,j));
% The maximum value is stored in maxi
if maxi < abs(Mat(i,j))
maxi = max(maxi, abs(Mat(i,j)));
xIndex = i;
yIndex = j;
end
end
% sum of the Mat(i,i) is lower than sum then -
if abs(Mat(i,i)) < sum
% if Mat(i,i) is lower than Mat(xIndex, yIndex) then -
if abs(Mat(i,i)) < abs(Mat(xIndex, yIndex))
% swap the elemet
temp = Mat(i,i);
Mat(i,i) = Mat(xIndex, yIndex);
Mat(xIndex, yIndex) = temp;
else
% Error it's not possible
Error('Not possible!');
return;
end
end
end
% return ans1
ans1 = Mat;
end
Save it as : test.m
clc
close all;
clear all;
lower_mat_A = [[1, 0, 0]; [2, 1, 0] ;[3, 4, 1]];
disp(dominantMatirx(lower_mat_A));
*************** The code ends here ********************
Input :
1 0 0
2 1 0
3 4 1
Output :
1 0 0
1 2 0
3 1 4
Thank you. Feel free to ask anything. If you like the answer. Please upvote it. It means a lot. Thank you again.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.