0
$\begingroup$

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}]
$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$ Oct 5, 2021 at 21:26
  • $\begingroup$ see the second line of the code. I have provided the values of G and m. $\endgroup$
    – Jpmg
    Oct 5, 2021 at 21:33
  • $\begingroup$ 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. $\endgroup$ Oct 5, 2021 at 21:54
  • $\begingroup$ 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. $\endgroup$
    – Jpmg
    Oct 5, 2021 at 21:56
  • $\begingroup$ 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}]] $\endgroup$
    – nsap
    Oct 5, 2021 at 22:07

3 Answers 3

2
$\begingroup$

Here we have made some changes of your code.

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}]
(* %/. 
 Line[a_] :> {Arrowheads[{{0.025, .85}}], Arrow[a]}*)

enter image description here

$\endgroup$
5
  • $\begingroup$ Can I remove arrows in it to get lines only? I tried it using Line-> True but it gave error. $\endgroup$
    – Jpmg
    Oct 7, 2021 at 1:11
  • $\begingroup$ @Jpmg Just remove the last line /. Line[a_] :> {Arrowheads[{{0.025, .85}}], Arrow[a]} $\endgroup$
    – cvgmt
    Oct 7, 2021 at 1:22
  • $\begingroup$ I want these lines to be continuous trajectories. Plane lines properly rotating around the central part. $\endgroup$
    – Jpmg
    Oct 7, 2021 at 1:45
  • $\begingroup$ @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] $\endgroup$
    – cvgmt
    Oct 7, 2021 at 1:50
  • $\begingroup$ That worked awesomely. A many thanks and now the code is also understandable for me. $\endgroup$
    – Jpmg
    Oct 7, 2021 at 1:58
0
$\begingroup$
 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]
$\endgroup$
1
  • $\begingroup$ Thanks for the code. $\endgroup$
    – Jpmg
    Oct 7, 2021 at 1:58
0
$\begingroup$

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

enter image description here

Have fun!

$\endgroup$
1
  • $\begingroup$ Thank you for the help. $\endgroup$
    – Jpmg
    Oct 7, 2021 at 1:58

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.