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
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
2017년 6월 9일
편집: Christine Tobler
2017년 6월 9일
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.
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에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!