Message Boards Message Boards

Is there a way to precalculate an animation and then run easily with no lag

Posted 9 years ago

I have an animation of a triple pendulum on a moving base, however even after trying to change the whole coding to make it faster by using module and all it is still pretty bad as I need it to be smooth because I will be adding more physics based object. So, Please, I am pretty new so any help is appreciated, heres the code i'll also add the file:

nd2[m1_, m2_, m3_, m4_, l_, k_, p_, g_] := 
 Module[{time = 100, x2, x3, x4, y2, y3, y4, v1, v2, v3, v4, T, U, L, 
   equ1, equ2, equ3, equ4, iniConds1, iniConds2, iniConds3, iniConds4},
  y1 = 0;
  x2[t_] = x1[t] + l Sin[\[Theta][t]];
  y2[t_] = -l Cos[\[Theta][t]];
  x3[t_] = x1[t] + l Sin[\[Theta][t]] + k Sin[\[Theta]1[t]];
  y3[t_] = -l Cos[\[Theta][t]] - k Cos[\[Theta]1[t]];
  x4[t_] = 
   x1[t] + l Sin[\[Theta][t]] + k Sin[\[Theta]1[t]] + 
    p Sin[\[Theta]2[t]];
  y4[t_] = -l Cos[\[Theta][t]] - k Cos[\[Theta]1[t]] - 
    p Cos[\[Theta]2[t]];
  v1[t_] = \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(x1[t]\)\);
  v2[t_] = Sqrt[(\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(x2[t]\)\))^2 + (\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(y2[t]\)\))^2];
  v3[t_] = Sqrt[(\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(x3[t]\)\))^2 + (\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(y3[t]\)\))^2];
  v4[t_] = Sqrt[(\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(x4[t]\)\))^2 + (\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(y4[t]\)\))^2];
  T[t_] = 
   1/2 m1 v1[t]^2 + 1/2 m2 v2[t]^2 + 1/2 m3 v3[t]^2 + 1/2 m4 v4[t]^2;
  U[t_] = m2 g y2[t] + m3 g y3[t] + m4 g y4[t];
  L[t_] = T[t] - U[t];
  equ1 = \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(
\*SubscriptBox[\(\[PartialD]\), \(\(x1'\)[t]\)]L[t]\)\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(x1[t]\)]\(L[t]\)\) == 0;
  equ2 = \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(
\*SubscriptBox[\(\[PartialD]\), \(\(\[Theta]'\)[t]\)]L[t]\)\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(\[Theta][t]\)]\(L[t]\)\) == 0;
  equ3 = \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(
\*SubscriptBox[\(\[PartialD]\), \(\(\[Theta]1'\)[t]\)]L[t]\)\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(\[Theta]1[t]\)]\(L[t]\)\) == 0;
  equ4 = \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(
\*SubscriptBox[\(\[PartialD]\), \(\(\[Theta]2'\)[t]\)]L[t]\)\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(\[Theta]2[t]\)]\(L[t]\)\) == 0;
  iniConds1 = {x1'[0] == 0.5, x1[0] == -4};
  iniConds2 = {\[Theta]'[0] == 0, \[Theta][0] == 3.14/3};
  iniConds3 = {\[Theta]1'[0] == 0, \[Theta]1[0] == 3.14/3};
  iniConds4 = {\[Theta]2'[0] == 0, \[Theta]2[0] == 3.14/2};
  NDSolve[
   Flatten[{equ1, equ2, equ3, equ4, iniConds1, iniConds2, iniConds3, 
     iniConds4}], {x1, \[Theta]1, \[Theta], \[Theta]2}, {t, 0, 100}]]

Animate[Module[{nd = nd2[m1, m2, m3, m4, l, k, p, g], x2, x3, y2, y3, 
   x4, y4},
  x2[t_] := x2[t] = x1[t] + l Sin[\[Theta][t]];
  x3[t_] := x3[t] = x1[t] + l Sin[\[Theta][t]] + k Sin[\[Theta]1[t]];
  y2[t_] := y2[t] = -l Cos[\[Theta][t]];
  y3[t_] := y3[t] = -l Cos[\[Theta][t]] - k Cos[\[Theta]1[t]];
  x4[t_] := 
   x4[t] = x1[t] + l Sin[\[Theta][t]] + k Sin[\[Theta]1[t]] + 
     p Sin[\[Theta]2[t]];
  y4[t_] := -l Cos[\[Theta][t]] - k Cos[\[Theta]1[t]] - 
    p Cos[\[Theta]2[t]];
  Show[
   Graphics[{
     Evaluate[Red, 
      Rectangle[{x1[t] - 0.3, 0}, {x1[t] + 0.3, 0.5} ] /. nd],
     Evaluate[Blue, PointSize[0.01 m1], Opacity[1], 
      Point[{x1[t], 0} /. nd]],
     Evaluate[Red, PointSize[0.01 m2], 
      Point[{x2[t], y2[t]} /. nd /. x2[t] -> x2[t] /. y2[t] -> y2[t]]],
     Evaluate[Yellow, PointSize[0.01 m3], 
      Point[{x3[t], y3[t]} /. nd /. x3[t] -> x3[t] /. y3[t] -> y3[t]]],
     Evaluate[Green, PointSize[0.01 m4], 
      Point[{x4[t], y4[t]} /. nd /. x4[t] -> x4[t] /. y4[t] -> y4[t]]],
     Evaluate[Black, 
      Line[{{x1[t], 0}, {x2[t], y2[t]}, {x3[t], y3[t]}, {x4[t], 
            y4[t]}} /. nd /. x4[t] -> x4[t] /. y4[t] -> y4[t]]]},
    PlotRange -> pr,
    Axes -> {True, True}],
   ParametricPlot[{Evaluate[{x3[Time], y3[Time]} /. nd], 
     Evaluate[{x2[Time], y2[Time]} /. nd], 
     Evaluate[{x4[Time], y4[Time]} /. nd]}, {Time, 0.0001, t},
    PlotStyle -> {Orange, Opacity[0.7]},
    PerformanceGoal -> "Speed",
    PlotPoints -> 200]]],
 {{t, 1, "Time"}, 1, 10, Appearance -> "Open"}, {{m1, 1, "Mass 1"}, 1,
   10, Appearance -> "Open"}, {{m2, 2, "Mass 2"}, 1, 10, 
  Appearance -> "Open"}, {{m3, 3, "Mass 3"}, 1, 10, 
  Appearance -> "Open"}, {{m4, 4, "Mass 4"}, 1, 10, 
  Appearance -> "Open"}, {{l, 1, "Length 1"}, 1, 10, 
  Appearance -> "Open"}, {{k, 1, "Length 2"}, 1, 10, 
  Appearance -> "Open"}, {{p, 1, "Length 3"}, 1, 10, 
  Appearance -> "Open"}, {{g, 1, "Gravitational Constant"}, 1, 10, 
  Appearance -> "Open"}, {{pr, 10, "Plot Range"}, 1, 100},
 DefaultDuration -> 100,
 AppearanceElements -> All,
 AnimationRepetitions -> 1,
 AnimationRunning -> False, ControlPlacement -> Top]
Attachments:
POSTED BY: Hamid Aghasi
POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract