I am trying to make a fancy interactive plot that shows how the eigenvalues of a matrix mat
change under variation of parameters a
and b
. The eigenvalues themselves are function of a variable q
. I can easily do something like this:
mat = {{b, a*-q^2}, {2, a/2}};
H = mat /. {a -> 1, b -> 0.2}
Plot[Eigenvalues[H], {q, 0, 0.2}]
This gives me the desired yet static plot:
How can I do that interactively, lets say with some sliders? Here is what I tried:
mat = {{b, a*-q^2}, {2, a/2}};
DynamicModule[{a = 1, b = 0.5, H},
Column[{
H = Dynamic[Evaluate[mat]];
H,
Dynamic[Eigenvalues[H]],
Dynamic[Plot[Eigenvalues[H], {q, 0, 3}]],
{"a", Slider[Dynamic@a, {0, 1, 0.01}], Dynamic[a]}, {"b",
Slider[Dynamic@b, {0, 1, 0.01}], Dynamic[b]}
}]
]
The code above works half way, i.e., it makes mat
dynamic with regard to the choice of a
and b
via the sliders. However, the part where I try to calculate the eigenvalues does not work and correspondingly also the plotting fails.
What am I doing wrong here? Any help is greatly appreciated!
- Thanks, Erik
By the way: I know I could calculate the eigenvalues with symbolic a
and b
outside the dynamic module and then simply plot the resulting polynomials. The actual problem involves a larger matrix, where this is unfortunately no option. See also How to plot several functions without jumping? (multiple eigenvalues of a system as functions of 2 parameters) along these lines.