MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Rewrite a Manipulate to call NDSolve just once?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127149] Rewrite a Manipulate to call NDSolve just once?
  • From: RBaillie <bobbaillie at frii.com>
  • Date: Mon, 2 Jul 2012 05:29:27 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

Here is a nice pendulum demo from http://demonstrations.wolfram.com/NonlinearizedMotionOfASimplePendulum

Like all other demos I've seen, NDSolve[ ] is called many times.  Is
there a way to write this so that NDSolve is called once each time the
"release system" button is pressed?

Manipulate[
  If[run == time, run = 0];
  Module[
    { sol, \[Theta], t },

    sol = NDSolve[{ \[Theta]''[t] + (9.8/l)*Sin[\[Theta][t]] == 0,
                    \[Theta][0] == theta, \[Theta]'[0] ==
thetaPrime },
                    \[Theta], {t, 0, time}, Method ->
"StiffnessSwitching"];

    With[{angle = \[Theta][run] /. First[sol]},
      Graphics[
        { {Lighter[Brown], Rectangle[{-4, 0}, {4, .5}]},   (* top bar
*)
          {Darker[Red], Disk[{0, 0}, .1]},                 (* top
pivot *)
          {Line[{{0, 0}, {l*Sin[angle], -l*Cos[angle]}}]}, (* pendulum
*)
          {Blue, Disk[{l*Sin[angle], -l*Cos[angle]}, .2]}  (* mass at
bottom *)
        } ,
        PlotRange -> {{-4, 4}, {.5, -4}},
        ImageSize -> {500, 300} / 2
      ]  (* end Graphics *)
    ]  (* end With *)
  ]  (* end Module *)
  ,
  { {l, 3, "length of pendulum"}, .5, 3.5, .1, Appearance ->
"Labeled"},
  { {theta, Pi/4, "initial angle (radians)"}, 0, Pi/2, .01, Appearance
-> "Labeled"},
  { {thetaPrime, 0, "initial angular velocity"}, 0, 5, Appearance ->
"Labeled"},
  { {time, 10.00, "time to watch motion"}, 0, 20, .01, Appearance ->
"Labeled"},
  { {run, 0, "release system"}, 0, time, .001, ControlType -> Trigger,
AnimationRate -> 1},
  SynchronousUpdating -> False,
  AutorunSequencing -> {1, 2}
]



  • Prev by Date: Re: Book
  • Next by Date: Re: How to export to Excel numbers as text fields?
  • Previous by thread: Re: Evaluate code fragment without making new cell for it?
  • Next by thread: Re: How to export to Excel numbers as text fields?