0
ClearAll["Global`*"]
Manipulate[Module[{place,velocity,sol, g, m},sol = NDSolve[{-2*g*m*x[t]+4*m*x[t]*(x'[t])^2-0.5*m*(16*x[t]*(x'[t])^2+2*x''[t]+8*(x[t])^2*x''[t])==0, x[0] == place, x'[0]==velocity}, x, {t, p-1, p},MaxSteps->100000];Graphics[{White, Rectangle[{-10,-10},{10,10}],Black,Disk[{sol[t], (sol[t])^2},0.1]}]], {{g, 9.8, "gravitational acceleration"}, 1, 100, Appearance->"Labeled"}, {{m, 2, "mass"}, 1, 10, Appearance-> "Labeled"}, {{place, 5, "starting place"}, -10, 10, Appearance->"Labeled"}, {{velocity, 2, "starting velocity"}, 1, 10, Appearance-> "Labeled"}, {{p, 0, "animation"}, 0, Infinity, ControlType -> Trigger}]

It just shows a white square with that small black disk on it. But there is no animation whatsoever.

What is wrong with my code? Also, it would be nice to somehow include a parabola too, so we can see how that ball rolls up and down along it

David G. Stork
  • 41,180
  • 3
  • 34
  • 96
anon
  • 163
  • 4
  • Welcome to Mathematica StackExchange! There are several problems with your code ... First, remove Module. Second, replace NDSolve with NDSolveValue. Third, fix the integration limits in NDSolveValue[..., {t, 0, 10}]. Fourth, use Disk[{sol[p], (sol[p])^2}, 0.1]. With these fixes, the code at least runs ... – Domen Oct 15 '22 at 23:02
  • @Domen I did that and it does not work. Could you maybe tell me the exact code I should type in instead? – anon Oct 15 '22 at 23:09
  • 1
    Manipulate[sol = NDSolveValue[{-2*g*m*x[t] + 4*m*x[t]*(x'[t])^2 - 0.5*m*(16*x[t]*(x'[t])^2 + 2*x''[t] + 8*(x[t])^2*x''[t]) == 0, x[0] == place, x'[0] == velocity}, x, {t, 0, 10}, MaxSteps -> 100000]; Graphics[{White, Rectangle[{-10, -10}, {10, 10}], Black, Disk[{sol[p], (sol[p])^2}, 0.1]}] , {{g, 9.8, "gravitational acceleration"}, 1, 100}, {{m, 2, "mass"}, 1, 10}, {{place, 5, "starting place"}, -10, 10}, {{velocity, 2, "starting velocity"}, 1, 10}, {{p, 0, "animation"}, 0, Infinity, ControlType -> Trigger}] – Domen Oct 15 '22 at 23:14
  • @Domen That's great! Thanks! Do you know maybe a way to include the plot of the $x^2$ parabola here, too? So we can see how that black point (disk) moves along it? – anon Oct 15 '22 at 23:41

1 Answers1

2

Maybe this.

Clear["Global`*"];
f[p_, g_, m_, place_, velocity_] := 
  Module[{sol}, 
   sol = NDSolveValue[{-2*g*m*x[t] + 4*m*x[t]*(x'[t])^2 - 
        0.5*m*(16*x[t]*(x'[t])^2 + 2*x''[t] + 8*(x[t])^2*x''[t]) == 0,
       x[0] == place, x'[0] == velocity}, x, {t, p - 1, p}, 
     MaxSteps -> 100000];
   Show[Plot[t^2, {t, -10, 10}], 
    Graphics[{Black, AbsolutePointSize[10], 
      Point[{sol[p], (sol[p])^2}]}, Axes -> True], 
    PlotRange -> {{-10, 10}, {-10, 50}}]];
Manipulate[
 f[p, g, m, place, velocity], {{g, 9.8, "gravitational acceleration"},
   1, 100, Appearance -> "Labeled"}, {{m, 2, "mass"}, 1, 10, 
  Appearance -> "Labeled"}, {{place, 5, "starting place"}, -10, 10, 
  Appearance -> "Labeled"}, {{velocity, 2, "starting velocity"}, 1, 
  10, Appearance -> "Labeled"}, {{p, 0, "animation"}, 0, Infinity, 
  ControlType -> Trigger}]

enter image description here

cvgmt
  • 72,231
  • 4
  • 75
  • 133
  • This works! Do you maybe know how we could simultaneously plot the graph $y=x^2$ too, so we can see how the black point (disk) moves along it? – anon Oct 16 '22 at 00:04
  • Ok, I figured it out, instead of sol[p], (sol[p])^2 in the ParametricPlot you take x, x^2. But how can we align the parabola in the middle? Because I'd like it to be a coordinate system with the parabola $x^2$ and that black ball moving along it – anon Oct 16 '22 at 00:10
  • 1
    @anon it seems like you keep adding requirements to your original question. It is better to instead ask a new question, in most cases. – CA Trevillian Oct 16 '22 at 04:49
  • @anon: Title of your question says: "ball rolling up and down a parabola". But there is difference between ball rolling inside parabola and ball's center of mass moving along parabola. The two cases are the same only if the ball radius is negligible - but then I would not call it a ball but a point and a point cannot roll, only move along some trajectory. – azerbajdzan Oct 16 '22 at 20:33