Matlab Random Walk Solution (Code)
Matlab Random Walk Solution (Code)
Matlab Random Walk Solution (Code)
Question A:
Input variables:
maxrange at line 3: the interval the random numbers taken from will be *1:maxrange+ length at line4: is the number of arrays considered, also the length of the last/ longest array.
X=[x{1:i}] %X is a matrix containing the numbers in the cells of x. same for Y and y. figure plot(X) grid on xlabel('length of array') ylabel('mean of array values'); title('mean vs array length') Y=[y{1:i}] figure plot(Y) grid on xlabel('length of array') ylabel('standard deviation of array values') title('standard deviation vs array length') %CONCLUSION: the mean appears to converge to half "maxrange", and the standard deviation converges to %approximatly a third of "maxrange".
Question B:
Input variables:
x and y at line 18 and 19: are the dimensions of the rectangular (or square) array of particles. howmanytimestepsat line 35: number of random steps the particles take randomly, a step on the xaxis plus a step on the y-axis are considered as one step. STEPSIZEAt line 38: the distance of the step taken on each axis by the particles, the actual step distance would be .
This second figure shows the ratio of the average squared distance between the particles and the time step at that point in time.
x=2 y=2 for i=1:x for i=1:y n=n+1 p{n}=[i,j] end end p n
%the line of cells which contain the initial coordinates of the points %which we found (p) will be inserted as the first entry for q. q will have %the different lines of coordinates which every time step. q{1}=p howmanytimesteps= 100 % 100 being the time steps or time, and q{1} is reserved for the initial position p, changing this value would make the random walk of points clearer, but take more time for k=1:howmanytimesteps for l=1:n %the second for(and the counter 'l') is to add the random step of x and y to each point directly, i.e to each cell of p at a certain time, which would coincide to a cell of q. STEPSIZE=0.25 % .25 here is delta, or the step length taken on both x and y axes. q{k+1}{l}=(STEPSIZE*randsrc(1,2))+q{k}{l} end end
% r has rows of cells. in each row; cells have all the squared distances(substracted coordinates) from one point to all others, %including its distance to itself(n distances). different rows corrospond %to different time steps. %if we want to get the distances(instead of squared distances) we just put all what's after the = in the for statement in sqrt() for K=1:k for N=1:n for i=1:n r{K,N}(i)=sum((q{K}{i}-q{K}{N}).^2) end end end r %R is a (k x n) matrix, (k is the number of time iterations and n is the number of particles). the elements of a row corrospond to a certain iteration and are the average squared distances from each %point at that time to all other points, in other words, the average of the cells of r. for K=1:k for j=1:n
R(K,j)=mean(r{K,j}) end end R %avg is a one column vector where each element is the average of the average distances from each point to all other %points at a certain time avg=mean(R,2) %avgratio is just each element of avg devided by its respective time step (k). for i=1:k avgratio(i,1)=avg(i)/i end figure% for g=1:k for i=1:n xaxis{g}(i)=q{g}{i}(1) yaxis{g}(i)=q{g}{i}(2) end scatter(xaxis{g},yaxis{g}) hold on; grid on drawnow; end %finally the ratio of the squared average distance/time step is plotted %(with time?) %since the column matrix 'avgratio' has the average ratios in the consecutive time points, i just plot it directly hold off figure% plot(avgratio) % you can replace 'avgratio' by 'avg' to see how the average behaves xlabel('time') ylabel('squared mean distance/time step') %CONCLUSION: the diffusion coefficient or the ratio in the last plot %has a mean, or converges to a number that relates to the step size %(STEPSIZE).