1
$\begingroup$

I can't animate this code, what's wrong and how can I write omega, theta,...
If I copy XG1 and YG1 directly in animate it works properly but when I use defined function(XG1 and YG1) it can't works.

ClearAll[L, t, V, θ1] 
m1 = 1;
m2 = 1;
I1 = 1;
L1 = 1;
LC1 = L1/2;
R = 0.3;
L2 = 1;

XG1[t_] := +R*Sin[θ1[t]] - LC1*Cos[θ1[t]];
YG1[t_] := -R*Cos[θ1[t]] - LC1*Sin[θ1[t]];



ω1[t_] = D[θ1[t], t];

T1 = 0;

T = 1/2*m1*((D[XG1, t])^2 + (D[YG1, t])^2) + 1/2*I1*ω1^2;
V = m1*g*YG1;

L = T - V;
Eq = D[D[L, θ1'[t]], t] - D[L, θ1[t]] == T1;
ics = {θ1[0] == 0, θ1'[0] == 0};
eqs = Join[{Eq}, ics];

s = First@NDSolve[eqs, {θ1[t], θ1'[t]}, {t, 0, 100}];



Animate[Graphics[{Line[{{0,0},{XG1, YG1}}]}], {t, 0.01, 50}] /. s
$\endgroup$
4
  • 1
    $\begingroup$ You have so many errors before you even get to the animation part. Your Lagrangian is not even function of time. You should evaluate each step one by one and make sure it is correct before you go to the next step. You define w1 as function of time, but you do not use that fact when you reference it. Many other problems like this. $\endgroup$
    – Nasser
    Jan 10, 2017 at 8:35
  • $\begingroup$ To write the greek letters, hit Esc, type Omega, then hit Esc again. For small letter use omega. Alternatively you can go to Palettes -> Special characters. $\endgroup$
    – Kiro
    Jan 10, 2017 at 8:42
  • $\begingroup$ this code work properly when I use +R*Sin[θ1[t]] - LC1*Cos[θ1[t]] instead of XG1 but when I use XG1 and YG1 it doesn't work. I defined XG1 but it can't work in last line!!!! $\endgroup$
    – Farshid
    Jan 10, 2017 at 9:16
  • 1
    $\begingroup$ I doubt it. As mentioned by Nasser, your code is full of errors. Even after correcting those, your Animate command does not produced anything. $\endgroup$
    – zhk
    Jan 10, 2017 at 9:21

1 Answer 1

4
$\begingroup$

Here is some code that is similar to yours, but with a few fixes:

  1. Specify g=1/6
  2. Remove $\omega$ and use $\theta'$ instead
  3. NDSolve[] for $\theta$ and $\theta'$ instead of $\theta[t]$ and $\theta'[t]$
  4. Move /.s inside of Animate[], just after Graphics[]
  5. Add a Rectangle[] to the background of the graphics to prevent zoom changes.
  6. Add arguments [t] to functions XG1 and YG1 inside Graphics[]

Try this:

ClearAll["Global`*"]

m1 = 1; m2 = 1; I1 = 1;
L1 = 1; LC1 = L1/2;
R = 0.3; L2 = 1; g = 1/6;

XG1[t_] := +R*Sin[θ1[t]] - LC1*Cos[θ1[t]]
YG1[t_] := -R*Cos[θ1[t]] - LC1*Sin[θ1[t]]
T1 = 0;
T = 1/2*m1*((D[XG1[t], t])^2 + (D[YG1[t], t])^2) +
   1/2*I1*θ1'[t]^2;
V = m1*g*YG1[t];

L = T - V;
Eq = D[D[L, θ1'[t]], t] - D[L, θ1[t]] == T1;
ics = {θ1[0] == 0, θ1'[0] == 0};
eqs = Join[{Eq}, ics];

s = First@NDSolve[eqs, {θ1, θ1'}, {t, 0, 100}];

Animate[Graphics[{PointSize[1/25],
    Line[{{0, 0}, {XG1[t], YG1[t]}}],
    Point[{XG1[t], YG1[t]}],
    Opacity[1/10], Rectangle[{-1, -1}, {1, 1}]},
   Frame -> True] /. s, {t, 0, 100}]
$\endgroup$
3
  • $\begingroup$ Thanks Louis. Its work exactly my friend. $\endgroup$
    – Farshid
    Jan 10, 2017 at 9:47
  • $\begingroup$ what is difference between teta and teta[t] in NDsolve $\endgroup$
    – Farshid
    Jan 10, 2017 at 10:15
  • $\begingroup$ @Farshid An excellent question! All of the examples in the documentation for NDSolve, indicate we should solve for y and then Plot[y[x], ...]. It is possible to NDSolve for y[x] and then Plot[y[x]/.s, ...], at least in some cases. I do not know why that does not work here. You may find the answer by studying the simplest possible example, or by searching this site for questions about NDSolve. If that doesn't work, please ask this as a new question. $\endgroup$
    – LouisB
    Jan 10, 2017 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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