I am calculating, numerically, the zeros of a determinant. a1 and a2 are constants, q is a parameter. I would like to vary q and find the roots w as a function of q and plot roots w vs. q. All roots w associated with a q should be plotted "above/below" that q in the plot. For this determinant, I have eight roots w corresponding to each value of q. Thus, my final plot should consist of 8 dots placed above and/or below the corresponding q-value. The q-value should be on the horizontal axis.

a1 = 2; a2 = 3;
det[q_, w_] := (1 - q/w^2 (1 + Exp[-2 a1 q ] 1/(2 w^2 - 1))) (1 - 
      q/w^2 (1 + Exp[-2 q] 1/(2 w^2 - 1))) - 
   q^2/w^4 (Exp[-q Abs[a1 - a2]] + 
      Exp[-q (a1 + a2)] 1/(2 w^2 - 1)) (Exp[-q Abs[a2 - a1]] + 
      Exp[-q (a2 + a1)] 1/(2 w^2 - 1));
(*numerical solve*)
sol = NSolve[det[q, w] == 0, w];
(*build table for {q,{w1,w2,w3,w4,w5,w6,w7,w8}} pairs*)
MF = Table[{q, sol[[All, 1, 2]]}, {q, 0, 2, 0.5}] // MatrixForm;
(*this should be plotted on the horizontal axis*)
xvar = Table[MF[[1, ii]][[1]], {ii, 1, 5}];
(*this should be plotted on the vertical axis*)
yvar = Table[MF[[1, ii]][[2]], {ii, 1, 5}];
(*my listplot*)
Show[ListPlot[{xvar, #}] & /@ yvar]

the problem with the plot (below) is that xvar-s are also plotted against the vertical axis (blue dots) yvar-s (orange dots) are plotted at x=1, x=2 etc. instead of the corresponding q-values, which, in this case, are 0, 0.5, 1, 1.5 etc.

my listplot

(I have two questions: 1. How can I get the plot I want? 2. Is there a better way of doing this?)


1 Answer 1


ListPlot requires lists of point coordinates, but the code was providing it with lists of lists. This can be fixed by replacing the last two lines by

yvar = Transpose[Table[MF[[1, ii]][[2]], {ii, 1, 5}]];
ListPlot[Table[{xvar[[i]], yvar[[j, i]]}, {i, 5}, {j, 8}], PlotStyle -> Black]

enter image description here

Is this what you had in mind?

  • $\begingroup$ Thanks bbgodfrey, this is exactly what I needed! $\endgroup$ Feb 7, 2015 at 17:36

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.