Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

home / study / engineering / computer science / computer science questions and answers / you will write a function to calculate the determinant of

home / study / engineering / computer science / computer science questions and answers / you will write a function to calculate the determinant of a matrix. it should work for any ...

Question: You will write a function to calculate the determinant of a matrix. It should work for any size m...

Edit question

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. Dont forget comments and use reasonable variable names.

Exampleif the matrix passed to the function is [1 5 9 3, 3 4 7 2, 5 2 -2 5, 3 1 7 ] the determinant is 102.

Interim steps in the calculation:

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

use these local functions:

function bigRow=RowComp(matrix,row,col)

% function to return the row number with the greatest absolute

% value in the column passed

% including and below the row passed in the matrix passed.

% If the arguments are (matrix,2,2), the function will return the row that

% has the largest absolute value in or below row 2, and in column 2.

[rows cols]=size(matrix); % get dimension of matrix

bigNum=abs(matrix(row,col)); % assume first number is largest

bigRow=row; % assume row with first number

for i=row+1:rows % row loop beginning at next row

if abs(matrix(i,col)) > bigNum % if new number is greater

bigNum=abs(matrix(i,col)); % save new number

bigRow=i; % save new row

end % end if

end % end row loop

end % end function

function matrix=Swap(matrix,row1,row2)

% function to swap rows in a matrix

% exchange row1 with row2

[rows cols]=size(matrix);

for j=1:cols % loop through all columns

tmpNum=matrix(row1,j); % temporary storage

matrix(row1,j)=matrix(row2,j); % exchange rows

matrix(row2,j)=tmpNum; % restore temporary row

end % j - column loop

end % end function

function matrix=LinComb(matrix,row1,row2,alpha)

% function to make a linear combination of row1 with row2

% row2 = row1 * alpha + row2

[rows,cols]=size(matrix); % get matrix size

for j=1:cols % loop through all columns

matrix(row2,j)=matrix(row1,j)*alpha+matrix(row2,j);

end % j - column loop

end % end function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Repairing And Querying Databases Under Aggregate Constraints

Authors: Sergio Flesca ,Filippo Furfaro ,Francesco Parisi

2011th Edition

146141640X, 978-1461416401

More Books

Students also viewed these Databases questions

Question

=+what kinds of policies and practices should be developed?

Answered: 1 week ago

Question

=+ Of the HR issues mentioned in the case,

Answered: 1 week ago