2
$\begingroup$

I am working with a Wolfram Demonstration that uses the Manipulate function (which I am not yet familiar with).

The code is

Manipulate[
 
 dist = 5;
 
 posA = -dist;
 posB = dist;
 vA2 = -((-mA vA1 + ee mB vA1 - mB vB1 - ee mB vB1)/(mA + mB));
 vB2 = -((-mA vA1 - ee mA vA1 + ee mA vB1 - mB vB1)/(mA + mB));
 
 (* vA1*t-dist+mA/10=vB1*t+dist+mB/10, (vA1-vB1)*t=2*dist+mB/10 *)
 tImpact = (20 dist - mA - mB)/(10 (vA1 - vB1));
 
 posImpactA = posA + vA1*tImpact;
 posImpactB = posB + vB1*tImpact;
 
 If[tt <= tImpact, posA2 = vA1*tt + posA, 
  posA2 = posImpactA + vA2*(tt - tImpact)];
 If[tt <= tImpact, posB2 = vB1*tt + posB, 
  posB2 = posImpactB + vB2*(tt - tImpact)];
 
 If[tt <= tImpact, 
  vecB = Graphics[{Red, Thick, 
     Arrow[{{posB2, 0}, {posB2 + vB1, 0}}]}], 
  vecB = Graphics[{Darker[Green], 
     Arrow[{{posB2, 0}, {posB2 + vB2, 0}}]}]];
 If[tt <= tImpact, 
  vecA = Graphics[{Red, Thick, 
     Arrow[{{posA2, 0}, {posA2 + vA1, 0}}]}], 
  vecA = Graphics[{Darker[Green], Thick, 
     Arrow[{{posA2, 0}, {posA2 + vA2, 0}}]}]];
 
 parA = Graphics[{Darker[Gray], Disk[{posA2, 0}, mA/10]}];
 parB = Graphics[{Darker[Gray], Disk[{posB2, 0}, mB/10]}];
 road = Graphics[{Lighter[Gray], Line[{{-10, 0}, {10, 0}}]}];
 Show[{road, parA, parB, vecA, vecB}, 
  PlotRange -> {{-10, 10}, {-1, 5}}, ImageSize -> 450],
 {{mA, 5, "mass of A"}, 1, 10, Appearance -> "Labeled"},
 {{mB, 10, "mass of B"}, 1, 10, Appearance -> "Labeled"},
 {{vA1, 5, "initial velocity of A"}, 1, 5, Appearance -> "Labeled"},
 {{vB1, -5, "initial velocity of B"}, -5, 0, Appearance -> "Labeled"},
 {{ee, 0.5, "coefficient of elasticity"}, 0, 1, 
  Appearance -> "Labeled"},
 {{tt, 0, "time"}, 0, 5}, TrackedSymbols -> True,
 AutorunSequencing -> {1, 2, 3, 4, 6}]

I want to have as if there is a photogate at the center of the simulation that measures the velocity and outputs it in the animation. I would also want to output the corresponding times. In other words, I should have a time data column and a velocity data column but the velocity is just measured twice: once before it collides and once after it collides.

I tried the With function but it didn't work.

Here is my attempt:

Manipulate[
 With[{
   dist = 5;
   
   posA = -dist;
   posB = dist;
   vA2 = -((-mA vA1 + ee mB vA1 - mB vB1 - ee mB vB1)/(mA + mB)),
   vB2 = -((-mA vA1 - ee mA vA1 + ee mA vB1 - mB vB1)/(mA + mB))},
  
  (* vA1*t-dist+mA/10=vB1*t+dist+mB/10, (vA1-vB1)*t=2*dist+mB/10 *)
  tImpact = (20 dist - mA - mB)/(10 (vA1 - vB1));
  
  posImpactA = posA + vA1*tImpact;
  posImpactB = posB + vB1*tImpact;
  
  If[tt <= tImpact, posA2 = vA1*tt + posA, 
   posA2 = posImpactA + vA2*(tt - tImpact)];
  If[tt <= tImpact, posB2 = vB1*tt + posB, 
   posB2 = posImpactB + vB2*(tt - tImpact)];
  
  If[tt <= tImpact, 
   vecB = Graphics[{RGBColor[0., 0.18, 0.41], Thick, 
      Arrow[{{posB2, 0}, {posB2 + vB1, 0}}]}], 
   vecB = Graphics[{Darker[RGBColor[0.28, 0.39, 0.]], 
      Arrow[{{posB2, 0}, {posB2 + vB2, 0}}]}]];
  If[tt <= tImpact, 
   vecA = Graphics[{RGBColor[0., 0.18, 0.41], Thick, 
      Arrow[{{posA2, 0}, {posA2 + vA1, 0}}]}], 
   vecA = 
    Graphics[{Darker[RGBColor[0.28, 0.39, 0.]], Thick, 
      Arrow[{{posA2, 0}, {posA2 + vA2, 0}}]}]];
  
  parA = Graphics[{Darker[Gray], Disk[{posA2, 0}, mA/10]}];
  parB = Graphics[{Darker[Gray], Disk[{posB2, 0}, mB/10]}];
  road = Graphics[{Lighter[Gray], Line[{{-10, 0}, {10, 0}}]}]];
 Show[{road, parA, parB, vecA, vecB}, 
  PlotRange -> {{-10, 10}, {-1, 5}}, ImageSize -> 450],
 {{mA, 1, "mass of A"}, 1, 10, Appearance -> "Labeled"},
 {{mB, 10, "mass of B"}, 1, 10, Appearance -> "Labeled"},
 {{vA1, 5, "initial velocity of A"}, 1, 5, Appearance -> "Labeled"},
 {{vB1, 0, "initial velocity of B"}, -5, 0, Appearance -> "Labeled"},
 {{ee, 0.5, "coefficient of elasticity"}, 0, 1, 
  Appearance -> "Labeled"},
 {{tt, 0, "time"}, 0, 5}, TrackedSymbols -> True,
 AutorunSequencing -> {1, 2, 3, 4, 6}]

Any help would be so appreciated!

$\endgroup$
0
1
$\begingroup$

As the velocities after collision stay constant, it is good enough to give the constant values. The velocities before collision are given as input. It makes therefore no sense to give a table with velocities at different times.

Manipulate[dist = 5;
 posA = -dist;
 posB = dist;
 vA2 = -((-mA vA1 + ee mB vA1 - mB vB1 - ee mB vB1)/(mA + mB));
 vB2 = -((-mA vA1 - ee mA vA1 + ee mA vB1 - mB vB1)/(mA + mB));
 (*vA1*t-dist+mA/10=vB1*t+dist+mB/10,(vA1-vB1)*t=2*dist+mB/10*)
 tImpact = (20 dist - mA - mB)/(10 (vA1 - vB1));
 posImpactA = posA + vA1*tImpact;
 posImpactB = posB + vB1*tImpact;
 If[tt <= tImpact, posA2 = vA1*tt + posA, 
  posA2 = posImpactA + vA2*(tt - tImpact)];
 If[tt <= tImpact, posB2 = vB1*tt + posB, 
  posB2 = posImpactB + vB2*(tt - tImpact)];
 If[tt <= tImpact, 
  vecB = Graphics[{Red, Thick, 
     Arrow[{{posB2, 0}, {posB2 + vB1, 0}}]}], 
  vecB = Graphics[{Darker[Green], 
     Arrow[{{posB2, 0}, {posB2 + vB2, 0}}]}]];
 If[tt <= tImpact, 
  vecA = Graphics[{Red, Thick, 
     Arrow[{{posA2, 0}, {posA2 + vA1, 0}}]}], 
  vecA = Graphics[{Darker[Green], Thick, 
     Arrow[{{posA2, 0}, {posA2 + vA2, 0}}]}]];
 parA = Graphics[{Darker[Gray], Disk[{posA2, 0}, mA/10]}];
 parB = Graphics[{Darker[Gray], Disk[{posB2, 0}, mB/10]}];
 road = Graphics[{Lighter[Gray], Line[{{-10, 0}, {10, 0}}]}];
 Column[{
   Show[{road, parA, parB, vecA, vecB}, 
    PlotRange -> {{-10, 10}, {-1, 5}}, ImageSize -> 450]
   , StringForm["Velocities after collision. A: ``, B: ``", vA2, vB2]}]
 , {{mA, 5, "mass of A"}, 1, 10, 
  Appearance -> "Labeled"}, {{mB, 10, "mass of B"}, 1, 10, 
  Appearance -> "Labeled"}, {{vA1, 5, "initial velocity of A"}, 1, 5, 
  Appearance -> "Labeled"}, {{vB1, -5, "initial velocity of B"}, -5, 
  0, Appearance -> "Labeled"}, {{ee, 0.5, 
   "coefficient of elasticity"}, 0, 1, 
  Appearance -> "Labeled"}, {{tt, 0, "time"}, 0, 5}, 
 TrackedSymbols -> True, AutorunSequencing -> {1, 2, 3, 4, 6}]

enter image description here

$\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.