0
$\begingroup$

In order to reproduce the functions, that I am using in my Animate, you should use this code:

     n = 4;

variables = 
  Table[ToExpression["φ" <> ToString[i]], {i, 1, n}];

xComponents = Table[Cos[variables[[i]][t]], {i, 1, n}];
yComponents = Table[-Sin[variables[[i]][t]], {i, 1, n}];

endsOfRods = 
  Table[{xComponents[[i]], yComponents[[i]]}, {i, 1, n - 1}];

xTotal = Table[
   l (Sum[endsOfRods[[i, 1]], {i, 1, j - 1, 1}] + 
      1/2 endsOfRods[[j, 1]]), {j, 2, n - 1, 1}];
yTotal = Table[
   l (Sum[If[i <= n/2, 
        endsOfRods[[i, 2]], (-1)*endsOfRods[[i, 2]]], {i, 1, j - 1, 
        1}] + If[j > n/2, (-1)*1/2 endsOfRods[[j, 2]], 
       1/2 endsOfRods[[j, 2]]]), {j, 2, n - 1, 1}];

centreOfMass = 
  Table[{xTotal[[i]], yTotal[[i]]}, {i, 1, Length[xTotal], 1}];

velocityCentreOfMass = 
  Table[D[centreOfMass[[i]], t].D[centreOfMass[[i]], t], {i, 1, 
    Length[xTotal], 1}];

kineticEnergy = 
  Sum[If[i == 1 || i == n, 1/2*(1/3 m l^2)*D[variables[[i]][t], t]^2, 
    1/2 m velocityCentreOfMass[[i - 1]] + 
     1/2*(1/12 m l^2)*D[variables[[i]][t], t]^2], {i, 1, n, 1}];

potentialEnergy = 
  Sum[If[i == 1 || i == n, -m g l 1/2 Sin[variables[[i]][t]] , 
     m g yTotal[[i - 1]]], {i, 1, n, 1}](*gravity*)+ 
   Sum[If[i < n/2, 
     1/2 k (variables[[i]][t] - variables[[i + 1]][t])^2, 
     If[i == n/2, 1/2 k (variables[[i]][t] + variables[[i + 1]][t])^2,
       1/2 k (variables[[i + 1]][t] - variables[[i]][t])^2]], {i, 1, 
     n - 1, 1}](*rotational springs*);

lagrange = Simplify[kineticEnergy - potentialEnergy];
F = {0, d D[(-1)*(yTotal[[n/2 - 1]] - l/2 Sin[variables[[(n/2)]][t]]),
      t]};

r = {(xTotal[[n/2 - 1]] + 
     l/2 Cos[variables[[(n/2)]][t]]), (yTotal[[n/2 - 1]] - 
     l/2 Sin[variables[[(n/2)]][t]])};

generalizedForces = 
  Table[Simplify[F.D[r, variables[[i]][t]]], {i, 1, n/2, 1}];
equations = 
  Table[If[i <= n/2, 
    Simplify[
      Rationalize[
       D[D[lagrange, D[variables[[i]][t], t]], t] - 
        D[lagrange, variables[[i]][t]] - generalizedForces[[i]]]] == 
     0, Simplify[
      Rationalize[
       D[D[lagrange, D[variables[[i]][t], t]], t] - 
        D[lagrange, variables[[i]][t]]]] == 0], {i, 1, n - 1, 1}];

AppendTo[equations, 
  Rationalize[(-1)*(yTotal[[(n/2)]] + 
       l/2 Sin[variables[[n/2 + 1]][t]] + 
       l Sin[variables[[n]][t]])] == 0];

m = 229(*kg*);
l = 0.63(*m*);
d = 4460(*Ns/m*);
EE = 2.1*10^(11)(*Pa*);
II = 1.2*10^(-6)(*m^4*);
g = 9.81(*m/s^2*);
k = EE II/(l);

For[i = 1, i <= n, i++, 
  AppendTo[equations, variables[[i]][t] == 0 /. t -> 0]];

Pause[1];

For[i = 1, i <= n, i++, 
  AppendTo[equations, D[variables[[i]][t], t] == 0 /. t -> 0]];

Pause[3];

solution = 
  NDSolve[Rationalize[equations], 
   Table[variables[[i]][t], {i, 1, n, 1}], {t, 0, 5}];

Now If you are lucky, NDSolve will notify there is an error I can't get rid off but at the same time, you should get some result out of it. The error is not the reason i am writing this. The problem is with the Animate

Animate[Grid[{{Show[{ParametricPlot[{(l*Cos[solution[[1, 1, 2]]] + 
          l*Cos[solution[[1, 2, 2]]]), (-l*Sin[solution[[1, 1, 2]]] - 
          l*Sin[solution[[1, 2, 2]]])}, {t, 0, tmax}], 
      Graphics[{Arrow[{{l*n/2, 
           0}, {(l*Cos[solution[[1, 1, 2]]] + 
              l*Cos[solution[[1, 2, 2]]]) /. 
            t -> tmax, (-l*Sin[solution[[1, 1, 2]]] - 
              l*Sin[solution[[1, 2, 2]]]) /. t -> tmax}}]}], 
      Graphics[{PointSize[.018], 
        Point[{(l*Cos[solution[[1, 1, 2]]] + 
             l*Cos[solution[[1, 2, 2]]]) /. 
           t -> tmax, (-l*Sin[solution[[1, 1, 2]]] - 
             l*Sin[solution[[1, 2, 2]]]) /. t -> tmax}]}]}, 
     PlotRange -> {{1.2597, l*n/2}, {0, -.025}}, AspectRatio -> 1, 
     AxesOrigin -> {0, 0}, Frame -> True, ImageSize -> Large, 
     GridLines -> Automatic], 
    Show[{ParametricPlot[{(l*Cos[solution[[1, 1, 2]]] + 
          l*Cos[solution[[1, 2, 2]]]), (-l*Sin[solution[[1, 1, 2]]] - 
          l*Sin[solution[[1, 2, 2]]])}, {t, 0, tmax}], 
      Graphics[{Arrow[{{l*n/2, 
           0}, {(l*Cos[solution[[1, 1, 2]]] + 
              l*Cos[solution[[1, 2, 2]]]) /. 

            t -> tmax, (-l*Sin[solution[[1, 1, 2]]] - 
              l*Sin[solution[[1, 2, 2]]]) /. t -> tmax}}]}], 
      Graphics[{PointSize[.018], 
        Point[{(l*Cos[solution[[1, 1, 2]]] + 
             l*Cos[solution[[1, 2, 2]]]) /. 
           t -> tmax, (-l*Sin[solution[[1, 1, 2]]] - 
             l*Sin[solution[[1, 2, 2]]]) /. t -> tmax}]}]}, 
     PlotRange -> {{1.25985, 1.25990}, {-0.015, -.019}}, 
     AspectRatio -> 1, AxesOrigin -> {0, 0}, Frame -> True, 
     ImageSize -> Large, GridLines -> Automatic]}}], {tmax, 0, 3}, 
 AnimationRate -> .05]

Now let's focus on right plot. After some time, you can clearly see that the blue lines from the ParametricPlot[] move - vibrate as if they were atoms. :) I honestly don't understand why this happens nor do I know how to get rid of that. It looks funny and wrong at the same time.

$\endgroup$

closed as off-topic by m_goldberg, MarcoB, RunnyKine, user9660, WReach Mar 24 '16 at 2:43

  • The question does not concern the technical computing software Mathematica by Wolfram Research. Please see the help center to find out about the topics that can be asked here.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ A MINIMAL working (or not working) example is much better $\endgroup$ – Dr. belisarius Mar 22 '16 at 23:30
  • 4
    $\begingroup$ I'm voting to close this question as off-topic because it is too localized; i.e, it applies only to the local situation and needs of its poster and answers will not benefit others. $\endgroup$ – m_goldberg Mar 23 '16 at 2:26
4
$\begingroup$

Let's look at your solutions.

φ1F, φ2F, φ3F, φ4F} = solution[[1, All, 2, 0]];
Plot[{φ1F[t], φ2F[t], φ3F[t], φ4F[t]}, {t, 0, 5}, ImageSize -> Large]

plot

See all that jitter in the solutions? I believe that is causing sampling jitter in the parametric plots of you Animate expression.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.