# Solve equation in a interval

I have a particle bouncing in a rigid box, my code is this:

L = 5;
L2 = 8;
L3 = 10;
a[x_] := 1 - 2 Boole@OddQ@Quotient[x, L];
a2[x_] := 1 - 2 Boole@OddQ@Quotient[x, L2];
a3[x_] := 1 - 2 Boole@OddQ@Quotient[x, L3];

dir = Normalize[{1, 2, 3}];
x0 = {0, L2/2, L3/2};

x[t_] := (t*dir + x0).{1, 0, 0};
x2[t_] := (t*dir + x0).{0, 1, 0};
x3[t_] := (t*dir + x0).{0, 0, 1};
P[t_] := {Mod[a[x[t]] x[t], L] , Mod[a2[x2[t]] x2[t], L2],Mod[a3[x3[t]]x3[t], L3]};

ParametricPlot3D[P[t], {t, 0, 30},  PlotRange -> {{0, L}, {0, L2}, {0, L3}}]


Now i want to calculate the time needed to the particle to reach a little square of 1x1 of area at the top of the box, something like a "Findroot" but with an interval as entry. Any susgestions please?

• do you realise this is a linear equation on time, don't you? You can write the analytical solution in this case. Also, maybe you could consider to link the question where that code comes from, it looks eerily familiar. Apr 21, 2016 at 4:03
• @tsuresuregusa The equations are probably from this answer.
– shrx
Apr 21, 2016 at 9:57

I presume you wish to find the first time that P[t] reaches L3. This is given by

FindRoot[P[t][[3]] == L3, {t, 6}]
(* {t -> 6.2361} *)


It is helpful to plot the curve (here as far as t == 35) along with points at integer values of t.

Show[ParametricPlot3D[P[t], {t, 0, 35}, PlotRange -> {{0, L}, {0, L2}, {0, L3}}],
ListPointPlot3D[Table[P[t], {t, 0, 35}], PlotStyle -> Directive[Red, PointSize -> Medium]]]


The time for the second instance that the path reaches L3 can then be seen to be about t == 30. For completeness,

FindRoot[P[t][[3]] == L3, {t, 30}]
(* {t -> 31.1805} *)


The OP in a comment below asks to find when the path reaches L3 and lies within the range 2.4 < P[t][[1]] < 2.6 || 3.9 < P[t][[2]] < 4.1 in the other two coordinates. It turns out that this never happens, as can be seen from

ParametricPlot3D[P[t], {t, 0, 10000}, PlotRange -> {{0, L}, {0, L2}, {0, L3}},
PlotPoints -> 1000]


or from

lt = Quiet@Table[FindRoot[P[t][[3]] == L3, {t, 6 + 25 i}], {i, 0, 10000}];
lp = P[t] /. lt;
ListPlot[#[[1 ;; 2]] & /@ lp, PlotRange -> {{0, L}, {0, L2}},
PlotStyle -> PointSize[Small]]


To find a time for which the path reaches L3 and comes near, for instance, {5., 6.} in the other two coordinates, use

i = 0; p1 = -1; p2 = -1;
Quiet@While[! (4.9 < p1 < 5.1 && 5.9 < p2 < 6.1), {t0, p1, p2} = {t, P[t][[1]], P[t][[2]]}
/. FindRoot[P[t][[3]] == L3, {t, 6 + 25 i}]; i = i + 1]; t0
(* 168.375 *)

• Not just L3, but a subset of the top face, something like: P[t][[3]]==L3, but at the same time 2.4 < P[t][[1]] < 2.6 || 3.9 < P[t][[2]] < 4.1. I guess with a "If" but perhaps there's a more elegant way. Apr 20, 2016 at 17:31
• @jesus The curve does not intersect this area, as shown in an addendum to my answer. So, I found where it intersected another area. Apr 21, 2016 at 3:55