Seeing a solution of a differential equation as it run, using EvaluationMonitor
- To: mathgroup at smc.vnet.net
- Subject: [mg118891] 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:06 -0400 (EDT)
- Reply-to: nma at 12000.org
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