The following is a fairly hacky way of achieving continuous evaluation with history.
de[t0_, y0_, yp0_, dt_, a_, b_, c_, d_, \[Omega]_] :=
Module[{y, t, sol},
sol = y /. First@NDSolve[{
y''[t] + d y'[t] + a y[t] + b y[t]^3 == c Cos[\[Omega] t],
y[t0] == y0,
y'[t0] == yp0},
y, {t, t0, t0 + dt}];
sol]
evalpts[f_, t_, dt_, n_] := Table[{x, f[x]}, {x, t, t + dt, dt/n}]
t0 = 0;
y0 = 1;
yp0 = 0;
z0 = 1;
zp0 = 0;
history = 20;
length = 0;
oldest = 0;
ysols = {};
zsols = {};
n = 10;
Manipulate[
(* Solve differential equation *)
ysol = de[t0, y0, yp0, dt, a1, b1, c1, d1, w1];
zsol = de[t0, z0, zp0, dt, a2, b2, c2, d2, w2];
(* Get points in this segment *)
ypts = evalpts[ysol, t0, dt, n];
zpts = evalpts[zsol, t0, dt, n];
(* Build up our solution history.
Replace the oldest part of the solution
with the newest one *)
If[length < history,
ysols = Append[ysols, ypts];
zsols = Append[zsols, zpts];
length++,
ysols = ReplacePart[ysols, oldest -> ypts];
zsols = ReplacePart[zsols, oldest -> zpts];
];
oldest++;
If[oldest == history + 1, oldest = 1];
(* Set our initial conditions for next segment *)
t0 = t0 + dt;
{y0, yp0} = {ysol[t0], ysol'[t0]};
{z0, zp0} = {zsol[t0], zsol'[t0]};
(* Wait to make the animation a bit slower *)
Pause[0.05];
(* Display the solution *)
ListLinePlot[{Sort@Flatten[ysols, 1], Sort@Flatten[zsols, 1]},
PlotRange -> {ymin, ymax}, Axes -> {False, False}],
{{a1, 0.1}, -1, +1}, {{a2, 0.1}, -1, +1},
{{b1, 0}, -1, +1}, {{b2, 0.01}, -1, +1},
{{c1, 0}, -1, +1}, {{c2, 0}, -1, +1},
{{d1, 0}, -1, +1}, {{d2, 0}, -1, +1},
{{w1, 0}, 0, 10}, {{w2, 0}, 0, 10},
{{dt, 0.5}, 0.01, 1, 0.01},
{{ymin, -1.2}, -10, -1},
{{ymax, +1.2}, 1, 10}
]
You can adjust the parameters in real time. It is fairly fragile though. It basically works by creating a circular queue of previous solutions and continuously replacing the oldest portion of the solution at each step. I added a small delay so it didn't evaluate too fast.
You can then visualize two different solutions with different initial parameters.
There's LOTS of room for improvement for this.
NDSolve[]
, I've seen the use ofMethod -> "EventLocator"
along with GUI bits likeManipulate[]
; of course it's limited only to NDSolve. In the past, people had to useGUIKit`
, like with the Equation Trekker... $\endgroup$