Using MATLAB create a movie that simulates a random walk on a 1000 x 1000 grid.
ID: 3802073 • Letter: U
Question
Using MATLAB create a movie that simulates a random walk on a 1000 x 1000 grid. Start the walk in the center (500 x 500) and increase/decrease x and y by, about, 10 units depending on the value of rand.
*For example, If the value of rand is less than .25, increase both x and y by 10. If the value of rand is less than .5 but greater than .25, decrease x by 10 and increase y by 10, etc.
You can run your walk for a set number of iterations or have it walk until it reaches the border of the plot window. Update the plot title with the current x,y values. Save your movie as an AVI file.
Please show all work. Thanks!
Explanation / Answer
clear;
clc;
x=0:1000;
y=0:1000;
Nj = 10;
Ns = 100;
forj=1:Ns
fork=1:Nj
Snx(1) = 500;
Sny(1) = 500;
fori=1:j
s = rand(1,2);
if(s(1,1) < .25)
x=x+10;
y=y+10;
elseif(s(1,1) < .5 && s(1,1)>.25)
X = x-10;
Y = Y+10;
end
end
Snx(i+1) = Snx(i) + X;
Sny(i+1) = Sny(i) + Y;
end
if(k==1 && j == Ns)
figure(1);
plot(Snx,Sny,'-k','LineWidth',4);
holdon;
plot(Snx(Ns+1),Sny(Ns+1),'.k','MarkerSize',40);
elseif(k==2 && j == Ns)
plot(Snx,Sny,'-r','LineWidth',3);
plot(Snx(Ns+1),Sny(Ns+1),'.r','MarkerSize',50);
elseif(k==Nj-1 && j == Ns)
plot(Snx,Sny,'-b','LineWidth',2);
plot(Snx(Ns+1),Sny(Ns+1),'.b','MarkerSize',50);
elseif(k==Nj && j == Ns)
plot(Snx,Sny,'-g','LineWidth',1);
plot(Snx(Ns+1),Sny(Ns+1),'.g','MarkerSize',50);
holdoff;
gridon;
xlabel('x');
ylabel('y');
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.