2

I am quite new to Mathematica and I guess this question is really basic, but I can't figure it out myself. I'm making a simple notebook that plots a Hamiltonian function, a plane of constant energy, and the phase space of the system. I highlight the intersection of the plane and the Hamiltonian, and I would like that the highlighted curve would show up as a red orbit in the phase plane. This is my code:

H[q_, p_, a_] := a q p + p^3 + q^2;

Qdot[q_, p_, a_] := Derivative[0, 1, 0][H][q, p, a];
Pdot[q_, p_, a_] := -Derivative[1, 0, 0][H][q, p, a];

Const[E_] := E;

Pbar[q_, a_, E_] := Evaluate[NSolve[H[q, p, a] == E, p, Reals]];

Manipulate[
    {
        ContourPlot3D[{H[q, p, a] == z, Const[E] == z}, {q, -qext, 
    qext}, {p, -pext, pext}, {z, -zext, zext}, 
   MeshFunctions -> {0 &, 0 &, Const[E] - H[#1, #2, a] &}, 
   MeshStyle -> {Blue, Thick}, Mesh -> {{0}}, 
   ContourStyle -> 
    Directive[Gray, Opacity[0.5], Specularity[White, 60]]],
        StreamPlot[{Qdot[q, p, a], Pdot[q, p, a]}, {q, -qext, 
    qext}, {p, -pext, pext}, 
   StreamPoints -> {{200, {{qbar, Pbar[qbar, a, E]}, {Red, Thick}}}, 
     0.1, 10}]
    },
    {E, -5, 5},
    {a, -10, 10},
    {qbar, -1, 1},
    {qext, 1, 5},
    {pext, 1, 5},
    {zext, 0.01, 5}
 ]

So the problem is with Pbar: it is a function, so it returns p -> _number_, not _number_. I need it to return the number, because that way StreamPlot will highlight the streamline starting from {qbar, Pbar[qbar, a,E]}

Karsten7
  • 27,448
  • 5
  • 73
  • 134
Leonardo
  • 23
  • 2

1 Answers1

3

with few new edits:

H[q_, p_, a_] := a q p + p^3 + q^2; 
Qdot[q_, p_, a_] := Derivative[0, 1, 0][H][q, p, a]; 
Pdot[q_, p_, a_] := -Derivative[1, 0, 0][H][q, p, a];
const[e_] := e; 
Pbar[q_, a_, e_] := p /. First@NSolve[H[q, p, a] == e, p, Reals];

Manipulate[
{ContourPlot3D[{H[q, p, a] == z, const[e] == z}, {q, -qext, qext}, {p, -pext, pext}, {z, -zext, zext}, 
MeshFunctions -> {0 &, 0 &, const[e] - H[#1, #2, a] &}, 
MeshStyle -> {Blue, Thick}, Mesh -> {{0}}, 
ContourStyle -> Directive[Gray, Opacity[0.5], Specularity[White, 60]]], 

 StreamPlot[{Qdot[q, p, a], Pdot[q, p, a]}, {q, -qext,qext}, {p, -pext, pext}, 
 StreamPoints -> {{200, {{qbar, Pbar[qbar, a, e]}, {Red, Thick}}},0.1, 10}]},
 {e, -5, 5}, {a, -10, 10}, {qbar, -1, 1}, {qext, 1, 5}, {pext, 1, 5}, {zext, 0.01, 5}]

enter image description here

  • This works outside of manipulate, e.g. if I write Pbar[2,4,1] it outputs {-0.368733} but it doesn't work in the Manipulate! Why is that so? – Leonardo Dec 23 '15 at 09:17
  • The code works fine for me, Windows 10x64, Mathematica 10.3.1 –  Dec 23 '15 at 09:29
  • Does it highlight in red the streamline that corresponds to the intersection of the plane and the Hamiltonian? Because it never does, for me. My guess is that since there is not one unique solution to the intersection, it cannot work – Leonardo Dec 23 '15 at 09:35
  • Nothing red in Mathematica 10 on Win10 – Baran Cimen Dec 23 '15 at 10:43
  • As has already been said, the code runs fine (not the red streamline). First I would check the function of StreamPlot without Manipulate. –  Dec 23 '15 at 10:43
  • Ok now my problem is to figure out how to get StreamPlot to accept Pbar. Thank you for your help – Leonardo Dec 23 '15 at 11:12
  • @Leonardo it is Christmastime! Your code runs fine! –  Dec 23 '15 at 14:41