4
$\begingroup$

I am using Plot to look at the eigenvalues of a matrix, and find that in some circumstances I get artifacts. For example

H = {{x, -g}, {g, -x}};
Eigenvalues[H]
Plot[Eigenvalues[H] /. g -> 1, {x, -4, 4}]

gives me

{-Sqrt[-g^2 + x^2], Sqrt[-g^2 + x^2]}

good plot

but

H = {{x, -1}, {1, -x}};
Eigenvalues[H]
Plot[Eigenvalues[H], {x, -4, 4}]

gives me

{-Sqrt[-1 + x^2], Sqrt[-1 + x^2]}

bad plot

Fianlly, just to add to the confusion,

Plot[{-Sqrt[-1 + x^2], Sqrt[-1 + x^2]}, {x, -4, 4}]

gives

good plot 2

What's going on here? I thought there would be no difference between these.

$\endgroup$

1 Answer 1

7
$\begingroup$

When you are using Eigenvalues[H] inside Plot it is evaluated each time for an x, and depending on the matrix the order of the eigenvalues may change as well. Evaluate it before and then plot

H = {{x, -1}, {1, -x}};
ev = Eigenvalues[H]
Plot[ev, {x, -4, 4}]

$\left\{-\sqrt{x^2-1},\sqrt{x^2-1}\right\}$

enter image description here

Or you can try to Sort it

Plot[Sort[Eigenvalues[H]], {x, -4, 4}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.