Question

Given the following code:

s := NDSolve[{x''[t] == -x[t], x[0] == 1, x'[0] == 1}, x, {t, 0, 5 }]
Plot[Evaluate[{x[t]} /. s], {t, 0, 3}]

This plots the solution to the differential equation. How would I numerically solve for a zero of x[t] where t ranges between 0 and 3?

Was it helpful?

Solution

The original question was answered by @rcollyer. I am answering the question you posted in your first comment to rcollyer's answer:

But what if instead our s is "s := NDSolve[{x'[t]^2 == -x[t]^3 - x[t] + 1, x[0] == 0.5}, x, {t, 0, 5}]" Then the FindRoot function just gives back an error while the plot shows that there is a zero around 0.6 or so.

So:

s = NDSolve[{x'[t]^2 == -x[t]^3 - x[t] + 1, x[0] == 0.5}, 
             x, {t, 0, 1}, Method -> "StiffnessSwitching"];
Plot[Evaluate[{x[t]} /. s], {t, 0, 1}]
FindRoot[x[t] /. s[[1]], {t, 0, 1}]

enter image description here

{t -> 0.60527}

Edit

Answering rcollyer's comment, the "second line" comes from the squared derivative, as in:

s = NDSolve[{x'[t]^2 == Sin[t], x[0] == 0.5}, x[t], {t, 0, Pi}];
Plot[Evaluate[{x[t]} /. s], {t, 0, Pi}]

enter image description here

Coming from:

DSolve[{x'[t]^2 == Sin[t]}, x[t], t]
(*
{{x[t] -> C[1] - 2 EllipticE[1/2 (Pi/2 - t), 2]}, 
 {x[t] -> C[1] + 2 EllipticE[1/2 (Pi/2 - t), 2]}}
*)

OTHER TIPS

FindRoot works

In[1]:=  FindRoot[x[t] /. s, {t, 0, 3}]
Out[1]:= {t -> 2.35619}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top