« View all new features in
Mathematica
9
◄
previous
|
next
►
New in
Mathematica
9
›
Advanced Hybrid and Differential Algebraic Equations
Billiard Balls
In[3]:=
X
GraphicsGrid[{Map[ ParametricPlot[{x[t], y[t]} /. sol1, {t, 0, #}, Frame -> True, FrameTicks -> None, PlotRange -> 1, Axes -> False, PlotStyle -> Blue] &, {5, 20, 100}], Map[ParametricPlot[{x[t], y[t]} /. sol2, {t, 0, #}, Frame -> True, FrameTicks -> None, PlotRange -> 1, Axes -> False, PlotStyle -> Red] &, {5, 20, 100}]}]
In[6]:=
X
GraphicsGrid[{Map[ ParametricPlot3D[{x[t], y[t], z[t]} /. sol3d1, {t, 0, #}, Axes -> False, PlotStyle -> Blue] &, {5, 20, 100}], Map[ParametricPlot3D[{x[t], y[t], z[t]} /. sol3d2, {t, 0, #}, Axes -> False, PlotStyle -> Red] &, {1, 50, 200}]}]
Out[1]=
Play Animation
»
Stop Animation
»
Model the trajectory of a billiard ball struck from two nearly identical angles.
In[2]:=
X
{sol1, sol2} = Map[NDSolve[{x'[t] == a[t], y'[t] == b[t], x[0] == 0, y[0] == 0, a[0] == 1, b[0] == #, WhenEvent[x[t]^2 == 1, a[t] -> -a[t]], WhenEvent[y[t]^2 == 1, b[t] -> -b[t]]}, {x, y}, {t, 0, 100}, DiscreteVariables -> {a, b}] &, {Rationalize[Sqrt[2], .01], Sqrt[ 2]}];
Out[3]=
Model the trajectory of a 3D billiard ball struck from two different angles.
In[4]:=
X
ball3d[r1_?NumericQ, r2_?NumericQ] := NDSolve[{x'[t] == a[t], y'[t] == b[t], z'[t] == c[t], x[0] == 0, y[0] == 0, z[0] == 0, a[0] == 1, b[0] == r1, c[0] == r2, WhenEvent[x[t]^2 - 1, a[t] -> -a[t]], WhenEvent[y[t]^2 - 1, b[t] -> -b[t]], WhenEvent[z[t]^2 - 1, c[t] -> -c[t]]}, {x[t], y[t], z[t]}, {t, 0, 200}, DiscreteVariables -> {a, b, c}];
In[5]:=
X
{sol3d1, sol3d2} = {ball3d[1/3, 2/3], ball3d[Sqrt[2], Sqrt[3]]};
Out[6]=