2
$\begingroup$

I have a wolfram demonstration project with the following code:

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;
 c1 = (vA2 - vA1)/2 Tanh[ (10 x)/dt - (10 tImpact)/dt] + (vA2 + vA1)/2;
 c2 = (vB2 - vB1)/2 Tanh[(10 x)/dt - (10 tImpact)/dt] + (vB1 + vB2)/2;
 
 posA2 = If[tt <= tImpact, vA1 tt + posA, 
   posImpactA + vA2*(tt - tImpact)];
 posB2 = If[tt <= tImpact, vB1 tt + posB, 
   posImpactB + vB2*(tt - tImpact)];
 
 vecB = If[
   tt <= tImpact,
   {Red, Thick, Arrow[{{posB2, 0}, {posB2 + vB1, 0}}]},
   {Darker[Green], Thick, Arrow[{{posB2, 0}, {posB2 + vB2, 0}}]}
   ];
 vecA = If[
   tt <= tImpact,
   {Red, Thick, Arrow[{{posA2, 0}, {posA2 + vA1, 0}}]},
   {Darker[Green], Thick, Arrow[{{posA2, 0}, {posA2 + vA2, 0}}]}
   ];
 If[sh,
  Show[
   Plot[Tooltip[{vA1, vB1}], {tt, 0, tImpact - dt/2}, 
    PlotRange -> {{0, 5}, {-10, 10}}, 
    PlotStyle -> {{AbsoluteThickness[3]}, {AbsoluteThickness[3]}}, 
    PlotLabel -> "velocity versus time", 
    AxesLabel -> {t , Subscript[v, x]}],
   Plot[Tooltip[{vA2, vB2}], {tt, tImpact + dt/2, 5}, 
    PlotStyle -> {{AbsoluteThickness[3]}, {AbsoluteThickness[3]}}],
   Plot[{c1, c2}, {x, tImpact - dt/2, tImpact + dt/2}, 
    PlotStyle -> {{AbsoluteThickness[3]}, {AbsoluteThickness[3]}}],
   ImageSize -> {550, 275}], 
  timeSeries = 
   Table[{vA1, vB1, vA2, vB2, tt}, {tt, 0.1, tImpact + dt/2, 5}], 
  Graphics[{
    (* road *) {Lighter[Gray], Arrow[{{-10, 0}, {10, 0}}]},
    (* disk for A *) {RGBColor[0.368417, 0.506779, 0.709798], 
     EdgeForm[Black], Disk[{posA2, 0}, mA/10]},
    (* disk for B *) {RGBColor[0.880722, 0.611041, 0.142051], 
     EdgeForm[Black], Disk[{posB2, 0}, mB/10]}, vecA, vecB}, 
   PlotRange -> {{-10, 10}, {-6.6, 6.6}}, ImageSize -> {550, 275}]
  ],
 Grid[{
   {
    Spacer[40],
    Control[{{mA, 1, "mass of A"}, 1, 10, 0.1, 
      Appearance -> "Labeled", ImageSize -> Small}], 
    Control[{{vA1, 5, "initial velocity of A"}, 1, 5, 0.1, 
      Appearance -> "Labeled", ImageSize -> Small}]
    },
   {
    Spacer[40],
    Control[{{mB, 10, "mass of B"}, 1, 10, 0.1, 
      Appearance -> "Labeled", ImageSize -> Small}], 
    Control[{{vB1, 0, "initial velocity of B"}, -5, 0, 
      Appearance -> "Labeled", ImageSize -> Small}]
    },
   {
    Spacer[40],
    Control[{{tt, 0, "time"}, 0, 5, 0.1, Appearance -> "Labeled", 
      ImageSize -> Small}], 
    Control[{{ee, 1, "coefficient of elasticity"}, 0, 1, 0.1, 
      Appearance -> "Labeled", ImageSize -> Small}]
    },
   {
    Spacer[40],
    Control[{{sh, False, "velocity plot"}, {True, False}, 
      ImageSize -> Small}], 
    Control[{{dt, 0.1, "collision time"}, 0.01, 1.5, 
      Appearance -> "Labeled", ImageSize -> Small}]
    }
   },
  Alignment -> Left],
 TrackedSymbols -> True,
 AutorunSequencing -> {1, 2, 3, 4, 6, 7}]
Export["file.txt", timeSeries, "Table"]

I modified it so that I can get a Table of the initial and final velocities of both masses over time using the timeSeries and Table function. The problem is when I open the file I get:

5   0   -45/11  10/11   1.83
5   0   -45/11  10/11   2.83
5   0   -45/11  10/11   3.83
5   0   -45/11  10/11   4.83

How can I modify it so that I can get the velocities at smaller time intervals, for example 0.1 seconds instead of 1 second as shown in the table that I generated.

EDIT

I am as can be seen from the graph generated from the demonstration trying to find the velocities when it changes during the collision:

enter image description here

Thank you.

*EDIT Based on the answer below I get a table that looks like:

7/4 {4.977521607666957, 0.0022478392333043495}
351/200 {4.93915590068832, 0.006084409931168044}
44/25   {4.836489000344622, 0.016351099965537796}
353/200 {4.5688556983857564, 0.043114430161424355}
177/100 {3.916337072526204, 0.10836629274737963}
71/40   {2.555077987545499, 0.24449220124545012}
89/50   {0.45454545454545453, 0.45454545454545453}
357/200 {-1.6459870784545898, 0.6645987078454589}
179/100 {-3.007246163435295, 0.8007246163435294}
359/200 {-3.6597647892948477, 0.8659764789294847}
9/5 {-3.9273980912537136, 0.8927398091253713}
361/200 {-4.030064991597412, 0.9030064991597411}
181/100 {-4.068430698576049, 0.9068430698576047}
363/200 {-4.082626807323631, 0.908262680732363}
91/50   {-4.087860453359396, 0.9087860453359395}
73/40   {-4.089787322036489, 0.9089787322036489}

Are the terms in the brackets the final velocities of the mass A and mass B and outside the brackets is the time? How can I insert the initial velocities as well? Also, how can I get rid of the brackets so that I can use this data automatically.

$\endgroup$
2
  • $\begingroup$ Please correct the errors in your code. $\endgroup$ – Daniel Huber Mar 11 at 9:48
  • $\begingroup$ My code isn't working properly. My code is my (failed) attempt to modify the original code (posted first) to get the velocities. But I need help with it. I added an extra explanation of what I am looking for under ***EDIT**** in my question. I would appreciate any help $\endgroup$ – usernew Mar 11 at 16:15
1
$\begingroup$

I inserted some code, that will calculate 100 time steps between t=0..5. The output is available outside the Manipulate in variable velocities. It corresponds to the current setting. If you want more resolution, feel free to change the time step in the Table command.

The first number in a row is the time, The next two, the initial and the last to the final velocities.

Here is the inserted code

....

vecA = If[
   tt <= tImpact, {Red, Thick, 
    Arrow[{{posA2, 0}, {posA2 + vA1, 0}}]}, {Darker[Green], Thick, 
    Arrow[{{posA2, 0}, {posA2 + vA2, 0}}]}];

vel[t_] = Piecewise[{
    {{vA1, vB1}, 0 <= t < tImpact - dt/2},
    {{c1, c2} /. x -> t, tImpact - dt/2 <= t < tImpact + dt/2},
    {{vA2, vB2}, tImpact - dt/2 <= t <= 5},
    {0, True}
    }];
velocities = 
  Table[{t, vA1, vB1, Sequence @@ vel[t]}//N, {t, 0, 5, 5/100}];    


If[sh, Show[Plot[Tooltip[{vA1, vB1}], {tt, 0, tImpact - dt/2},

....
$\endgroup$
4
  • $\begingroup$ Thank you, I had a couple more questions under my last edit in my question to clarify your answer. $\endgroup$ – usernew Mar 11 at 18:41
  • $\begingroup$ I added the changes in my answer. $\endgroup$ – Daniel Huber Mar 11 at 19:47
  • $\begingroup$ Thank you! One last thing: Can we have the outputs in decimals instead of fractions. $\endgroup$ – usernew Mar 11 at 19:50
  • $\begingroup$ I see it now. I wasn't sure how to insert N. But I got it now. Thanks for the updates and help!! $\endgroup$ – usernew Mar 11 at 19:58

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.