Is a square matrix strictly diagonally dominant?

A square matrix A is strictly diagonally dominant if for all rows the absolute value of the diagonal element in a row is strictly greater than than the sum of absolute value of the rest of the elements in that row.

In this posting, I show a MATLAB program that finds whether a square matrix is strictly diagonally dominant by using two different methods. These are academic ways to reinforce programming skills in a student.

The MATLAB program can be downloaded as a Mfile (better to download it, as single quotes from the web-post do not translate correctly with the MATLAB editor). The html file showing the mfile and the command window output is also available.

%% IS A GIVEN SQUARE MATRIX STRICTLY DIAGONALLY DOMINANT?
% Language : Matlab 2007a
% Authors : Autar Kaw
% Last Revised : November 25, 2008
% Abstract: This program shows you two ways of finding out
% if a square matrix is diagonally dominant. A square matrix is
% diagonally dominant if for all rows the absolute value of the
% diagonal element in a row is strictly greater than than the sum
% of absolute value of the rest of the elements in that row
clc
clear all
disp(‘This program shows you two ways of finding out’)
disp(‘if a square matrix is diagonally dominant. A square matrix is’)
disp(‘diagonally dominant if for all rows the absolute value of the’)
disp(‘diagonal element in a row is strictly greater than than the sum’)
disp(‘of absolute value of the rest of the elements in that row’)
disp(‘ ‘)
%% INPUTS
% The square matrix
A=[-12 1 -7 2;1 3.4 1.1 1.1; 1 0 -4.5 0;10 1 1 10];
disp (‘INPUTS’)
disp(‘Here is the square matrix’)
A
disp(‘ ‘)

%% FIRST SOLUTION
% This is based on finding for how many rows the condition
% the absolute value of the diagonal element in a row is
% strictly greater than than the sum of absolute value
% of the rest of the elements in that row.

%size gives how many rows and columns in the A matrix
rowcol=size(A);
n=rowcol(1);
% count = for how many rows is the inequality met that
% the absolute value of the diagonal element in a row is
% strictly greater than than the sum of absolute value
% of the rest of the elements in that row
count=0;
for i=1:1:n
sumrow=0;
for j=1:1:n
if i~=j
sumrow=sumrow+abs(A(i,j));
end
end
if abs(A(i,i))>sumrow
count=count+1;
end
end
disp(‘FIRST WAY’)
if count==n
disp(‘Matrix is strictly diagonal dominant’)
else
disp(‘Matrix is NOT strictly diagonal dominant’)
end

%% SECOND SOLUTION
% This is based on finding for if for any row the condition
% the absolute value of the diagonal element in a row is
% strictly greater than than the sum of absolute value
% of the rest of the elements in that row is NOT met

%size gives how many rows and columns in the A matrix
rowcol=size(A);
n=rowcol(1);
% flag = keeps track if the condition is not met
% flag = 1 if matrix is strictly diagonally dominant
% flag = 2 if matrix is not strictly diagonally dominant

% Assuming matrix is strictly diagonally dominant
flag=1;
for i=1:1:n
sumrow=0;
for j=1:1:n
if i~=j
sumrow=sumrow+abs(A(i,j));
end
end
% As soon as the condition is not met, it is not a strictly
% diagonally dominant matrix
if abs(A(i,i))<=sumrow
flag=2;
break;
end
end
disp(‘ ‘)
disp(‘SECOND WAY’)
if flag==1
disp(‘Matrix is strictly diagonal dominant’)
else
disp(‘Matrix is NOT strictly diagonal dominant’)
end

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com and the textbook on Numerical Methods with Applications available from the lulu storefront.

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Skipping numbers in picking the lotto numbers

In a previous post, a reader wanted a program that would skip some numbers that he thinks would presumably not show up because they were picked in the previous week’s lotto.  Although the probability of the same numbers being picked is the same, human psychology and the lack of true randomness is a viable factor in writing such a program.  So here it is where you can input what numbers you do not want to see picked up by the program.

The MATLAB program can be downloaded as a Mfile (better to download it, as single quotes from the web-post do not translate correctly with the MATLAB editor).  The html file showing the mfile and the command window output is also available.

%% PICKING THE LOTTO NUMBERS WHILE SKIPPING SOME
% Language : Matlab 2007a
% Authors : Autar Kaw
% Last Revised : November 10, 2008
% Abstract: This program chooses randomly m unique numbers to play
% the lotto.  Lotto numbers allowed are positive integers from
% xlow to xhigh, while numbers asked to be skipped are not chosen
clc
clear all
disp(‘This program chooses randomly m unique numbers to play’)
disp(‘the lotto.  Lotto numbers allowed are positive integers from’)
disp(‘xlow to xhigh, while numbers asked to be skipped are not chosen’)
disp(‘  ‘)
%% INPUTS
% xlow= lowest integer allowed
xlow=1;
% xhigh = highest integer allowed
xhigh=53;
% number of integers to be picked
m=6;
% numbers to be skipped
lottoskip=[ 2  5  7 12  21];
disp (‘INPUTS’)
fprintf(‘Lowest integer allowed=%g’,xlow)
fprintf(‘\nHighest integer allowed=%g’,xhigh)
fprintf(‘\nNumbers to be picked=%g\n\n’,m)
disp(‘Lotto numbers skipped’)
lottoskip
disp(‘ ‘)
% Using the random number generator rand to get the lotto numbers.
% rand generates numbers between 0 and 1.  So we multiply that by xhigh-xlow+1
% and shift it by xlow and then floor it to get the integer part.
%% SOLUTION
% number of lotto numbers to be skipped from being chosen
mskip=length(lottoskip);
i=1;
while (i<=m)
lottonum(i)=floor(xlow+rand*(xhigh-xlow+1));
% flag= variable that keeps track of previous numbers
% being same or different from the number picked
% also it checks if the numbers asked to skipped are
% not chosen
flag=0;
% checking against previous numbers chosen
for j=1:1:i-1
if (lottonum(i)==lottonum(j))
flag=1;
end
end
% checking against numbers to be skipped
for j=1:1:mskip
if (lottonum(i)==lottoskip(j))
flag=1;
end
end
if flag==0
i=i+1;
end
end
%% OUTPUT
disp(‘ ‘)
disp(‘OUTPUT’)
disp(‘The lotto numbers picked are’)
fprintf(‘%g ‘,lottonum)
disp (‘  ‘)

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com.

An abridged (for low cost) book on Numerical Methods with Applications will be in print (includes problem sets, TOC, index) on December 10, 2008 and available at lulu storefront.

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Is a square matrix diagonal or not?

A square matrix A is diagonal if all the elements on the off-diagonal are zero. That is, A(i,j)=0 for i~=j.

In this posting, I show a MATLAB program that finds whether a square matrix is diagonal by using three different methods. These are academic ways to reinforce programming skills in a student.

The MATLAB program can be downloaded as a Mfile (better to download it, as single quotes from the web-post do not translate correctly with the MATLAB editor). The html file showing the mfile and the command window output is also available.

%% IS A GIVEN SQUARE MATRIX A DIAGONAL MATRIX?
% Language : Matlab 2007a
% Authors : Autar Kaw
% Last Revised : November 15, 2008
% Abstract: This program shows you three ways of finding out
% if a square matrix is a diagonal matrix. A square matrix is
% diagonal if all the off-diagonal elements are zero, that is
% A(i,j)=0 for i~=j.
clc
clear all
disp(‘This program shows you three ways of finding out’)
disp(‘if a square matrix is a diagonal matrix.’)
disp(‘A square matrix is diagonal if all the off-diagonal’)
disp(‘elements are zero, that is A(i,j)=0 for i~=j.’)
disp(‘ ‘)
%% INPUTS
% The square matrix
A=[1 0 0 0;0 3.4 0 0; 0 0 -4.5 0;0 0 0 0];
disp (‘INPUTS’)
disp(‘Here is the square matrix’)
A
disp(‘ ‘)

%% FIRST SOLUTION
% This is based on counting the number of zeros on
% off the diagonal. If this count is n^2-n then it
% is a diagonal matrix, otherwise it is not a diagonal matrix

%size gives how many rows and columns in the A matrix
rowcol=size(A);
n=rowcol(1);
% count = how many zeros not on the diagonal
count=0;
for i=1:1:n
for j=1:1:n
if A(i,j)==0 & i~=j
count=count+1;
end
end
end
disp(‘FIRST WAY’)
if count==n^2-n
disp(‘Matrix is diagonal’)
else
disp(‘Matrix is NOT diagonal’)
end

%% SECOND SOLUTION
% This is based on finding if any of the off-diagonal elements
% are nozero. As soon as this condition is met, the matrix can be
% deemed not diagonal. If the condition is never met, the matrix is
% diagonal

%size gives how many rows and columns in the A matrix
rowcol=size(A);
n=rowcol(1);
% flag = keeps track if it is diagonal or not
% flag = 1 if matrix is diagonal
% flag = 2 if matrix is not diagonal

% Assuming matrix is diagonal
flag=1;
for i=1:1:n
for j=1:1:n
% flag=2 if off-diagonal element is nonzero.
if A(i,j)~=0 & i~=j
flag=2;
end
end
end
disp(‘ ‘)
disp(‘SECOND WAY’)
if flag==1
disp(‘Matrix is diagonal’)
else
disp(‘Matrix is NOT diagonal’)
end

%% THIRD SOLUTION
% This is based on finding if the sum of the absolute value of
% the off-diagonal elements is nonzero.
% If the sum is nonzero, the matrix is NOT diagonal.
% If the sum is zero, the matrix is diagonal

%size gives how many rows and columns in the A matrix
rowcol=size(A);
n=rowcol(1);

% sum_off_diagonal= sum of absolute value of off-diagonal elements
sum_off_diagonal=0;
for i=1:1:n
for j=1:1:n
if i~=j
sum_off_diagonal=sum_off_diagonal+abs(A(i,j));
end
end
end

disp(‘ ‘)
disp(‘THIRD WAY’)
if sum_off_diagonal==0
disp(‘Matrix is diagonal’)
else
disp(‘Matrix is NOT diagonal’)
end

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com.

An abridged (for low cost) book on Numerical Methods with Applications will be in print (includes problem sets, TOC, index) on December 10, 2008 and available at lulu storefront.

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Picking lotto numbers

In these tough economic times, more people are playing lotto to hit it big.  We have written a MATLAB program that chooses unique and random numbers for a lotto.  The inputs are the number of numbers to pick, and the range in which the numbers can be picked.

The MATLAB program can be downloaded as a Mfile (better to download it, as single quotes from the web-post do not translate correctly with the MATLAB editor).  The html file showing the mfile and the command window output is also available.

Can you modify this program to put the numbers in an ascending order?

How does the random number generator work?

How can I get random numbers that are different every time I run the program?

%% PICKING THE LOTTO NUMBERS
% Language : Matlab 2007a
% Authors : Autar Kaw
% Last Revised : November 10, 2008
% Abstract: This program chooses randomly m unique numbers to play
% the lotto.  Lotto numbers allowed are positive integers from
% xlow to xhigh
clc
clear all
disp(‘This program chooses randomly m unique numbers to play’)
disp(‘the lotto.  Lotto numbers allowed are positive integers from’)
disp(‘xlow to xhigh’)
disp(‘  ‘)
%% INPUTS
% xlow= lowest integer allowed
xlow=1;
% xhigh = highest integer allowed
xhigh=53;
% number of integers to be picked
m=6;

disp (‘INPUTS’)
fprintf(‘Lowest integer allowed=%g’,xlow)
fprintf(‘\nHighest integer allowed=%g’,xhigh)
fprintf(‘\nNumbers to be picked=%g’,m)
disp(‘ ‘)
% Using the random number generator rand to get the lotto numbers.
% rand generates numbers between 0 and 1.  So we multiply that by xhigh-xlow+1
% and shift it by xlow and then floor it to get the integer part.
%% SOLUTION
i=1;
while (i<=m)
lottonum(i)=floor(xlow+rand*(xhigh-xlow+1));
% flag= variable that keeps track of previous numbers
% being same or different from the number picked
flag=0;
for j=1:1:i-1
if (lottonum(i)==lottonum(j))
flag=1;
end
end
if flag==0
i=i+1;
end
end
%% OUTPUT
disp(‘ ‘)
disp(‘OUTPUT’)
disp(‘The lotto numbers picked are’)
fprintf(‘%g ‘,lottonum)
disp (‘  ‘)

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com.

An abridged (for low cost) book on Numerical Methods with Applications will be in print (includes problem sets, TOC, index) on December 10, 2008 and available at lulu storefront.

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.