Null is misbehaving if used within loops.
6 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 9 Jun 2017
@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.
Answers (2)
Christine Tobler
on 9 Jun 2017
Edited: Christine Tobler
on 9 Jun 2017
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
on 10 Jun 2017
Instead of using Eigenvalues=roots(sym2poly(wat)) you could use Eigenvalues = solve(wat) together with null(Z)
0 Comments
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!