How to use for loops to calculate the determinant of the first n powers of 2x2 matrix (A) without using the implicit Matlab command "det"

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 2x2 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)

 Accepted 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

More Answers (2)

This together with the fact that with SVD, the N'th power of the matrix can be found by taking the N'th power of the diagonal.
Or you could just use the formula for the determinant of a 2 x 2 matrix.

4 Comments

would the following make sense? I am supposed to take use the values of the determinant to determine if it is invertible or not then display a particular string
function ret = invertiblePowers(A,n)
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
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)
ret=prod(diag(A).^n)
elseif prod(diag(A).^n)==0
ret=disp('det(A^k) = 0 so A^k is singular')
elseif prod(diag(A).^n)~=0
ret=disp('det(A^k) = A_k so A^k is invertible')
A few issues that I can see right away:
The way you have your function set up, 'ret' is going to be your returned variable. This doesn't match up with using
ret = disp('Matrix has wrong dimensions')
If you try to test out using disp in this way you'll get an error telling you you have too many output arguments.
Also, checking if floor(n) ~= ceil(n) doesn't help you distinguish if a number is positive, only if it's an integer. For example floor(-1) = ceil(-1) = 1.
Additionally, you're over using "elseif" (for example,
elseif isequal(size(A), [2 2])==1 & floor(n)=ceil(n)
will work exactly like an "else" statement (assuming you fix the = by making it an ==) because those are the two conditions you've already filtered out, so the rest of the code will never be evaluated.
And you really don't want to be bringing the SVD into this at all...
Ok so i tried switching to using the formula for a det(A) and I got rid of the extra elseif statments since those would be filtered out already. I still am unsure how to incorporate displaying the strings. The next part of this asks me to call the function and evaluate a provided matrix and n value.
function [] = invertiblePowers(A,n)
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
if isequal(size(A), [2 2])==1 & n>0
(A(1)*A(4)-A(2)*A(3))^n
elseif (A(1)*A(4)-A(2)*A(3))^n==0
disp('det(A^k) = 0 so A^k is singular')
elseif (A(1)*A(4)-A(2)*A(3))^n~=0
disp('det(A^k) = A_k so A^k is invertible')
end
I wouldn't assume you're allowed to use the rule that det(A^n) = det(A)^n. I'd assume you're supposed to calculate A^n then find the determinate of that new matrix.

Sign in to comment.

To calculate the determinant of a 2x2 matrix, see this link a little over halfway down the first page:
To get the determinant of a matrix power, det(A^n), also note from the above link that the determinant of a matrix product is the product of the individual determinants. I.e. det(A*A) = det(A)*det(A). So you can extend this to powers and figure out the formula for det(A^n).
Using the above hints should help you to write the code.

2 Comments

I'm guessing that discovering that relationship between det(A^n) and det(A) is the point of this problem =P
I see the relationship that det(A^n) can be determined by finding the det(A) then raising it to the nth power but I am confused about incorporating the for loop. In the comment section of the other response you can see I'm only using if and elseif statements

Sign in to comment.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Asked:

on 28 Sep 2015

Commented:

on 29 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!