# Plotting a function variable vs. an explicit function

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!

-

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]


-
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]
]


-
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