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 commentaire

Stephen23
Stephen23 le 9 Juin 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.

Connectez-vous pour commenter.

Réponses (2)

Christine Tobler
Christine Tobler le 9 Juin 2017
Modifié(e) : Christine Tobler le 9 Juin 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 commentaire

Juan Méndez
Juan Méndez le 10 Juin 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.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 10 Juin 2017

0 votes

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

Catégories

En savoir plus sur Linear Algebra dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by