3
$\begingroup$

Can someone tell me why this doesn't plot/animate?

x = Xm Cos[ω t + ϕ];
v = -ω Xm Sin[ω t + ϕ]; 
a = -ω^2 Xm Cos[ω t + ϕ];
Animate[Plot[{x, v, a}, {t, -12.5, 12}, 
  PlotStyle -> {Blue, Red, DarkGreen}], 
 {{ω, 1.57}, 0, 6.28}, {{ϕ, 0}, 0, 10}, {{Xm, 1}, 0, 3}, 
 AnimationRunning -> False]

If I move the definitions of the functions into the plot parameters instead of the x,v, and a variables, it will then work. Why can't I use the variables?

Thanks!

$\endgroup$

2 Answers 2

4
$\begingroup$

Make the definition as function of all the variables.

x[ω_, ϕ_, t_, Xm_] := Xm*Cos[ω t + ϕ]; 
v[ω_, ϕ_, t_, Xm_] := -ω*Xm*Sin[ω t + ϕ]; 
a[ω_, ϕ_, t_, Xm_] := -ω^2*Xm*Cos[ω t + ϕ];

Animate[
Plot[{x[ω, ϕ, t, Xm], v[ω, ϕ, t, Xm], a[ω, ϕ,t, Xm]}, {t, -12.5, 12}, 
PlotStyle -> {Blue, Red, Green}], 
{{ω, 1.57}, 0, 6.28}, {{ϕ, 0}, 0, 10}, {{Xm, 1}, 0, 3}, 
 AnimationRunning -> False]

enter image description here

$\endgroup$
1
  • $\begingroup$ You don't really need to include t as it isn't localized by Animate[ ]- I would do the same nevertheless $\endgroup$ Jan 21, 2016 at 6:25
3
$\begingroup$

Another option would be to use the With scoping construct

With[{
  x = Xm Cos[ω t + ϕ],
  v = -ω Xm Sin[ω t + ϕ],
  a = -ω^2 Xm Cos[ω t + ϕ]
  },
 Animate[Plot[{x, v, a}, {t, -12.5, 12},
   PlotStyle -> {Blue, Red, DarkGreen}],
  {{ω, 1.57}, 0, 6.28}, {{ϕ, 0}, 0, 10}, {{Xm, 1}, 0, 3},
  AnimationRunning -> False]
 ]

enter image description here

$\endgroup$
2
  • $\begingroup$ Both of these are very helpful, thanks. $\endgroup$
    – Bruce
    Jan 22, 2016 at 4:08
  • $\begingroup$ For the future, can you help me understand why I must do this? $\endgroup$
    – Bruce
    Jan 22, 2016 at 4:09

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.