PLEASE HELP! Write a MatLab function that calculates the determinant of a matrix
ID: 3698020 • Letter: P
Question
PLEASE HELP! Write a MatLab function that calculates the determinant of a matrix. You should continue the Gauss Elimination down the diagonal to the last row. Count the number of times you switch rows because each row exchange changes the sign of the determinant. When the matrix is in upper-triangular form, the determinant is the product of the diagonal elements with the proper sign. Your function should accept a square matrix of any size as an argument and return the scalar determinant. Note: Your function should be written to handle matrices of any size. Test your function with matrices of different sizes for which you know the solution to ensure that your function works correctly. Use variable names that relate to their function and include comments that explain your program logic. Include all the functions your main function uses as local functions in the same script. We must be able to run your function to determine if it works correctly. Do not use any built-in MATLAB functions except size(), input(), fprintf() and zeros().
Explanation / Answer
function C = gauss_elimination(A,B) % defining the function
A= [ 1 2; 4 5] % Inputting the value of coefficient matrix
B = [-1; 4] % % Inputting the value of coefficient matrix
i = 1; % loop variable
X = [ A B ];
[ nX mX ] = size( X); % determining the size of matrix
while i <= nX % start of loop
if X(i,i) == 0 % checking if the diagonal elements are zero or not
disp('Diagonal element zero') % displaying the result if there exists zero
return
end
X = elimination(X,i,i); % proceeding forward if diagonal elements are non-zero
i = i +1;
end
C = X(:,mX);
function X = elimination(X,i,j)
% Pivoting (i,j) element of matrix X and eliminating other column
% elements to zero
[ nX mX ] = size( X);
a = X(i,j);
X(i,:) = X(i,:)/a;
for k = 1:nX % loop to find triangular form
if k == i
continue
end
X(k,:) = X(k,:) - X(i,:)*X(k,j); % final result
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.