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:

enter image description here

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},
   H = Dynamic[Evaluate[mat]];
   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.

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Commented May 16, 2016 at 15:56

1 Answer 1


One possibility is to define your matrix as a function and to use Manipulate for the dynamics

mat[a_, b_, q_] := {{b, a*-q^2}, {2, a/2}};
Manipulate[Plot[Eigenvalues[mat[a, b, q]], {q, 0, 0.2}], {a, 0, 2}, {b, 0, 2}]

enter image description here


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.