Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question

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

share|improve this answer
    
You don't really need to include t as it isn't localized by Animate[ ]- I would do the same nevertheless – Dr. belisarius Jan 21 at 6:25

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

share|improve this answer
    
Both of these are very helpful, thanks. – Bruce Jan 22 at 4:08
    
For the future, can you help me understand why I must do this? – Bruce Jan 22 at 4:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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