How to plot eigenvalues as a function of parameters in a dynamic module? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-22T04:58:56Z https://mathematica.stackexchange.com/feeds/question/115257 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/115257 0 How to plot eigenvalues as a function of parameters in a dynamic module? Erik Bernitt https://mathematica.stackexchange.com/users/40275 2016-05-16T15:06:10Z 2016-11-02T22:58:40Z <p>I am trying to make a fancy interactive plot that shows how the eigenvalues of a matrix <code>mat</code> change under variation of parameters <code>a</code> and <code>b</code>. The eigenvalues themselves are function of a variable <code>q</code>. I can easily do something like this:</p> <pre><code>mat = {{b, a*-q^2}, {2, a/2}}; H = mat /. {a -&gt; 1, b -&gt; 0.2} Plot[Eigenvalues[H], {q, 0, 0.2}] </code></pre> <p>This gives me the desired yet static plot:</p> <p><a href="https://i.stack.imgur.com/Y4mpv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y4mpv.png" alt="enter image description here"></a></p> <p>How can I do that interactively, lets say with some sliders? Here is what I tried:</p> <pre><code>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]} }] ] </code></pre> <p>The code above works half way, i.e., it makes <code>mat</code> dynamic with regard to the choice of <code>a</code> and <code>b</code> via the sliders. However, the part where I try to calculate the eigenvalues does not work and correspondingly also the plotting fails. </p> <p>What am I doing wrong here? Any help is greatly appreciated! </p> <ul> <li>Thanks, Erik</li> </ul> <p>By the way: I know I could calculate the eigenvalues with symbolic <code>a</code> and <code>b</code> 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 <a href="https://mathematica.stackexchange.com/questions/39747/how-to-plot-several-functions-without-jumping-multiple-eigenvalues-of-a-system">How to plot several functions without jumping? (multiple eigenvalues of a system as functions of 2 parameters)</a> along these lines. </p> https://mathematica.stackexchange.com/questions/115257/-/130275#130275 2 Answer by bill s for How to plot eigenvalues as a function of parameters in a dynamic module? bill s https://mathematica.stackexchange.com/users/1783 2016-11-02T22:58:40Z 2016-11-02T22:58:40Z <p>One possibility is to define your matrix as a function and to use Manipulate for the dynamics</p> <pre><code>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}] </code></pre> <p><a href="https://i.stack.imgur.com/z7ygj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z7ygj.png" alt="enter image description here"></a></p>