Null is misbehaving if used within loops.

Background: I'm trying to make a program that can calculate eigenvectors given a nxn matrix. I already know that there's a matlab function to calculate eigenvalues but I was requested to do calculations 'manually'. As far as I can see, there's nothing wrong with the code, but I'm struggling with the for loop. Here's the code:
syms x;
A=[2 1 -1; 1 2 -1; -1 1 2]; %example matrix
B=diag(repelem(x,size(A,1)));
disp('Characteristic polynomial: ')
wat=det(A-B)
Eigenvalues=roots(sym2poly(wat))
for n=1:size(Eigenvalues,1);
disp('Current eigenvalue: ')
Eigenvalues(n)
EigM=diag(repelem(Eigenvalues(n),size(A,1)));
Z=A-EigM;
null(Z,'r')
end
What happens? As you might know, we need to apply null(Z,'r') to the matrix on which we've substracted the eigenvalues in order to get the eigenvectors for the same eigenvalue. That's the problem, when it does that Null returns 'empty matrix nx0', even when there's indeed an answer. It only runs correcly usually in the last iterations, but I still don't know why. How could I make it work correctly? It seems that if I do Z=round(Z, 5) it'd work 'ok' but only for non-complex eigenvalues. I'm using Matlab 2017.

댓글 수: 1

Stephen23
Stephen23 2017년 6월 9일
@Juan Méndez: today I formatted your code correctly for you. In future you can do it yourself: first select the code text, then click the {} Code button above the textbox.

댓글을 달려면 로그인하십시오.

답변 (2개)

Christine Tobler
Christine Tobler 2017년 6월 9일
편집: Christine Tobler 2017년 6월 9일

1 개 추천

Try using
null(Z)
instead of
null(Z, 'r')
With the 'r' option, no tolerance for round-off errors in the matrix Z is used. So, because Z is not low-rank exactly (since the subtraction causes some numerical error), null(Z, 'r') determines that Z has full rank, and so has not null-space.
Using round(Z) will work only if the exact values in Z are all integers, because in this case, rounding gives you a matrix that is exactly low-rank.

댓글 수: 1

Juan Méndez
Juan Méndez 2017년 6월 10일
Thanks for answering. I didn't know that null(Z,'r') would have no tolerance for round-offs, but surprisingly that's why rounding the matrix before applying null works (with real numbers). null(Z) could be an option, but Z is probably always going to be made up of values with a lot of decimals. Also, it gives an orthonormal basis, and unfortunately I can't work with that.
null(Z,'r') is perfect (it even works with some complex values), but the error keeps me from getting the correct eigenvectors. I'm not even using format rat, so I don't know if there's a way to get (imaginary) eigenvectors correctly. Cheers.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2017년 6월 10일

0 개 추천

Instead of using Eigenvalues=roots(sym2poly(wat)) you could use Eigenvalues = solve(wat) together with null(Z)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2017년 6월 9일

답변:

2017년 6월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by