# How can I Plot Phase Portraits Using ParametricNDSolve function(already know about StreamPlot function)?

I have a 2nd order ODE: $$\ddot{\phi}+3\dot{\phi}\sqrt{(\frac{8\pi G}{3})(\frac{1}{2}[\dot{\phi}^{2}+m^{2}\phi^{2}])}+\phi m^{2}=0.$$ I can use the StreamPlot command by breaking the ODE into a set of 2 first-order DEs. But for my learning, I want to do it by using the parametricNDSolve command. Here is what I tried, but the problem for me is $$t$$.

  eq=\[Phi]''[t]+3*\[Phi]'[t]*Sqrt[(8*Pi*G/3)*(0.5*\[Phi]'[t]^2+0.5*m^2*\[Phi][t]^2)]+\[Phi]*m^2==0
sol=ParametricNDSolve[{eq/.{G->1,m->0.5},\[Phi]'[0]=a,\[Phi]=b},{\[Phi],\[Phi]'},{t,0,1},{a,b}]
ParametricPlot[Flatten[Table[\[Phi][a,b],\[Phi]'[a,b],{a,-1,1},{b,-1,1}],1]/.sol//Evaluate,{t,0,1}]

• NDSolve is a purely numeric routine. Therefore, if you use NDSolve you need to give all parameters (such as G and m) some numeric values. Otherwise, it does not work. Commented Oct 5, 2021 at 21:26
• see the second line of the code. I have provided the values of G and m.
– Jpmg
Commented Oct 5, 2021 at 21:33
• I do not understand what do you try to obtain with the ParametricPlot. Maybe you should explain it better. Anyway, the code contains several errors. I corrected some and simplified the code: sol = NDSolve[{\[Phi]''[t] + 3*\[Phi]'[t]* Sqrt[(8*Pi*1/3)*(0.5*\[Phi]'[t]^2 + 0.5*0.5^2*\[Phi][t]^2)] + \[Phi][t]*0.5^2 == 0, \[Phi][0] == 1, \[Phi]'[0] == 0}, \[Phi], {t, 0, 1}] Plot[\[Phi][t] /. sol, {t, 0, 1}] Like this it works, and can be a starting point for the folloing. Commented Oct 5, 2021 at 21:54
• I want to plot $\dot{\phi}$ against $\phi$. I wanted to do it by using several initial conditions by using the parametricNDSolve command. Thanks for the correction.
– Jpmg
Commented Oct 5, 2021 at 21:56
• sol = NDSolve[{\[Phi]''[t] + 3*\[Phi]'[t]* Sqrt[(8*Pi*1/3)*(0.5*\[Phi]'[t]^2 + 0.5*0.5^2*\[Phi][t]^2)] + \[Phi][t]*0.5^2 == 0, \[Phi][0] == 1, \[Phi]'[0] == 0}, \[Phi], {t, 0, 1}] ParametricPlot[Evaluate[{\[Phi]'[t], \[Phi][t]} /. sol, {t, 0, 1}]]
– nsap
Commented Oct 5, 2021 at 22:07

eq = ϕ''[t] +
3*ϕ'[t]*
Sqrt[(8*Pi*G/3)*(0.5*ϕ'[t]^2 +
0.5*m^2*ϕ[t]^2)] + ϕ[t]*m^2 == 0;
sol = ParametricNDSolve[{eq /. {G -> 1, m -> 0.5}, ϕ'[0] ==
a, ϕ[0] == b}, {ϕ, ϕ'}, {t, 0, 1}, {a, b}];
ParametricPlot[
Flatten[Table[{ϕ[a, b][t], ϕ'[a, b][t]}, {a, -1,
1, .25}, {b, -1, 1, .25}], 1] /. sol // Evaluate, {t, 0, 1}]
(* %/.


• Can I remove arrows in it to get lines only? I tried it using Line-> True but it gave error.
– Jpmg
Commented Oct 7, 2021 at 1:11
• @Jpmg Just remove the last line /. Line[a_] :> {Arrowheads[{{0.025, .85}}], Arrow[a]} Commented Oct 7, 2021 at 1:22
• I want these lines to be continuous trajectories. Plane lines properly rotating around the central part.
– Jpmg
Commented Oct 7, 2021 at 1:45
• @Jpmg Maybe set {t,0,10} ? eq = ϕ''[t] + 3*ϕ'[t]* Sqrt[(8*Pi*G/3)*(0.5*ϕ'[t]^2 + 0.5*m^2*ϕ[t]^2)] + ϕ[t]*m^2 == 0; sol = ParametricNDSolve[{eq /. {G -> 1, m -> 0.5}, ϕ'[0] == a, ϕ[0] == b}, {ϕ, ϕ'}, {t, 0, 10}, {a, b}]; ParametricPlot[ Flatten[Table[{ϕ[a, b][t], ϕ'[a, b][t]}, {a, -1, 1, .25}, {b, -1, 1, .25}], 1] /. sol // Evaluate, {t, 0, 10}, AspectRatio -> 1] Commented Oct 7, 2021 at 1:50
• That worked awesomely. A many thanks and now the code is also understandable for me.
– Jpmg
Commented Oct 7, 2021 at 1:58
 Manipulate[ Module[ {G = 1, t, tfinal = 1},
eqn := {\[Phi]''[t] +
3*\[Phi]'[t]*
Sqrt[(8*Pi*G/3)*(0.5*\[Phi]'[t]^2 + m^2*\[Phi][t]^2)] + \[Phi][
t]*m^2 == 0, \[Phi][0] == IC1, \[Phi]'[0] == IC2};
sol = First[NDSolve[eqn, \[Phi], {t, 0, tfinal}]];

plot  =
ParametricPlot[
Evaluate[{\[Phi]'[t], \[Phi][t]} /. sol, {t, 0, tfinal}]]

],
"postion",
{{IC1, 1, "\[Phi][0]"}, 0.5, 1.5, .1, Appearance -> "Labeled",
ImageSize -> Tiny},
Delimiter, "velocity",
{{IC2, 0, "\[Phi]'[0]"}, 0, 1, .1, Appearance -> "Labeled",
ImageSize -> Tiny},
Delimiter, "mass",
{{m, 0.5, "m"}, 0.1, 1, .1, Appearance -> "Labeled",
ImageSize -> Tiny},
TrackedSymbols :> {IC1, IC2, m}, ControlPlacement -> Left]

• Thanks for the code.
– Jpmg
Commented Oct 7, 2021 at 1:58

Try this:

sol = Table[
NDSolve[{\[Phi]''[t] +
3*\[Phi]'[t]*
Sqrt[(8*Pi*1/3)*(0.5*\[Phi]'[t]^2 +
0.5*0.5^2*\[Phi][t]^2)] + \[Phi][t]*0.5^2 ==
0, \[Phi][0] == RandomReal[{-1, 1}], \[Phi]'[0] ==
RandomReal[{-1, 1}]}, \[Phi], {t, 0, 1}], 50];

ParametricPlot[Evaluate[{\[Phi][t], \[Phi]'[t]} /. sol], {t, 0, 1},
PlotRange -> All] /. Line -> Arrow


Have fun!

• Thank you for the help.
– Jpmg
Commented Oct 7, 2021 at 1:58