MATLAB: How to use for loops to calculate the determinant of the first n powers of 2×2 matrix (A) without using the implicit Matlab command “det”

determinantfor loophomeworkif statment

I am allowed to use the for loop as well as if/elseif/else statements to create the function but I am not sure how exactly to do this. The input will be a matrix A and a scalar value n. I began using if statements to make sure that the matrix is 2×2 and that n is positive however i do not know how to code for det(A^n) without using the det function. Below is an example of what i have thus far:
function ret = invertiblePowers(A,n)
if isequal(size(A), [2 2])==0
ret= disp('Matrix has wrong dimensions')
elseif floor(n)~=ceil(n)
ret= disp('n is not a positive integer')
elseif isequal(size(A), [2 2])==1 & floor(n)=ceil(n)

Best Answer

You're going to want a function that takes A and n as inputs and either returns a string or nothing at all. That would look like
function ret = invertiblePowers(A,n)
or
function [] = invertiblePowers(A,n)
Assuming you want to return a string, then just do something like
function ret = invertiblePowers(A,n)
ret = ''; % initialize ret to empty string
% Make sure A is the right size
if (~isequal(size(A),[2,2]))
ret = 'Matrix has wrong dimensions'; % note that 'disp' is not used
return; % stop working and exit out of this function
end
% Make sure n is positive integer
if ((floor(n)~=ceil(n)) || (n <=0))
ret = 'n is not a positive integer';
return;
end
% if A is 2x2 and n is positive integer, find det(A^n)
% It's unclear whether this function needs to find all
% determinates for I=1 up to n, or just n.
% Assuming you want 1:n, loop through
for i = 1:n
An = A^i; % raise A to the ith power
% find the determinate here, you need to do this part
detAn = YOUR MATH HERE;
% append results to return string
ret = [ret ; sprintf('n = %i : det(A^n) = %f',i,detAn)];
end % end for loop
end
Related Question