MathGroup Archive 2011

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

Search the Archive

Re: Seeing a solution of a differential equation as it run, using EvaluationMonitor

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118894] Re: Seeing a solution of a differential equation as it run, using EvaluationMonitor
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Sat, 14 May 2011 03:09:39 -0400 (EDT)

On 5/13/2011 7:30 PM, Nasser M. Abbasi wrote:
> Here is a small example of looking at solution of some basic made
> up ode as it runs:
>
> --------------------
> x1 = 0; x2 = 0;
> pt = {{x1, x2}};
>
> Dynamic[ListPlot[pt, Joined ->  False, PlotRange ->  {{0, 2*Pi}, {0, 7}}]]
>
> (*when pt is updated below, this causes ListPlot to revaluate automatically*)
> process[t_, y_] := Module[{},
>          {pt = Append[pt, {t, y}]};
>           Pause[0.05] ]
>
> (*solve the ode, use EvaluationMonitor*)
> eq = y''[t] == Cos[t];
> sol = NDSolve[{eq, y[0] == 1, Derivative[1][y][0] == 1}, y[t],
>      {t, 0, 2*Pi}, EvaluationMonitor :>  process[t, y[t]]]
>

I thought I update the above example to show how to do
it inside Manipulate.

A Manipulate slider is used to enter maximum simulation time,
and then the display on the Manipulate output will show
the solution, but one step at time (as opposed to normal
way of showing the final solution in one shot.)

--------------------------------------
Manipulate[
(
   pt = {{0, 0}};

   NDSolve[{y''[t] == Cos[t], y[0] == 1, y'[0] == 1}, y[t], {t, 0, maxt},
          EvaluationMonitor :>process[t,y[t]]];

   Dynamic[ListPlot[pt, Joined -> False ,PlotRange -> {{0, maxt}, {0, 7}}]]
),

   {{maxt,0.5,"max time"},0.5,10,.1},

   TrackedSymbols->{maxt},
   SynchronousUpdating->False,  (*important to have this*)
   ContinuousAction->False,     (*and this also*)
   Initialization:>
   (
     process[t_,y_]:=Module[{},pt=Append[pt, {t, y}]; Pause[0.01]]
   )
]
------------------------------------

That is all. I think it is a little more interesting to
be able to see the solution as it progress. The Pause[0.01]
is needed to be able to do this. One can adjust this as
needed (may be make a Manipulate parameter also).

Isn't Mathematica fun?

--Nasser


  • Prev by Date: Re: Protein Sequence Alignment efficiency
  • Next by Date: Re: Noticed now much improved response at Wolfram demonstrations.
  • Previous by thread: Seeing a solution of a differential equation as it run, using EvaluationMonitor
  • Next by thread: Re: Seeing a solution of a differential equation as it run, using EvaluationMonitor