Message Boards Message Boards

Animate these 2D plots?

Posted 5 years ago

Consider the following code:

z1 = {-R*W*Sin[W*t] + l'[t]*Sin[\[Phi][t]] + 
            l[t]*(\[Phi]'[t])*Cos[\[Phi][t]], 
           R*W*Cos[W*t] - l'[t]*Cos[\[Phi][t]] + 
            l[t]*(\[Phi]'[t])*Sin[\[Phi][t]]};

        V = m*g*(R*Sin[W*t] - l[t]*Cos[\[Phi][t]]) + 1/2*k*(l[t] - l0)^2;
        T = 1/2*m*z1.z1;
        Lagrange = T - V;
        eqs = D[D[Lagrange, \[Phi]'[t]], t] - D[Lagrange, \[Phi][t]];
        eqs2 = D[D[Lagrange, l'[t]], t] - D[Lagrange, l[t]];

        g = 9.7; m = 1; l0 = 1; k = 1000; R = 2; W = Pi/2;
        sol = NDSolveValue[{eqs == 0, eqs2 == 0, l[0] == l0, l'[0] == 0, 
           Derivative[1][\[Phi]][0] == 0, \[Phi][0] == 0}, {l[t], \[Phi][
            t]}, {t, 0, 20}]

        {Plot[sol.{1, 0}, {t, 0, 5}, AxesLabel -> {"t", "l"}], 
         Plot[sol.{0, 1}, {t, 0, 5}, AxesLabel -> {"t", "\[Phi]"}]}
POSTED BY: Ricardo Waste
8 Replies

Try

Plot[Evaluate[#[[2]] & /@ Flatten[sol] /. t -> tt], {tt, 0, 5},  PlotStyle -> {Red, Blue}]
POSTED BY: Hans Dolhaine
Posted 5 years ago

I mean like this double-pendulum animation here.How can I make like this for my equation?

https://mathematica.stackexchange.com/questions/48059/animation-of-double-pendulum

POSTED BY: Ricardo Waste

You could try this, I hope it helps.

z1 = {-R*W*Sin[W*t] + l'[t]*Sin[\[Phi][t]] + 
l[t]*(\[Phi]'[t])*Cos[\[Phi][t]], 
R*W*Cos[W*t] - l'[t]*Cos[\[Phi][t]] + 
l[t]*(\[Phi]'[t])*Sin[\[Phi][t]]};

V = m*g*(R*Sin[W*t] - l[t]*Cos[\[Phi][t]]) + 1/2*k*(l[t] - l0)^2;
T = 1/2*m*z1.z1;
Lagrange = T - V;
eqs = D[D[Lagrange, \[Phi]'[t]], t] - D[Lagrange, \[Phi][t]];
eqs2 = D[D[Lagrange, l'[t]], t] - D[Lagrange, l[t]];

g = 9.7; m = 1; l0 = 1; k = 1000; R = 2; W = Pi/2;
sol = NDSolve[{eqs == 0, eqs2 == 0, l[0] == l0, l'[0] == 0, 
Derivative[1][\[Phi]][0] == 0, \[Phi][0] == 0}, {l[t], \[Phi][
t]}, {t, 0, 20}];

z1a = z1 /. Flatten[sol /. a_[t] -> a];
Animate[
Graphics[{Red, PointSize[.05], Point[z1a /. t -> tt]}, Axes -> True, 
PlotRange -> {{-4, 4}, {-4, 4}}],
{tt, 0, 20}]
POSTED BY: Hans Dolhaine

Or did you mean something like this?

ParametricPlot[z1a /. t -> tt, {tt, 0, 3}, Axes -> True,  PlotRange -> {{-10, 10}, {-10, 10}}]
POSTED BY: Hans Dolhaine
Posted 5 years ago

Yes it is works, but there is a disc and the spring pendulum rotating at above the disc. How can I show the disc in this code?

POSTED BY: Ricardo Waste

Dear Ă–mer,

I don't know what you really want to do, what the physical situation is you want to describe and if your equations are correct.

Anyhow, what about this? Is it this what you want to see?

z1 = {-R*W*Sin[W*t] + l'[t]*Sin[\[Phi][t]] + 
    l[t]*(\[Phi]'[t])*Cos[\[Phi][t]], 
   R*W*Cos[W*t] - l'[t]*Cos[\[Phi][t]] + 
    l[t]*(\[Phi]'[t])*Sin[\[Phi][t]]};

V = m*g*(R*Sin[W*t] - l[t]*Cos[\[Phi][t]]) + 1/2*k*(l[t] - l0)^2;
T = 1/2*m*z1.z1;
Lagrange = T - V;
eqs = D[D[Lagrange, \[Phi]'[t]], t] - D[Lagrange, \[Phi][t]];
eqs2 = D[D[Lagrange, l'[t]], t] - D[Lagrange, l[t]];

g = 9.7; m = 1; l0 = 1; k = 1000; R = 2; W = Pi/2;
sol = NDSolve[{eqs == 0, eqs2 == 0, l[0] == l0, l'[0] == 0, 
    Derivative[1][\[Phi]][0] == 0, \[Phi][0] == 0}, {l[t], \[Phi][
     t]}, {t, 0, 20}];

p1 = R {Cos[W t], Sin[W t]};
z1a = z1 /. Flatten[sol /. a_[t] -> a];
Animate[Graphics[{Circle[{0, 0}, R], PointSize[.05], Black, 
   Point[p1 /. t -> tt], Red, Point[z1a /. t -> tt], Blue, 
   Line[{p1, z1a} /. t -> tt]}, Axes -> True, 
  PlotRange -> {{-4, 4}, {-4, 4}}], {tt, 0, 20}]
POSTED BY: Hans Dolhaine

Is it this what you want to see?

I am afraid that no. you wrote that z1 is the derivative of your position-vector. So

xx = \[Integral]z1 \[DifferentialD]t

and

p1 = R {Cos[W t], Sin[W t]};
xxa = xx /. Flatten[sol /. a_[t] -> a];
Animate[Graphics[{Circle[{0, 0}, R], PointSize[.05], Black, 
   Point[p1 /. t -> tt], Red, Point[xxa /. t -> tt], Blue, 
   Line[{p1, xxa} /. t -> tt]}, Axes -> True, 
  PlotRange -> {{-4, 4}, {-4, 4}}], {tt, 0, 20}]

is perhaps more what you want to see.

POSTED BY: Hans Dolhaine
Posted 5 years ago

Thanks for reply.It was helpful.

POSTED BY: Ricardo Waste
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