# Help on order of evaluation within plot function

it is my first time asking a question here so please let me know if there's anything I should be doing that I'm unaware of. I'm sure the answer to my question already exists on the site but I'm having a hard time understanding what are probably the relevant posts. So here's my question.

In my code I am creating a slightly complicated 4x4 matrix which depends symbolically on multiple variables. I then calculate the eigensystem. Next, for ease of readability and manipulation, I assign the real and imaginary part of each of the 4 eigenvalues into different variables, and then I arrange those variables into lists.

Next I make plots of those lists within manipulate functions so that I can tune the multiple variables on which the initial matrix depends and look at the behavior of the eigenvalues.

The problem which is arising is that the in my plot the lines corresponding to one value will jump back and forth between one line and another line rapidly. I'm pretty sure this is due to the fact that the order of the list for the eigenvalues of eigensystem isn't coming out in the same order every time since a lot of the code is evaluated symbolically.

I am open to suggestions about how to accomplish this. The idea I've come up with is the following: The symbolic expressions resulting from the eigensystem are very complicated and not illuminated and I don't look at them, I am only interested in the plot. To that end, I thought it might make sense to somehow hold the evaluation of much of the initial code until the plot. In other words:

-rather than calculating complicated expressions symbolically and then plugging values for the variables with the plot/manipulate construct the idea would be to:

-within the plot/manipulate construct the code could pick a value for the variables, plug those into the matrix, then calculate the eigensystem, then put the results into a list, and THEN make a plot point for a single set of variables, then move on to the next plot point.

Does this make sense? I realize that one there might be a question of computational efficiency but I'm not sure if the method I'm proposing would be too compuational prohibitive or not so I'd like to give it a try.

I'm pretty sure the way to do this has something to do with the Hold function or evaluation control or something but I haven't been able to put a solution together myself reading the documentation and the existing responses on this website unfortunately. Hopefully the answer is pretty simple!

Here is the code I am looking at.

Clear[w,x,y,z]
Mat[w_,x_,y_,z_]={
{0,1,0,0},
{-(1+w)^2,-x,+(y^2/Sqrt[1-z^2])*Sqrt[(1+w)/(1-w)],0},
{0,0,0,1},
{(y^2/Sqrt[1-z^2])*Sqrt[(1-w)/(1+w)],0,-(1-w)^2,-x}
};

{vals[w_,x_,y_,z_],vecs[w_,x_,y_,z_]}=Eigensystem[Mat[w,x,y,z]];

val1re[w_,x_,y_,z_]=Re[vals[w,x,y,z][]];
val1im[w_,x_,y_,z_]=Abs[Im[vals[w,x,y,z][]]];
val2re[w_,x_,y_,z_]=Re[vals[w,x,y,z][]];
val2im[w_,x_,y_,z_]=Abs[Im[vals[w,x,y,z][]]];
val3re[w_,x_,y_,z_]=Re[vals[w,x,y,z][]];
val3im[w_,x_,y_,z_]=Abs[Im[vals[w,x,y,z][]]];
val4re[w_,x_,y_,z_]=Re[vals[w,x,y,z][]];
val4im[w_,x_,y_,z_]=Abs[Im[vals[w,x,y,z][]]];

pListre[w_,x_,y_,z_]={val1re[w,x,y,z],val2re[w,x,y,z],val3re[w,x,y,z],val4re[w,x,y,z]};
pListim[w_,x_,y_,z_]={val1im[w,x,y,z],val2im[w,x,y,z],val3im[w,x,y,z],val4im[w,x,y,z]};

Manipulate[
Row[{
Plot[{Evaluate[pListim[w,x,y,z]]},{y,0,5},
PlotStyle->{Green,Blue,Purple,Yellow},PlotRange->{-3,3},ImageSize->300],
Plot[{Evaluate[pListre[w,x,y,z]]},{y,0,5},PlotStyle->{Green,Blue,Purple,Yellow},
PlotRange->{-3,3},ImageSize->300]
}],
{{z,0},-.9,.9},{{w,0},-.9,.9},{x,0,5}]


Here is a picture of the output of one of the graphs so you can see what I'm talking about about the eigenvalue jumping around. I've added different offsets to each eigenvalue so that you can see what is going on with each plot. Thank you very much for any help with this problem!!

edit: Code updated to -Include variable w -Use the real matrix I am using -pListre and pListim are now defined as pListre[w_,x_,y_,z_] so the code works

The problem I am looking at can be reproduced by using the manipulate to set x to a non-zero value.

• Your code does not produce the plot you showed. In fact, it does not produce any plot at all. You have an extra comma in the definition of Mat, but even removing that no plots are produced. Could you check & fix? – MarcoB Apr 17 '16 at 19:44
• Hello MarcoB. You are correct. The code didn't work properly because I forgot to add in the explicit variable dependencies on pListre and pListim. I have corrected that in the original question. I've also updated the matrix so it is now showing what I am actually trying to compute so that you can reproduce a very similar plot to what I have shown. The same issue will be exhibited. – Jagerber48 Apr 17 '16 at 20:27

{vals[w_, x_, y_, z_], vecs[w_, x_, y_, z_]} = Eigensystem[Mat[w, x, y, z], Cubics -> True, Quartics -> True];

• I suspect the code runs faster because Plot tries to compile the expression before evaluating it, but Root is not a compilable function. By expanding everything in terms of radicals the expression becomes compilable. – QuantumDot May 18 '16 at 13:32