Numec Assignment 1_2 (1)

3
MATLAB Source Code % Ask user to enter input matrix a=input('Please enter matrix:'); display(a) % Find dimensions of input matrix [m,n] = size(a); % m is number of row,n is number of column % If input matrix is not square, stop function if m ~= n % if number of row is not equal to the number of column disp('This is not a Square Matrix') end % If input matrix is square,use forward elimination if m == n % if number of row equal to the number of column for j = 1:n-1 % j is column of matrix for i = j+1:n % i is row of matrix f= a(i,j)/a(j,j); % f is factor a(i,j:n)=a(i,j:n)-f*a(j,j:n); % find new value for each row except first row end end det=1; % set the value of determinant equal to 1 for j=1:n det=det*a(j,j); % multiply the value diagonally to get determinant end display(det) end

description

numec

Transcript of Numec Assignment 1_2 (1)

Page 1: Numec Assignment 1_2 (1)

MATLAB Source Code % Ask user to enter input matrix a=input('Please enter matrix:');display(a) % Find dimensions of input matrix[m,n] = size(a); % m is number of row,n is number of column % If input matrix is not square, stop functionif m ~= n % if number of row is not equal to the number of column disp('This is not a Square Matrix') end % If input matrix is square,use forward eliminationif m == n % if number of row equal to the number of column for j = 1:n-1 % j is column of matrix for i = j+1:n % i is row of matrix f= a(i,j)/a(j,j); % f is factor a(i,j:n)=a(i,j:n)-f*a(j,j:n); % find new value for each row except first row end end det=1; % set the value of determinant equal to 1for j=1:n det=det*a(j,j); % multiply the value diagonally to get determinant

end display(det) end

Title: Write a programme that receives input from user and calculates the determinant of the input.

Page 2: Numec Assignment 1_2 (1)

MATLAB

Flow chart

FALSE

TRUE

Start

Input Matrix, a

If square matrixm = n

m=no. of row n= no. of column

Output No Determinant of Matrix a

End

for j = 1:n-1j = column of matrix

f= a(i,j)/a(j,j)f = factor

To get new matrix aa(i,j:n)=a(i,j:n)-f*a(j,j:n)

for i = j+1:ni = row of matrix

for j=1:n

Set det=1det= determinant

Value of determinantdet=det*a(j,j)

Output Determinant of Matrix a