4
$\begingroup$

I am new to Mathematica. Try to draw a point moving with time. Like at t=0, a point is at p1(coordinates are random numbers); at t=1, this point is at p2. And there is always ONE point on the graph. Below is my code, xy[t_] is the point coordinate at time t in a 2D region.

I have two problems here,

1). Points can be out of this region. How to fix it?

2). All the points are shown on a same graph, how to show only one point at a certain time t?

Any help? Thank you.

xy[t_]=Module[{t},
         Table[
             If[t<1,{x[0]=0,y[0]=RandomReal[{-1,1}]},
                    {x[t]=RandomReal[{0,1}],y[t]=RandomReal[{-1,1}]}],{t,0,10}]]

Animate[Graphics[{Red,PointSize[0.05],Point[xy[t]]},
  AxesLabel->{"x","y"},
  PlotRange->{{0,10},{0,10}}],{t,0,10}]
$\endgroup$
  • $\begingroup$ Greetings! Make the most of Mma.SE and take the tour now. Help us to help you, write an excellent question. Edit if improvable, show due diligence, give brief context, include minimal working examples of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ – rhermans Feb 7 '16 at 10:17
5
$\begingroup$

Your creation of xy[t_]=... is not a good idea in the sense that xy[anything] produces a static table.

Try

xyPointList = 
 Module[{t}, 
  Table[If[t < 1, {x[0] = 0, 
     y[0] = RandomReal[{-1, 1}]}, {x[t] = 
      x[t - 1] + RandomReal[{0, 1}], 
     y[t] = y[t - 1] + RandomReal[{-1, 1}]}], {t, 0, 10}]]

(* {{0, 0.0251049}, {0.211352, 0.0367706}, {0.489274, 
  0.7281}, {1.39872, 0.538182}, {1.68534, 1.26225}, {2.18648, 
  1.66184}, {2.4956, 2.65684}, {2.69291, 1.87413}, {3.20496, 
  1.26491}, {3.30305, 2.02537}, {4.0114, 2.23029}} *)

Next use Map to produce a list of red points. I have adjusted the PlotRange so your points do not go off scale.

xyGraphicsList = 
  Graphics[{Red, PointSize[0.05], Point[#]}, AxesLabel -> {"x", "y"}, 
     PlotRange -> {{-1, 10}, {-3, 3}}] & /@ xyPointList;

Finally, use ListAnimate rather than Animate.

ListAnimate[xyList]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks! That's what I need and it's very clear. Thanks Again. BTW, here is only one point moving, if I want to add more points (like 10 points) moving, how to make it work? I am trying to use xyPointList[n_] where n=10, it doesn't work. $\endgroup$ – B.Bai Feb 7 '16 at 19:50
  • $\begingroup$ Yes. You may have some work to do to place the expressions in the proper format for ListAnimate and to say have different colors for the different groups of points, but in principle what you have is great. $\endgroup$ – Jack LaVigne Feb 8 '16 at 4:32
4
$\begingroup$

I am precomputing an Array of random numbers from 0 to 1 for x and -1 to 1 for y that will be the displacements. To that I Prepend {0,0} to define a fixed initial position. Then I Accumulate that list for a successive accumulated totals of elements. Then use Interpolation so the function is also defined at any arbitrary position between points.

xy = Interpolation[
   Accumulate[
    Prepend[
     Array[{RandomReal[{0, 1}], RandomReal[{-1, 1}]} &, 100],
     {0, 0}
     ]]
   , InterpolationOrder -> 1
   ];

Then the trajectory is like this

Plot[xy[t], {t, 0, 50}]

Mathematica graphics

Now for the animation notice the {t, xy[t]} coordinates.

Animate[Graphics[{Red, PointSize[0.05], Point[{t, xy[t]}]}, 
  PlotRange -> {{0, 50}, {-5, 5}}], {t, 0, 
  50}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you very much. The point moves very smoothly and beautiful. But a little confused about " Array ", Thank you for giving me a chance to learn it and Mma. $\endgroup$ – B.Bai Feb 7 '16 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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