I'm trying to find all the roots of the solution to a differential equation. Using NSolve or Reduce I don't get the roots, so I'm using an iterative method which I found in physicsforums.com. This method solves my problem, but you have to choose the increment and thus in some cases it might give headaches. I wonder if there is any more general approach.
Following is a sample code:
data = NDSolve[{1.09 x''[t] - 0.05 x'[t] + 1.1759 Sin[x[t]] == 0,
x[0] == Pi/3, x'[0] == 0}, x, {t, 0, 50}]
{{x->InterpolatingFunction[{{0.,50.}},<>]}}
First attempt with no success:
sol = NSolve[x'[t] == 0 /. data , t]
NSolve::ifun: Inverse functions are being used by NSolve, so some solutions may not be found; use Reduce for complete solution information. >>
{{t->InverseFunction[InterpolatingFunction[{{0.,50.}},<>],1,1][0.]}}
Second attempt with no success:
sol = Reduce[x'[t] == 0 /. data , t]
Reduce::inex: Reduce was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Reduce require exact input, providing Reduce with an exact version of the system may help. >>
Reduce[{InterpolatingFunction[{{0.,50.}},<>][t]==0},t]
Third attempt, works fine, but manually choosing dt
could cause problems with some equations:
dt = 0.1;
tmin = 0.;
tmax = 50.;
Union[Table[t /. FindRoot[x'[t] == 0 /. data ,{t, tInit, tmin, tmax}],
{tInit, tmin + dt,tmax - dt, dt}], SameTest->(Abs[#1 - #2] < 10^-2&)]
{0., 3.26812, 6.58301, 9.95657, 13.4054, 16.9538, 20.6403, 24.533, 28.7857, 34.2571}
Is there any more elegant method to find all roots in a range?
Answer
I'm slightly surprised nobody's already mentioned the event location capabilities of Mathematica, as it's the most compact way to find the roots of an interpolating function that came from NDSolve[]
. I don't have Mathematica on this machine I'm writing in, but I'd do something like this:
Reap[NDSolve[{1.09 x''[t] - 0.05 x'[t] + 1.1759 Sin[x[t]] == 0, x[0] == Pi/3, x'[0] == 0},
x, {t, 0, 50}, Method -> {"EventLocator",
"Event" -> x[t], "EventAction" :> Sow[t]}]]
which should yield the interpolating function and the list of roots.
At least, that's how its done in version 8. Version 9 has the WhenEvent[]
function, which can be used like so:
Reap[NDSolve[{1.09 x''[t] - 0.05 x'[t] + 1.1759 Sin[x[t]] == 0, x[0] == Pi/3, x'[0] == 0,
WhenEvent[x[t] == 0, Sow[t]]}, x, {t, 0, 50}]]
Comments
Post a Comment