Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a MATLAB function named \"determinant\" to find the determinant of a 3

ID: 3726798 • Letter: 1

Question

1. Write a MATLAB function named "determinant" to find the determinant of a 3 x 3 matrix using a subfunction to calculate each 2 x 2 matrix within as shown below. Begin your function by checking that the matrix is square. If it is not, display an error message "matrix must be square." You may use the built-in command "det0" to check your work, but you may not use it in your function. To get started, identify the following: Inputs to function: Function will return: First line of function 2. Upload the following to Moodle: (a) A MATLAB (*.m) file which calculates the determinant for any m x n square matrix. (b) A screen shot of your program running.

Explanation / Answer

Question 1 :

#create function determinant to find 3*3 determinant
function det = determinant(a,m,n)
[m,n]=size(a)#find size
if m!=n#of m is not equals to n
fprintf('Matrix must be square');
end
#calculate determinant of 3*3 matrix
arr1(2,2);
arr2(2,2);
arr3(2,2);
#create 2 d array
arr1(0,0)=a(1,1); arr1(0,1)=a(1,2); arr1(1,0)=a(2,1); arr1(1,1)=a(2,2);
arr2(0,0)=a(1,0); arr2(0,1)=a(1,2); arr2(1,0)=a(2,0); arr2(1,1)=a(2,2);
arr3(0,0)=a(1,0); arr3(0,1)=a(1,1); arr3(1,0)=a(2,0); arr3(1,1)=a(2,1);
det=(a(0,0)*deter(arr1)) - (a(0,1)*deter(arr2)) + (a(0,2)*deter(arr3));
end   
#calculate determinant of 2 d
function det = deter(a)
det=a(0,0)*a(1,1)-(a(0,1)*a(1,0));
end % end of sub-function


a=[1,2,3;4,5,6;7,8,9];
determinant(a)

Question 2:

function det = determinant(a,m,n)
[m,n]=size(a)#find size
if m!=n#of m is not equals to n
fprintf('Matrix must be square');
return;
end
#if only one element then return it
if m==1
det=a(0,0);
return;
end
arr(m-1,n-1);#create new array for determinant of sub matrix
i=0;#for first row
#process for each column of first row
for j = 0: 1: n-1
ini=0;inj=0;#indexing for new array
#store the elements into arr such that it doesnt store the the row and column corresponding to the element present at current i and j
for k=1:1:m-1#start row from 1 to avoid 0th row
for l=0:1:n-1
if l!=j #column must not be the same
arr(ini,inj)=a(k,l);#insert
if inj==n-2#if the j value is equal to its max jth value
ini=ini+1;#then increase its ith value
inj=0;#and make column as 0 (start frm new row cause previous column is completely filled)
else
inj=inj+1;#increase column iif not filled
end
end
end
end
d=determinant(arr)#find the determinant for this sub array
det=det+(power(-1,(i+j))*a(i,j)*d);#find determinant
end
end   
a=[1,2,3,4;3,5,6,7;6,7,9,9;4,5,7,8]
determinant(a)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote