2
$\begingroup$

I was trying to use Manipulate to model a simple two-phase motion. In the first phase, a cart moves on a track in one-dimension with a certain acceleration and velocity. Then in the second phase, the cart crosses the track and falls due to gravity. I vary the time t with Manipulate, and use the formula ${ r }_{ 0 }+vt+\frac { 1 }{ 2 } a{ t }^{ 2 }$ to update the position of the cart. However, since the second phase happens at a latter time than the first phase, plug in t would not be correct for the second phase. For example, if the cart falls at time 5s, y should start at 0 instead of 1/2*g*5^2. How could I correctly manipulate the second phase of the motion?

$\endgroup$
  • $\begingroup$ How about a Piecewise function that is the first equation for the initial times and the second equation for later times. $\endgroup$ – bill s Sep 21 '16 at 3:42
  • $\begingroup$ How could I make t of the second part of the piecewise function start at 0? Since I put it inside a Manipulate, t is updated by Manipulate. $\endgroup$ – amasics Sep 21 '16 at 3:54
  • $\begingroup$ You don't want the 2nd part of the piecewise function to "start at 0". You want to define the 1st part to go from 0 to t1 and the 2nd part to go from t1, to t2. You can easily determine t1 from the length of the track and the initial speed and acceleration of the cart. You can determine t2 from the height of the track and the velocity of the cart at t1. $\endgroup$ – m_goldberg Sep 21 '16 at 4:06
  • $\begingroup$ But if I start from t1. Then the cart will immediately fall from its initial position to 1/2*g*t1^2, which is not correct physically. $\endgroup$ – amasics Sep 21 '16 at 4:11
  • $\begingroup$ @amasics you should consider adding any research (like equations of motion) and code, even non-functional, so that others can pick up where you left off. This also lowers the barrier for people looking to answer. $\endgroup$ – Musang Sep 28 '16 at 14:05
4
$\begingroup$

As mentioned by bill s, I think that a piecewise function is the right tool for the job. Here's how I did it.

Modelling

The first step is to obtain the trajectory equation $x(t)$ and $y(t)$. It's not too difficult to derive them by hand, but let's use Mathematica while we're at it! Let us call $d$ the position at which the track stops.

In the first phase of movement, the cart is submitted to uniform acceleration $a$: $$\frac{d^2x}{dt^2}=a$$

x1 = DSolve[{ x''[t] == a, x'[0] == v0, x[0] == r0}, x[t], t]

And of course $y(t)=h$, the track height.

We should now obtain $t_d$ and $v_d$ the time and velocity at which the cart crosses $d$.

teq = Solve[a/2 t^2 + v0 t + r0 - d == 0, t][[2]]
tatd = t /. teq
vatd = a (t /. teq) + v0;

Note that we take the second solution because it's the positive one.

So now we use them for the boundary conditions of the equations of the freefalling cart:

$$\frac{d^2x}{dt^2}=0$$ $$\frac{d^2y}{dt^2}=-g$$

x2 = DSolve[{m x''[t] == 0, x'[tatd] == vatd, x[tatd] == d}, x[t],t];
y3 = DSolve[{m y''[t] == -m g, y'[tatd] == 0, y[tatd] == h}, y[t], t];

So now we can plot these trajectories as piecewise solutions of $t$ (using ballpark parameters):

a = 3; r0 = 0; d = 15; m = 3; g = 10; v0 = 0; h = 5;
GraphicsGrid[{{
   Plot[Piecewise[{{x[t] /. First@x1, t <= tatd}, {x[t] /. First@x2, 
       t > tatd}}], {t, 0, 7}],
   Plot[Piecewise[{{h, t <= tatd}, {y[t] /. First@y3, t > tatd}}], {t,
      0, 7}]
   }}]

enter image description here

Animation

Now, we will use this as a basis for the animation. The idea is to generate a list of frames along the the trajectory we derived above and gather them as a movie using ListAnimate. I find this to be easier than going the full Dynamic/Manipulate route.

Generate a list of coordinates for our cart:

dt=0.05;
xlist = Table[
   Piecewise[{{x[t] /. First@x1, t <= tatd}, {x[t] /. First@x2, 
      t > tatd}}], {t, 0, 5, dt}];
ylist = Table[
   Piecewise[{{h, t <= tatd}, {y[t] /. First@y3, t > tatd}}], {t, 0, 
    5, dt}];
coordlist = Transpose[{xlist, ylist}];

dt can be adjusted for the smoothness of the animation.

Since you might want to play around with the model parameters, here's a module which lets you adjust your parameters on the fly:

Manipulate[
 a = adyn; r0 = r0dyn; d = ddyn; m = mdyn, g = gdyn; v0 = v0dyn; 
 h = hdyn; dt = animationsmoothness;
 x1 = DSolve[{ x''[t] == a, x'[0] == v0, x[0] == r0}, x[t], t];
 teq = Solve[a/2 t^2 + v0 t + r0 - d == 0, t][[2]];
 tatd = t /. teq;
 vatd = a (t /. teq) + v0;
 x2 = DSolve[{m x''[t] == 0, x'[tatd] == vatd, x[tatd] == d}, x[t], t];
 y3 = DSolve[{m y''[t] == -m g, y'[tatd] == 0, y[tatd] == h}, y[t], t];
 xlist = Table[
   Piecewise[{{x[t] /. First@x1, t <= tatd}, {x[t] /. First@x2, 
      t > tatd}}], {t, 0, 5, dt}];
 ylist = Table[
   Piecewise[{{h, t <= tatd}, {y[t] /. First@y3, t > tatd}}], {t, 0, 
    5, dt}];
 coordlist = Transpose[{xlist, ylist}];
 , {{adyn, 3}, 0, 5}, {{r0dyn, 0}, 0, 3}, {{ddyn, 15}, 10, 
  30}, {{mdyn, 3}, 1, 10}, {{gdyn, 10}, 5, 10}, {{v0dyn, 0}, 0, 
  5}, {{hdyn, 5}, 1, 20}, {{animationsmoothness, 0.05}, 0.01, 0.1}]

And finally:

ListAnimate[
 Table[
  Show[
   ParametricPlot[{Piecewise[{{x[t] /. First@x1, 
        t <= tatd}, {x[t] /. First@x2, t > tatd}}], 
     Piecewise[{{h, t <= tatd}, {y[t] /. First@y3, t > tatd}}]}, {t, 
     0, 5}, PlotRange -> {{0, 30}, {0, h + 1}}],
   Graphics[{
     Thick, Line[{{0, h - 1}, {d, h - 1}, {d, -10}}]
     , PointSize[0.05], Point[coordlist[[i]]]
     }]
   ]
  , {i, Length[coordlist]}]
 ]

A circular cart!

Final Tips

  • I detailed the steps in making the animation, but you only need to run the last two modules of code to make it work
  • Don't forget to re-evaluate the animation module once you change the parameters for it to update properly.
  • you can remove the trajectory of the cart simply by removing the ParametricPlot from the Show.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.