I would like to display the positions of a collection of particles as a function of time (animated) and color code them based on their velocities. For example, consider 10 particles moving in a 2D harmonic oscillator:
velocity = Flatten[(Sqrt[x'[t]^2 + y'[t]^2] /. position)];
Num = 10;
position0 =
Table[{RandomReal[{-5, 5}], RandomReal[{-5, 5}]}, {i, 1, Num}];
ωx = 1; ωy = 1.07;
position = Table[
NDSolve[{x''[t] == -ωx^2 x[t], x[0] == position0[[i, 1]], x'[0] == 0,
y''[t] == -ωy^2 y[t], y[0] == position0[[i, 2]],
y'[0] == 0}, {x, y}, {t, 0, 10}], {i, 1, Num}];
velocity = Flatten[(Sqrt[x'[t]^2 + y'[t]^2] /. position)];
I can then plot a single particle's trajectory colored by its velocity:
ParametricPlot[Evaluate[{x[t], y[t]} /. position[[2]]], {t, 0, 10},
ColorFunction ->
Function[{x, y, t},
ColorData[{"ThermometerColors", {0, 5}}][velocity[[2]]]],
ColorFunctionScaling -> False
]
But what I would like to do is plot the points using ListPlot, and color code each point by its velocity as a function of time. I've started:
Animate[
ListPlot[Table[{x[t], y[t]} /. position[[i, 1]], {i, 1, Num}],
PlotRange -> {{-5, 5}, {-5, 5}}, Joined -> True,
ColorFunction ->
Function[{i, x, y, t},
ColorData[{"ThermometerColors", {0, 5}}][velocity]]],
ColorFunctionScaling -> False] /. Line -> Point,
{t, 0, 10}, AnimationRunning -> False]
I get the animation right but the colors do not appear -- I'm sure I'm not addressing the ColorFunction correctly but I'm not sure how to get it to index by point rather than by one of the continuous variables (eg. x or y).
Comments
Post a Comment