Linear algebra Matlab help please...
A square matrix A is called nilpotent if there exists a positive integer q such that \[A^q=0\] Using Matlab, if I have A, how would I find out if a matrix is nilpotent and how would I find out for which q's, if so?
if you don't accept to write a while loop which might be an infinite loop, consider a for loop, \[\text{for i=1:N }\\ \dots\\ \text{end}\] don't compute each time the k-th power, but use the previous computation: \(A^k = A\times A^{k-1} \) the test for a matrix full of zeros, in matlab can't be just like testing if it contains only zeros. rather , test if the absolute value of the entries are smaller than epsilon.
the \(q\) such that \(A^q=0\) is teh value of \(i\), the iteration variable in the for loop.
I'm struggling a little to understand what you mean.. How do i test if the absolute value of the entries are smaller than epsilon; what will be the epsilon?
it depends on the values in \(A\). if they are small (in absolute value) integers, you can put epsilon to something like \(10^{-6}\). it should work fine. absolute value of a matrix \(A\): abs(\(A\)). maximum of a matrix: max(\(A\)). the condition you might use: max(abs(\(A\)))<\(10^{-6}\). (or whatever value you prefer).
Okay, I'll explain what I have to do: I have some matrices, A to G, all different sizes and values.. Now I want to write a matlab function where i give the matrix name (i.e. A/B/C/D/E/F/G) as input and it runs a few calculations. The function determines if the matrices are symmetrical, skew symmetrical, nilpotent and idempotent. Besides the fact that I can't get the function to work, I also don't know how to program it to determine if a matrix is nilpotent...
A = [0 -1 2 3 0; 1 0 1 -1 2; -2 -1 0 0 1; -3 1 0 0 -2; 0 -2 -1 2 0]; B = [0 1 1 1 1 1 1;0 0 1 1 1 1 1;0 0 0 1 1 1 1;0 0 0 0 1 1 1;0 0 0 0 0 1 1;0 0 0 0 0 0 1;0 0 0 0 0 0 0]; C = [0 1 2;3 0 1;2 3 0;1 2 3;0 1 2;3 0 1]; D = [2 3 -4;0 1 0;0.5 -1.5 -1]; E = [1 1 1;1 1 1;1 1 1]; F = [5/6 1/6 1/3;1/6 5/6 -1/3;1/3 -1/3 1/3]; G = C * inv([transpose(C)*C]) * transpose (C);
% nilpotent: W = A for i=2:LIMIT W = A * W % next power if "condition max(abs(W))<eps"... ->its nilpotent else do nothing end
or even W = eye("size of A") % so you even test if \(A^1\) is already the null matrix + "for i=1:LIMIT" if the condition is met-> A is nilpotent and you have to return (or save) the number i of that iteration.
Okay, thanks! I'll give it a try! Now I only need help with the function... I wrote it like in the attachment. How do I make the function so that I can input a matrix name (e.g. A or E) and the the function returns the outputs? PS the matrices will be declared before calling the function
I would use SVD decomposition
http://www.mathworks.nl/help/matlab/ref/function.html something like that: ——————————— % in some file nilpotent.m: function q = nilpotent(A) W = eye(...) % the size of A LIMIT = 20; for i=1:LIMIT W = A*W; if "nilpotent condition" q = i; break; end end % you q has not been defined in the if condition, just say it's not nilpotent, put q=0 or q=1 (your own convention) q = -1;
@phi good idea, that's faster to type. but less intersting when learning to code in matlab. but this method "should" be always correct.
"this method=your method"
PS I'm still very new to linear algebra and matlab... This is my first semester taking Lin Alg. example of what i want to do: DECLARE THE MATRICES M=[? ? ?;? ? ?; ? ? ?] CALL THE FUNCTION WITH THE MATRIX NAME AS INPUT MatrixCalc(M) THE FUNCTION RETURNS SOME OUTPUTS, LIKE M is symmetrical M is not skew symmetrical M is idempodent M is nilpodent
better divide the task into smaller bits, write 4 functions in one (-) or different file(s) (+), and write a simple like this one: function MatrixCalc(M) if symmetrical(M) % your function % output else % other output end if .... ...
I still don't really understand.. I'm so sorry.. Could you perhaps take a look at my file and help me correct it?
it looks ok (i didnt test). this would be the body of the matrixcalc function. just make it a real function: ——————— function MatrixCalc(A) x = input("what matrix....") if transpose(x) == x ...
You will have one file with the MatrixCalc function, called MatrixCalc.m Another one (or several ones) for any other test you want to make. the test for the transpose is so short that you don't have to write another function. But you have to write another function for the nilpotent condition.
Ahh okay, thanks! I'm trying that now... brb
I get this error: Undefined function 'Matlab' for input arguments of type 'char'.
i don't know matlab very well, im not used to this.. Never put quote in the arguments..
oh, maybe the result of input("....") is "char" type.
Yes! I got it working!! I just made a mistake in the function... Now I only need to do one more thing.. If an error occurs (such as n not = m), the whole function will stop.. Is there a way I can say that an error occured, but then just skip that part and go on with the rest of the function?
Thx!! Brb
I'm done! Don't you think this code will work for the nilpotent part? %nilpotent for i=1:1000000 if x.^i == 0 fprintf('Matriks %s is nulpotent. q = %d\n',inputname(1),i) break else if i == 1000000 fprintf('Matriks is nie nulpotent nie.\n',inputname(1)) end end end
1000000 is very big.. (it's ok if you are ready to wait this much lol) and, don't write x.^i. use a matrix - variable. As i did, W = A * W. this will compute the next power at each iteration of the loop.
my W*A is a normal matrix multiplication, i forgot the matlab syntax. :)
Hahahah! I actually got one answer where q = 37628. I'll change the limit to 100000, that would be a lot faster. W*A would be the correct syntax. But I don't understand how that would be better than what I typed? I'm sorry haha
Is the only difference that it is more effective programming since it saves computation time? (the previous W is already computed, instead of having to go through the entire loop all over again each time)
yes, computational time. And the condition "matrix" == 0 is dangerous.. because of the little approximation errors made by the computer. you should prefer the method with the epsilon over that one.
Okay, thanks a lot for the help! I really appreciate it so much!
I can attach the completed program if you like?
Here is one way to do this % I have some matrices, A to G, all different sizes and values.. % % Now I want to write a matlab function where i give the matrix name (i.e. A/B/C/D/E/F/G) as input and it runs a few calculations. % The function determines if the matrices are symmetrical, skew symmetrical, nilpotent and idempotent. % % THE FUNCTION RETURNS SOME OUTPUTS, LIKE % M is symmetrical % M is not skew symmetrical % M is idempodent % M is nilpodent A = [0 -1 2 3 0; 1 0 1 -1 2; -2 -1 0 0 1; -3 1 0 0 -2; 0 -2 -1 2 0]; B = [0 1 1 1 1 1 1;0 0 1 1 1 1 1;0 0 0 1 1 1 1;0 0 0 0 1 1 1;0 0 0 0 0 1 1;0 0 0 0 0 0 1;0 0 0 0 0 0 0]; C = [0 1 2;3 0 1;2 3 0;1 2 3;0 1 2;3 0 1]; D = [2 3 -4;0 1 0;0.5 -1.5 -1]; E = [1 1 1;1 1 1;1 1 1]; F = [5/6 1/6 1/3;1/6 5/6 -1/3;1/3 -1/3 1/3]; G = C * inv([transpose(C)*C]) * transpose (C); name= 'ABCDEFG'; MM= {A,B,C,D,E,F,G}; % cell array of matrices for ii= 1:(length(MM)) M= cell2mat(MM(ii)); attributes= analyze_matrix(M); str= sprintf('Matrix %c: %s', name(ii),attributes); disp(str); end;
function attributes= analyze_matrix(M) attributes= []; sz= size(M); try if (sz(1)== sz(2)) attributes= [ 'square ' attributes]; if (M== M') attributes= [ 'symmetric ' attributes]; end; if (M == -M') attributes= [ 'skew symmetric ' attributes]; end; tmp= M*M-M; if (all(abs(tmp(:))<1e-13)) attributes= [ 'idempotent ' attributes]; end; if (all(eig(M))== 0) N= eye(size(M)); ct=0; while ( (any(N(:))) && ct <= length(M)) N= N*M; ct= ct+1; end; str= sprintf('nilpotent degree %d ',ct); attributes= [ str attributes]; end; else attributes= [ 'not square ' attributes]; end; catch end;
Join our real-time social learning platform and learn together with your friends!