Question: Working in Matlab with random walks. This is my code to plot the positions of 1000 random walkers at times t1=100, t2=200 and t3=300. I
Working in Matlab with random walks. This is my code to plot the positions of 1000 random walkers at times t1=100, t2=200 and t3=300. I was wondering if someone could help me condense some of the lines to make it neater (the repeated parts in the for loop and then again with the subplots at the end). Been trying it for a couple of hours with no luck. Thanks.
See the answer to this question for a guide: https://www.chegg.com/homework-help/questions-and-answers/working-matlab-random-walks-code-plot-distance-5000-random-walkers-histograms-times-t1-100-q31101790
_________________
Here is the rw function called in the below script:
function[x,y] = rw(t)
x = zeros(1,t+1); y = zeros(1,t+1);
for i = 1:t ang = 2*pi*rand(1); x(i+1) = x(i)+cos(ang); y(i+1) = y(i)+sin(ang);
end
________________________
The Script:
n=1000; t1=100; t2=200; t3=300;
xt1=zeros(1,n); yt1=zeros(1,n);
xt2=zeros(1,n); yt2=zeros(1,n);
xt3=zeros(1,n); yt3=zeros(1,n);
for i = 1:n [x,y]=rw(t3); xt1(i)=x(t1+1); yt1(i)=y(t1+1); xt2(i)=x(t2+1); yt2(i)=y(t2+1); xt3(i)=x(t3+1); yt3(i)=y(t3+1); end
subplot(1,3,1); plot(xt1,yt1, '.'); xlabel('x') ylabel('y') title('t=100') axis([-50 50 -50 50]) axis square
subplot(1,3,2); plot(xt3,yt2, '.'); xlabel('x') ylabel('y') title('t=200') axis([-50 50 -50 50]) axis square
subplot(1,3,3); plot(xt3,yt3, '.'); xlabel('x') ylabel('y') title('t=300') axis([-50 50 -50 50]) axis square
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
