Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'How to convert this from 2D to 3D?' topicStudent Support Forum > General > "How to convert this from 2D to 3D?"

Next Comment >Help | Reply To Topic
Author Comment/Response
Peter
03/31/07 7:39pm

Hello, I am trying to convert this code which simulates RandomWalk in 2D to 3D. I am not versed in Mathematica at all and all the functions seem to confuse me. Here is the original code in 2D

(*generate path of random walk*)SeedRandom[]

x0 = 0;

y0 = 0;

x = x0;

y = y0;

n = 10;

xlst = {0};

ylst = {0};

Do[If[Random[Integer] == 0, x++, x--];
If[Random[Integer] == 0, y++, y--];
xlst = Append[xlst, x];
ylst = Append[ylst, y], {n}]

(*plot animation of path*)

xMin = Min[xlst];

xMax = Max[xlst];

yMin = Min[ylst];

yMax = Max[ylst];

lst = Transpose[{xlst, ylst}];

Do[(*plot points of path in blue*)
lpPts = ListPlot[Take[lst, i], PlotRange -> {{xMin, xMax}, {yMin, yMax}},PlotStyle -> {PointSize[0.03], RGBColor[0, 0, 1]},DisplayFunction ->Identity];

(*plot lines of path*)
lpLines = ListPlot[Take[lst, i], PlotRange -> {{xMin,xMax}, {yMin, yMax}}, PlotJoined -> True, DisplayFunction -> Identity];

(*display last point in red*)
lastPoint = ListPlot[{lst[[i]]}, PlotStyle -> {PointSize[0.03], RGBColor[1, 0, 0]},
DisplayFunction -> Identity];

Show[{lpPts, lpLines, lastPoint}, DisplayFunction -> $DisplayFunction], {i, n + 1}]

I tried to change it to...............


(*generate path of random walk*)SeedRandom[]

x0 = 0;

y0 = 0;

z0 = 0;

x = x0;

y = y0;

z = z0;

n = 10;

xlst = {0};

ylst = {0};

zlst = {0};

Do[If[Random[Integer] == 0, x++, x--];
If[Random[Integer] == 0, y++, y--];
If[Random[Integer] == 0, z++, z--];
xlst = Append[xlst, x];
ylst = Append[ylst, y];
zlst = Append[zlst, z], {n}]

(*plot animation of path*)

xMin = Min[xlst];

xMax = Max[xlst];

yMin = Min[ylst];

yMax = Max[ylst];

zMin = Min[zlst];

zMax = Max[zlst];

lst = Transpose[{xlst, ylst, zlst}];

Do[(*plot points of path in blue*)lpPts = ListPlot3D[Take[lst,
i], PlotRange -> {{xMin, xMax}, {yMin, yMax}, {zMin,
zMax}}, PlotStyle -> {PointSize[0.03], RGBColor[
0, 0, 1]}, DisplayFunction -> Identity];
(*plot lines of path*)lpLines =
ListPlot3D[Take[lst, i], PlotRange -> {{xMin, xMax}, {
yMin, yMax}, {zMin, zMax}}, PlotJoined ->
True, DisplayFunction -> Identity];
(*display last point in red*)lastPoint = ListPlot3D[{lst[[i]]}, PlotStyle \
-> {PointSize[0.03], RGBColor[1, 0, 0]}, DisplayFunction -> Identity];
Show[{lpPts, lpLines, lastPoint}, DisplayFunction -> $DisplayFunction], {i, \
n + 1}]




Which evidently does not work. I would appreciate if someone could point me in the right direction, thanks.




URL: ,

Subject (listing for 'How to convert this from 2D to 3D?')
Author Date Posted
How to convert this from 2D to 3D? Peter 03/31/07 7:39pm
Re: How to convert this from 2D to 3D? yehuda ben-s... 04/03/07 3:51pm
Re: How to convert this from 2D to 3D? Peter 04/04/07 03:23am
Next Comment >Help | Reply To Topic