Question

In: Electrical Engineering

Matlab You will write a function to calculate the determinant of a matrix. It should work...

Matlab

You will write a function to calculate the determinant of a matrix. It should work for any size matrix. Remember that the determinant can be calculated by multiplying the diagonal elements of an upper right triangular matrix. Your function will take a matrix passed to it and put it in upper right triangular form. You will work down the diagonal beginning at row 1 column 1, then row 2 column 2, etc. Note that the row and column numbers are the same on the diagonal. You will put zero in each column below the diagonal elements by making linear combinations of rows. Before you make linear combinations down a column, swap rows (if needed) to put the row with the largest value on the diagonal. Also remember, each time you swap rows, the determinant changes sign so count of how many row swaps are necessary and adjust the sign of the determinant accordingly. A matrix must be square to have a determinant. If the matrix passed to your function is not square, return the text ‘Matrix must be square’.

There are functions included that you MUST use in your function. These will be local functions that follow your main function. You must use ALL the functions. The functions are: RowComp(matrix, row, col) which returns the row with the largest absolute value in the column passed to the function beginning at the row passed to the function; Swap(matrix, row1, row2) which returns a matrix with row1 and row2 exchanged; and LinComb(matrix, row1, row2, alpha) which returns a matrix with row2 replaced by row1*alpha+row2. These functions will make your function much easier to write and debug.

Your function will be checked with a different matrix of a different size. Don’t forget comments and use reasonable variable names.

Example—if the matrix passed to the function is the determinant is 102.

Interim steps in the calculation:

1. Swap row 3 and row 1

     5     2    -2     5

     3     4     7     2

     1     5     9     3

     3     1     7    -3

2. Zeros in column 1

    5.0000    2.0000   -2.0000    5.0000

         0    2.8000    8.2000   -1.0000

         0    4.6000    9.4000    2.0000

         0   -0.2000    8.2000   -6.0000

3. Swap row 2 and row 3

5.0000    2.0000   -2.0000    5.0000

         0    4.6000    9.4000    2.0000

         0    2.8000    8.2000   -1.0000

         0   -0.2000    8.2000   -6.0000

4. Zeros in column 2

5.0000    2.0000   -2.0000    5.0000

         0    4.6000    9.4000    2.0000

         0         0    8.6087   -5.9130

         0         0    2.4783   -2.2174

5. Continue until all rows have been completed and zeros are in all columns below the diagonal.

6. Calculate the determinant by multiplying all the diagonal terms together and adjust the sign of the determinant by the number of row swaps.

Solutions

Expert Solution

ANSWER :

function D = det_row(matrix)

%function calculates determinant of matrix using elementary row operations

%some user defined functions given in problem are used in this function

%save this function along with other functions, in same folder

[m,n]=size(matrix);   

%calculating size of matrix to check whether given is square or not

mmsg='matrix should be square';

if m~=n

error(mmsg)   

%error message if not square matrix

end

sp=0; %count of row swaping

for i=1:1:m-1 %i =row1, this loop run through all rows

r=RowComp(matrix,i,i); %finding largest element row

if r~=i

matrix=Swap(matrix,i,r);  

%swaping if row is other than i'th row

sp=sp+1; %add swap count

end

for j=i+1:1:m

%j =row2, this loop run through all rows after row1

alpha=-(matrix(j,i)/matrix(i,i)); %find alpha

matrix=LinComb(matrix,i,j,alpha); %based on alpha find new jth row

end

end

D=prod(diag(matrix))*(-1)^sp; %product of diagonal elements of latest matrix

end

OUTPUT:


Related Solutions

Write a MATLAB function function = myMatrixInveesion(....) to calculate matrix inversion by implementing LU decomposition, forward...
Write a MATLAB function function = myMatrixInveesion(....) to calculate matrix inversion by implementing LU decomposition, forward and backward substitution procedures. Do NOT use the built-in "lu" or "inv" commands in your code. You will need to employ Nested Loops. Thank you! function_____ = myMatrixInversion(_____)
Write a Matlab function for a matrix that takes in a matrix in echelon form and...
Write a Matlab function for a matrix that takes in a matrix in echelon form and will return the row canonical form. The function cannot use rref, or any other matlab built in functions.
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate...
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate the sum of each column. Use subscript notation to find the 6th element and the last element of each magic square. Find the maximum of all the elements in each magic square.
Find the adjoint of matrix A, the determinant of matrix A, and the determinant of the...
Find the adjoint of matrix A, the determinant of matrix A, and the determinant of the adjoint A. A= 1 1 0 2 2 1 1 0 0 2 1 1 1 0 2 1
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
MATLAB Function: (Backward substitution) Write a function that takes an upper triangular matrix U, and a...
MATLAB Function: (Backward substitution) Write a function that takes an upper triangular matrix U, and a vector b as input and returns the solution of the linear system U x = b. Use this function in the case when U is made of the numbers [(3,2,1),(0,5,4),(0,0,6)], and b = [1,1,−6]^T.
In economics, What is the role or function of the Determinant of a Vandermonde Matrix?
In economics, What is the role or function of the Determinant of a Vandermonde Matrix?
Make a code in matlab to know the determinant of a matrix n x n, using...
Make a code in matlab to know the determinant of a matrix n x n, using the sarrus rule.
use matlab to create a function that finds inverse of 2x2 matrix and returns result. Should...
use matlab to create a function that finds inverse of 2x2 matrix and returns result. Should be a error check to make sure matrix is 2x2, and nonsingular.
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix...
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix of size 2 × 2 and returns back the inverse of that matrix if one exists. If no inverse exists, print back a suitable error message. You can safely assume that we will test your code on square matrices of size 2 × 2 only. You can always verify your answer by using the inbuilt inverse function in MATLAB, however, you cannot use the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT