2
$\begingroup$

Starting from what is read here, write the following code:

spring[a_, x1_, x2_] := Module[{m, n},
   xvalues = Table[m, {m, x1, x2, (x2 - x1)/1000}];
   yvalues = Table[a Sin[n Pi/2], {n, 0, 100, .1}];
   zvalues = Table[a Cos[n Pi/2], {n, 0, 100, .1}];
   Line[Transpose @ {xvalues, yvalues, zvalues}]];

x[t_] := Cos[t]

xmin = NMinValue[{x[t], 0 <= t <= tmax}, t];
xmax = NMaxValue[{x[t], 0 <= t <= tmax}, t];

Animate[Graphics3D[{
             spring[.2, xmin - .1, x[t]],
             {LightGray, Sphere[{x[t], 0, 0}, .3]}},
             Boxed -> False,
             Lighting -> "Neutral",
             PlotRange -> {{xmin - .1, xmax + .1}, {-1, 1}, {-1, 1}},
             ViewPoint -> Above],
        {t, 0, tmax, .1}]

you get the animation of what's shown in the figure:

enter image description here

I do not regret the result, but it is still far from the beautiful representations here reproduced.

Since I'm not able to use that complicated code for my purpose, I thought I would swell the line of my spring but I would not know how to do the code I posted.

I also had the idea of using Tube instead of Line, but in that case I pound my pc.

Can you give me a hand? Thank you!

$\endgroup$
3
$\begingroup$

Try this:

spring[a_, x1_, x2_] := 
  Module[{m, n}, xvalues = Table[m, {m, x1, x2, (x2 - x1)/1000}];
    yvalues = Table[a Sin[n Pi/2], {n, 0, 100, .1}];
    zvalues = Table[a Cos[n Pi/2], {n, 0, 100, .1}];
    Line[Transpose@{xvalues, yvalues, zvalues}]] /. Line -> Tube;

x[t_] := Cos[t]

xmin = NMinValue[{x[t], 0 <= t <= tmax}, t];
xmax = NMaxValue[{x[t], 0 <= t <= tmax}, t];
tmax = 30;
lst = Table[
   Graphics3D[{spring[.2, xmin - .1, x[t]], {LightGray, 
      Sphere[{x[t], 0, 0}, .3]}}, Boxed -> False, 
    Lighting -> "Neutral", 
    PlotRange -> {{xmin - .1, xmax + .1}, {-1, 1}, {-1, 1}}, 
    ViewPoint -> Above], {t, 0, tmax, .03}];
ListAnimate[lst, AnimationRate -> 150]

Have fun!

$\endgroup$
  • $\begingroup$ Perfect, I solved it. Thank you very much! $\endgroup$ – TeM Aug 18 '17 at 20:50

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.