I am new to programming inMathematica, and I am trying to pick up a few things by myself. As an exercise, I wanted to generate a two-dimensional random walk starting at the origin, and then moving a unit length randomly in each subsequent step. After that the goal of the exercise is to a) find the distance between the origin and the terminal point and b) if possible, to plot the random walk.
Here's what I thought of doing. Assign a vector a = {0,0} and then add a normalized vector b = Normalize[{RandomReal[], RandomReal[]}]
, and perform this procedure iteratively.
What I'm having trouble doing is performing the iterations. For instance, in the first step a + b generates c1, in the second I wish to generate c2 = c1 + b, and so on.
Moreover, I want b to be different each time, something I have failed at accomplishing. I'm kinda lost, and don't know where to start, any help would be appreciated.
Answer
Try the following code:
pt=Accumulate[{Sin@#,Cos@#}&/@RandomReal[{0,2 Pi},1000]];
boundary={Min@pt,Max@pt};
(*Distance is here*)
Norm@Last@pt
ListLinePlot[pt,PlotRange->{boundary,boundary},AspectRatio->1]
The result will be:
Also,I suspect you may need to run it multiple times and track it's end points distribution, so try this:
pt = Table[Total[{Sin@#, Cos@#} & /@RandomReal[{0, 2 Pi}, 1000]], {1000}];
Histogram3D[pt,ColorFunction->"TemperatureMap"]
Histogram[Norm /@ pt,ColorFunction->"TemperatureMap"]
And the result will be:
Comments
Post a Comment