MATLAB Write a function that calculates the determinant of a matrix. You should
ID: 3696188 • Letter: M
Question
MATLAB
Write a 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
Gaus Estimation method:
[m,n)=size(a); [m,n]=size(a); for j=1:m-1 for z=2:m if a(j,j)==0 t=a(j,:);a(j,:)=a(z,:); a(z,:)=t; end end for i=j+1:m a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j)); end end x=zeros(1,m); for s=m:-1:1 c=0; for k=2:m c=c+a(s,k)*x(k); end x(s)=(a(s,n)-c)/a(s,s); end disp('Gauss elimination method:'); a x'Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.