Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to plot the eigenvalues of a matrix which is dependant on a parameter (well, actually I want to plot a tight-binding electronic band structure). The dimension of the hamitonian matrix is greater than 5, so of course there is no analytical solution. But anyway, when I use Eigenvalues directly, Mathematica will give results containing Root. So I just use these Roots to plot. I thought that theoretically the plot will be fine. Because it is just a root searching process, so if Mathematica searches the roots little by little the eigenvalue curve will be fine. But it turns out that things are not that simple. I give all the code that I have written as follows:

n = 5;
h = Table[0, {i, 1, 2 n}, {j, 1, 2 n}];
a[m_] := 2 m - 1
b[m_] := 2 m
 h[[b[i], a[i]]] = E^((I k)/2) + E^((-I k)/2);
 If[i + 1 <= n, h[[b[i + 1], a[i]]] = 1, Null];
 If[i - 1 > 0, h[[a[i - 1], b[i]]] = 1, Null];
 h[[a[i], b[i]]] = E^((I k)/2) + E^((-I k)/2)
 , {i, 1, n}]

the above code is to generate the hamiltonian matrix with dimension 2n

eigen = Eigenvalues[h];

Plot[x /. x -> eigen[[1]], {k, 0, \[Pi]}]

the plot is as follows:


It is quite good at the beginning, but totally messed up in the end.So the first question is: What was wrong with the ugly tail?

Finally, about plotting eigenvalues, I only have one more method, that is to calculate the eigenvalues at the discrete point within the range of the parameter and then ListPlot them. But this method has a disadvantage, Because all the eigenvalues are in random order. So which point belongs to which curve can only be judged by eyes, and using this method, I can not plot different eigenvalue curve with different color especially when two curves intersect. So is there better solutions?

share|improve this question
What version are you using? With version 9, I get a smooth curve. Also, you can simply use Plot[eigen[[1]], {k, 0, \[Pi]}]. – Yves Klett Jan 3 '13 at 8:20
@YvesKlett I use mathematica 8. Have you tried larger value of n ? – matheorem Jan 3 '13 at 8:28
Works for all 10 values. So version 9 probably handles imaginary parts different... want to try a Re on your values? Seems like you get small imaginary residuals which are chopped more generously in verison 9. – Yves Klett Jan 3 '13 at 8:34
up vote 7 down vote accepted

Looks like you get small imaginary residuals that are not chopped in V8 (but are in V9).

Plot[eigen[[1]] // Im, {k, 0, \[Pi]}]

Adding a Re (or Chop or similar) gets rid of those for good:

Plot[eigen[[1]] // Re, {k, 0, \[Pi]}]

share|improve this answer
Right answer! Thank you very much! It seems that I need to update to mathematica 9 as soon as possible. – matheorem Jan 3 '13 at 9:57
@user15964 updating is nice, but putting in a Re or Chop is probably good practice anyway because residuals are going to pop up when using numerical methods sooner or later. – Yves Klett Jan 3 '13 at 10:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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