Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2371

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

From "Nasser M. Abbasi" <nma@12000.org>
Newsgroups comp.soft-sys.math.mathematica
Subject Seeing a solution of a differential equation as it run, using EvaluationMonitor
Date 2011-05-14 07:14 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iqla48$a00$1@smc.vnet.net> (permalink)

Show all headers | View raw


Just thought to share this with the group.

Been doing some simulation of Vehicle dynamics for a course
and wanted a way to plot the car as the solution is computed,
and not after the fact.

Found a nice use for EvaluationMonitor to do this.

By using EvaluationMonitor and Dynamic Plot, it turned out to
be really easy in Mathematica to do this sort of thing.

The idea is to make the Plot from inside the EvaluationMonitor,
on a Dynamic ListPLot, and append the solution to some variable
along the way.

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]]]

---------------------------------------------------

When running the above, one gets the same plot as the one
by running the following command on the final solution:

Plot[Evaluate[y[t]/.sol],{t,0,2 Pi},PlotRange->All]

I had to add a Pause[0.05] in the EvaluationMonitor to slow it
down a little to get the effect of seeing the solution advance as
NDSolve works, else it will just make one plot and lose the effect
of the animation part. There might be better way to do this.

--Nasser

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Seeing a solution of a differential equation as it run, using EvaluationMonitor "Nasser M. Abbasi" <nma@12000.org> - 2011-05-14 07:14 +0000

csiph-web