I am having issues finding the root of an interpolating function obtained from NDSolve. For example:
sol = NDSolve[{y''[t] == -y[t], y[0] == 1, y'[0] == 0}, y, {t, 0, 10}]
(* {{y -> InterpolatingFunction[{{0., 10.}}, <>]}} *)
Of course, the solution is simply $y=\cos(t)$ (this is a simpler example, my actual function doesn't have an analytic solution), but absurd things happen when I try to find roots:
FindRoot[y[t] == 0 /. sol, {t, 0}]
(* {t -> -1.77636*10^-15} *)
It gives me a number close to 0, which is not a root of $\cos(t)$. If I start elsewhere I get other stuff that is also wrong. For example, starting near 0.1, I get 10.99 which is not a zero either...
FindRoot[y[t] == 0 /. sol, {t, 0.1}]
(* {t -> 10.9973} *)
If I start the search near $\pi/2$ then I do get that answer, but I want to find the first positive zero and my actual function has a parameter that can push the zero arbitrarily close to $t=0$, so I need to start the search near 0 but then it just always returns something close to zero which is not the solution... is there any way to make this work?
My second attempt was to use FindInstance
, but with interpolating functions it doesn't evaluate at all, it just returns the same thing I wrote...
FindInstance[y[t] == 0 /. sol, t]
Out[36]= FindInstance[{InterpolatingFunction[{{0., 10.}}, <>][t] == 0}, t]
And it just does that and won't give me a number no matter what I do... am I doing something wrong?
Thanks for the help
FindRoot[y[t] == 0 /. sol, {t, 0.5, 0.7}]
) leads to the $\pi/2$ solution. On the other hand, if you start with $0$ but restrict the search domain to that over which you solved the differential equation (e.g.FindRoot[y[t] == 0 /. sol, {t, 0, 0, 10}]
) you get an error explaining the weird behavior you observed ("The point {0.} is at the edge of the search region {0.,10.} in coordinate 1 and the computed search direction points outside the region."). $\endgroup$