Manipulate[
(*by Nasser M. Abbasi, June,17,2014*)
tick;
Module[{y, data},
(*solution of eq=m y''[t]+c y'[t]+k y[t]\[Equal] -m g;*)
y = Chop[1/(2 k Sqrt[c^2 - 4 k m])
g m (-c E^(((-c - Sqrt[c^2 - 4 k m]) t)/(2 m)) + c E^(((-c + Sqrt[c^2 - 4 k m]) t)/(2 m)) - 2 Sqrt[c^2 - 4 k m] +
E^(((-c - Sqrt[c^2 - 4 k m]) t)/(2 m)) Sqrt[c^2 - 4 k m] + E^(((-c + Sqrt[c^2 - 4 k m]) t)/(2 m)) Sqrt[c^2 - 4 k m])];
If[y < -7,
If[wasHit,
wasHit = False;
y = -6.95,
wasHit = True;
y = -7]
];
data = rugo[-3, 0, -3, L0 + y - massThickness];
If[runningState == "RUNNING",
t = t + delta;
If[t > 999.9, t = 0];
tick = Not[tick]
];
Grid[{
{ Row[{"Time ", padIt2[t, {5, 2}]}]},
{
Graphics[
{
{EdgeForm[Black], LightGray, Rectangle[{-4, L0 + y - massThickness}, {4, L0 + y + massThickness}]},
Line[{{2, 0}, {2, 1}, {2.5, 1}, {2.5, 1.5}}],
Line[{{2, 1}, {1.5, 1}, {1.5, 1.5}}],
Line[{{1.6, 1.2}, {2.4, 1.2}}],
Line[{{1.6, 1.3}, {2.4, 1.3}}],
Line[{{2, 1.3}, {2, L0 + y - massThickness}}],
{Thick, Line[{{-6, 0}, {6, 0}}]},
Line[data]
}, PlotRange -> {{-5, 5}, {-1, 11}}, Axes -> False, ImageSize -> 300, ImagePadding -> 5
]
}
}
]
],
Grid[{
{"damping",
Manipulator[Dynamic[c, {c = #, t = 0} &], {0, 1, 0.01}, ImageSize -> Small], Style[Dynamic@padIt2[c, {3, 2}], 11]
},
{"stiffness",
Manipulator[Dynamic[k, {k = #, t = 0} &], {1, 100, 0.01}, ImageSize -> Small], Style[Dynamic@padIt2[k, {5, 2}], 11]
},
{"mass",
Manipulator[Dynamic[m, {m = #, t = 0} &], {1, 10, 0.01}, ImageSize -> Small], Style[Dynamic@padIt2[m, {4, 2}], 11]
},
{Text@Style["slow", 11],
Manipulator[Dynamic[delta, {delta = #} &], {0.01, .1, 0.01}, ImageSize -> Small, ContinuousAction -> False],
Text@Style["fast", 11]
},
{Grid[{
{
Button[Style["run", 12], {runningState = "RUNNING"; tick = Not[tick]}, ImageSize -> {55, 35}],
Button[Style["stop", 12], {runningState = "STOP"; t = 0; tick = Not[tick]}, ImageSize -> {55, 35}]
}
}
]
}
}
],
{{wasHit, False}, None},
{{m, 1}, None},
{{k, 10}, None},
{{c, 0}, None},
{{runningState, "STOP"}, None},
{{t, 0}, None},
{{delta, 0.01}, None},
{{tick, True}, None},
TrackedSymbols :> {tick},
Initialization :>
(
g = 9.8; L0 = 10; massThickness = 0.5;
(*definitions used for parameter checking*)
integerStrictPositive = (IntegerQ[#] && # > 0 &);
integerPositive = (IntegerQ[#] && # >= 0 &); numericStrictPositive = (Element[#, Reals] && # > 0 &);
numericPositive = (Element[#, Reals] && # >= 0 &); numericStrictNegative = (Element[#, Reals] && # < 0 &);
numericNegative = (Element[#, Reals] && # <= 0 &);
bool = (Element[#, Booleans] &);
numeric = (Element[#, Reals] &);
integer = (Element[#, Integers] &);
padIt1[v_?numeric, f_List] :=
AccountingForm[Chop[v], f, NumberSigns -> {"-", "+"}, NumberPadding -> {"0", "0"}, SignPadding -> True];
padIt2[v_?numeric, f_List] :=
AccountingForm[Chop[v], f, NumberSigns -> {"", ""}, NumberPadding -> {"0", "0"}, SignPadding -> True];
padIt2[v_?numeric, f_Integer] :=
AccountingForm[Chop[v], f, NumberSigns -> {"", ""}, NumberPadding -> {"0", "0"}, SignPadding -> True];
rugo[xkezd_, ykezd_, xveg_, yveg_] :=
Module[{step = 20, szel = 1(*spring width*), hx, hy, veghossz = 0.3, hossz, dh, i}, {hx = xveg - xkezd;
hy = yveg - ykezd;
hossz = Sqrt[hx^2 + hy^2];
dh = (hossz - 2*veghossz)/step;
{xkezd, ykezd}}~Join~{{xkezd + hx*(dh + veghossz)/hossz, ykezd + hy*(dh + veghossz)/hossz}}~Join~
Table[If[OddQ[i], {xkezd + hx*(i*dh + veghossz)/hossz + hy*szel/hossz,
ykezd + hy*(i*dh + veghossz)/hossz - hx*szel/hossz}, {xkezd + hx*(i*dh + veghossz)/hossz - hy*szel/hossz,
ykezd + hy*(i*dh + veghossz)/hossz + hx*szel/hossz}], {i, 2, (step - 2)}]~
Join~{{xkezd + hx*((step - 1)*dh + veghossz)/hossz, ykezd + hy*((step - 1)*dh + veghossz)/hossz}}~Join~{{xveg, yveg}}];
)
]