Null is misbehaving if used within loops.

6 views (last 30 days)
Juan Méndez
Juan Méndez on 9 Jun 2017
Answered: Walter Roberson on 10 Jun 2017
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
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.

Sign in to comment.

Answers (2)

Christine Tobler
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.
  1 Comment
Juan Méndez
Juan Méndez on 10 Jun 2017
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.

Sign in to comment.


Walter Roberson
Walter Roberson on 10 Jun 2017
Instead of using Eigenvalues=roots(sym2poly(wat)) you could use Eigenvalues = solve(wat) together with null(Z)

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!