This question has multiple parts to it. The setup is that I have a matrix that is a function of two parameters a and b. I wish to plot the eigenvalues of this matrix along a general path in the a-b plane and I want these two branches to have the correct coloring. For example, for the simple path for {a,b} from {0,0} to {1,0} the following works:
testMat[a_, b_] := {{2 a, 3 b^2}, {2 b, 4 a}};
Plot[Evaluate@Eigenvalues[testMat[t, 0]], {t, 0, 1}]
At this point please note that Evaluate
must be included in the second line for the two branches to have different colors. My first question is thus
- Why is Evaluate necessary to get the correct colors for the eigenvalue plots?
Now suppose that I wish to plot the eigenvalues on a path that goes from {0,0} to {0,1} to {1,1}. I implemented this in the following way
testfunc[t_] = Evaluate@Piecewise[{{testMat[t, 0], 0 <= t <= 1}, {testMat[1, t - 1],
1 < t < 1.5}}, {0, 0}];
Plot[Eigenvalues@testfunc[t], {t, 0, 1.5}]
However as you can see the two branches have the same color. Somehow Mathematica does not understand that they are two separate plots. Thus my final two questions are:
How do I get the two branches colored separately?
Is there a better way of plotting along a path that Mathematica will find more agreeable?
Thanks in advance!
EDIT: All answers are great but there is a significant problem using First/Last or dot products since the Eigenvalues are listed from largest to smallest while when plotting we are interested in smooth functions (i.e the largest eigenvalue will always be blue and the smaller orange). For example:
testMat2[a_, b_] := {{2 a, 3 b^2}, {2 b, 4 a^2}};
Plot[{First@Evaluate@Eigenvalues[testMat2[t, 0]],
Last@Evaluate@Eigenvalues[testMat2[t, 0]]}, {t, 0, 1.6}]
What do I do to fix this?
Edit 2: Inspired by the excellent answers below, the easiest method for me was to create a Table
of eigenvalues and then use Interpolation
to create a vector values function. Now Plot[{interpFunc[t][[1]], interpFunc[t][[2]],...}, {t,0,1}]
works beautifully!