NDSolve is not deterministic in its solutions??
- To: mathgroup at smc.vnet.net
- Subject: [mg129321] NDSolve is not deterministic in its solutions??
- From: JBB <barandiaran.juan at gmail.com>
- Date: Wed, 2 Jan 2013 21:15:47 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
I have a NDSolve in Mathematica 8 .0.1 in which I want to extract (with Sow) the maximums and minimums and also stop the integration when the maxima of the oscillation is under a threshold.
Both conditions require \[Alpha]'[t]==0.
The issue I find is that if I execute the same line of code several times, sometimes it records the last maxima and sometimes it doesn't.
Is there a way to avoid this non deterministic behaviour in NDSolve?
I solved it checking later if the last maxima is in the list and if it is not, adding it in another line of code, but this doesn't look a "nice" solution.
Here is the code:
Module[{interp, tmaxTemp, surf},
surf = 0.162;
Label[repeatChangingSurface]; (*If NDSolve has errors returns here \
and tries again with a slightly different surface *)
tmaxTemp =
1000;(* Define a long enough time of integration to ensure \
convergence *)
interp =
FlattenAt[ (*Eliminate unneccesary nestings in the list result *)
Reap[Quiet@
NDSolve[{1.1089 \[Alpha]''[t] + 1.2936 Sin[\[Alpha][t]] +
210*surf*
(0.33 Sin[\[Alpha][t]] + (
0.33^2 Cos[\[Alpha][t]] Sin[\[Alpha][t]])/Sqrt[
1 - 0.33^2 Sin[\[Alpha][t]]^2]) *
UnitStep[Sin[\[Alpha][t] + Pi*UnitStep[-\[Alpha]'[t]]]] ==
0, \[Alpha][0] == -Pi/3, \[Alpha]'[0] == 0}, \[Alpha], {t, 0,
tmaxTemp},
Method -> {"EventLocator",
"Event" -> {\[Alpha]'[t], \[Alpha]'[t]},
"EventCondition" -> {True, (Abs[\[Alpha][t]] < 0.01)},
"EventAction" :> {Sow[{t, \[Alpha][t], surf}],
Throw[tmaxTemp = t, "StopIntegration"]}}]], {{1}, {2}}];
If[tmaxTemp == interp[[1]][[1]][[2]][[1]][[1]][[2]],(*
If tmaxTemp is the same as the last value of the interp function,
the NDSolve has finished succesfully*)
interp,
surf = surf*1.0001;
Goto[repeatChangingSurface];] (* Else there was an error in NDSolve \
and we try with a sligthly different surface *)
]
Notice that if you repeat the execution several times the list of maxima changes.
Sometimes you get this result:
{{\[Alpha] -> \!\(\*
TagBox[
RowBox[{"InterpolatingFunction", "[",
RowBox[{
RowBox[{"{",
RowBox[{"{",
RowBox[{"0.`", ",", "7.578028259624528`"}], "}"}], "}"}], ",", "\<\"<>\"\>"}],
"]"}],
False,
Editable->False]\)}, {{1.97478, 0.283895, 0.162}, {3.84739, -0.0799267,
0.162}, {5.71299, 0.0225631, 0.162}, {7.57803, -0.00637085, 0.162}}}
and sometime this other:
{{\[Alpha]->InterpolatingFunction[{{0.,7.57803}},<>]},{{1.97478,0.283895,0.162},{3.84739,-0.0799267,0.162},{5.71299,0.0225631,0.162}}}