Message Boards Message Boards

Where I need put values in my code to generate the result that I want

Posted 9 years ago

I'm doing a project about random walks, but I don't find where put values from n and t to give me the result that I want

Meander[n_, t_] :=

Module[{RND, walk, VonNeumann, initConf},

RND := Random[Integer, {1, 4}];

  initConf = 

  ReplacePart[Table[0, {2 n + 1}, {2 n + 1}], RND, {n + 1, n + 1}];

  walk[1, 0, 0, 0, 0] := 0;

  walk[2, 0, 0, 0, 0] := 0;

  walk[3, 0, 0, 0, 0] := 0;

  walk[4, 0, 0, 0, 0] := 0;

  walk[0, 3, 0, 0, 0] := RND;

  walk[0, 0, 4, 0, 0] := RND;

  walk[0, 0, 0, 1, 0] := RND;

  walk[0, 0, 0, 0, 2] := RND;

walk[0, 1, 0, 0, 0] := 0;

walk[0, 2, 0, 0, 0] := 0;

walk[0, 4, 0, 0, 0] := 0;

walk[0, 0, 1, 0, 0] := 0;

walk[0, 0, 2, 0, 0] := 0;

walk[0, 0, 3, 0, 0] := 0;

walk[0, 0, 0, 2, 0] := 0;

walk[0, 0, 0, 3, 0] := 0;

walk[0, 0, 0, 4, 0] := 0;

walk[0, 0, 0, 0, 1] := 0;

walk[0, 0, 0, 0, 3] := 0;

walk[0, 0, 0, 0, 4] := 0;

walk[0, 0, 0, 0, 0] := 0;


VonNeumann[func_, lat_] := MapThread[func, Map[RotateRight[lat, #] &, {{0, 0}, {1, 0}, {0, -1}, {-1, 0}, {0, 1}}], 2];


  NestList[VonNeumann[walk, #] &, initConf, t]
  ]
POSTED BY: Camilo Pacheco
5 Replies

Hi Camilo,

So, it looks like the numbers are the directions to move. But if you are going for a graphic, all you really need is a position in the matrix.

Instead of using 0 for the entries that don't count, I used 0.0 so that I can distinguish the position of the newly generated move by looking for an Integer.

Then I generated a graphic of each of the moves by drawing a line from each move to the next.

I'm not very advanced, functional programming-wise, so I used a Do loop.

Meander[n_, t_] := 
 Module[{RND, walk, VonNeumann, initConf}, 
  RND := Random[Integer, {1, 4}];
  initConf = 
   ReplacePart[Table[0.0, {2 n + 1}, {2 n + 1}], RND, {n + 1, n + 1}];
  walk[1, 0.0, 0.0, 0.0, 0.0] := 0.0;
  walk[2, 0.0, 0.0, 0.0, 0.0] := 0.0;
  walk[3, 0.0, 0.0, 0.0, 0.0] := 0.0;
  walk[4, 0.0, 0.0, 0.0, 0.0] := 0.0;
  walk[0.0, 3, 0.0, 0.0, 0.0] := RND;
  walk[0.0, 0.0, 4, 0.0, 0.0] := RND;
  walk[0.0, 0.0, 0.0, 1, 0.0] := RND;
  walk[0.0, 0.0, 0.0, 0.0, 2] := RND;
  walk[0.0, 1, 0.0, 0.0, 0.0] := 0.0;
  walk[0.0, 2, 0.0, 0.0, 0.0] := 0.0;
  walk[0.0, 4, 0.0, 0.0, 0.0] := 0.0;
  walk[0.0, 0.0, 1, 0.0, 0.0] := 0.0;
  walk[0.0, 0.0, 2, 0.0, 0.0] := 0.0;
  walk[0.0, 0.0, 3, 0.0, 0.0] := 0.0;
  walk[0.0, 0.0, 0.0, 2, 0.0] := 0.0;
  walk[0.0, 0.0, 0.0, 3, 0.0] := 0.0;
  walk[0.0, 0.0, 0.0, 4, 0.0] := 0.0;
  walk[0.0, 0.0, 0.0, 0.0, 1] := 0.0;
  walk[0.0, 0.0, 0.0, 0.0, 3] := 0.0;
  walk[0.0, 0.0, 0.0, 0.0, 4] := 0.0;
  walk[0.0, 0.0, 0.0, 0.0, 0.0] := 0.0;
  VonNeumann[func_, lat_] := 
   MapThread[func, 
    Map[RotateRight[lat, #] &, {{0.0, 0.0}, {1, 0.0}, {0.0, -1}, {-1, 
       0.0}, {0.0, 1}}], 2];
  NestList[VonNeumann[walk, #] &, initConf, t]]


out = Meander[6, 126]; bb = {}; Do[
 AppendTo[bb, Position[out[[d, All]], _Integer]], {d, 1, 
  127}]; Graphics[Line[Flatten[bb, 1]]]

It looks as though your random walk is actually on a torus connected left-to-right and top-to-bottom. That would be interesting to draw!

Eric

POSTED BY: Eric Johnstone

Hi Camilo,

I'm beginning to see why you wanted NestList; is it so that you can see the progression of the walk?

Here are the individual parts summed into one:

Meander[n_,t_]:=Module[{RND,walk,VonNeumann,initConf,out,m},RND:=Random[Integer,{1,4}];
initConf=ReplacePart[Table[0,{2 n+1},{2 n+1}],RND,{n+1,n+1}];
walk[1,0,0,0,0]:=0;
walk[2,0,0,0,0]:=0;
walk[3,0,0,0,0]:=0;
walk[4,0,0,0,0]:=0;
walk[0,3,0,0,0]:=RND;
walk[0,0,4,0,0]:=RND;
walk[0,0,0,1,0]:=RND;
walk[0,0,0,0,2]:=RND;
walk[0,1,0,0,0]:=0;
walk[0,2,0,0,0]:=0;
walk[0,4,0,0,0]:=0;
walk[0,0,1,0,0]:=0;
walk[0,0,2,0,0]:=0;
walk[0,0,3,0,0]:=0;
walk[0,0,0,2,0]:=0;
walk[0,0,0,3,0]:=0;
walk[0,0,0,4,0]:=0;
walk[0,0,0,0,1]:=0;
walk[0,0,0,0,3]:=0;
walk[0,0,0,0,4]:=0;
walk[0,0,0,0,0]:=0;
VonNeumann[func_,lat_]:=MapThread[func,Map[RotateRight[lat,#]&,{{0,0},{1,0},{0,-1},{-1,0},{0,1}}],2];
out=NestList[VonNeumann[walk,#]&,initConf,t];
Sum[out[[m]],{m,1,t+1}]//MatrixForm]

Maybe Sum isn't what you want. Not sure what the numbers mean.

Eric

POSTED BY: Eric Johnstone

Hi Camilo,

I'm not quite sure what Meander is supposed to do, but if you change the NestList to Nest, you'll only show the last of the Nested calculation, not the whole list. Then if you use Meander[n,t]//MatrixForm, you can see the entry that was generated for each call.

This might not be what you are aiming for. A little description of Meander would be helpful.

Eric

POSTED BY: Eric Johnstone

Could it be that you've forgotten the blanks in the function arguments?

Try

Meander[n_, t_] :=

and

VonNeumann[func_, lat_] :=

Eric

POSTED BY: Eric Johnstone
Posted 9 years ago

Thank you Eric, I put it and show me the output but now I have a new question, can you explain why generate a matrix output?

POSTED BY: Camilo Pacheco
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract