You are on page 1of 7

ME G0200 Applied Fluid Mechanics HW#1: Random walk and diffusion Yussef Rikli

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.

Sample resulting figures:


As mentioned in the conclusion notes in the Matlab code, the mean converges to approximately half of maxrange(100/2=50), while the standard deviation converges to approximately one third of it(100/3=30). Changing maxrange supports this conclusion.

The Matlab code:


clc clear all maxrange=100 length=100 for i=1:length %x has cells containing mean values of arrays of different lengths, y has %cells of standard deviations of arrays of consecutive lengths. x{i}=mean(randi(maxrange,1,i)) y{i}=std(randi(maxrange,1,i)) %the place/position the mean is holding, coincides/corrosponds with its length. i.e: %first cell has mean/std for an array of one random number. second value is the mean/std of an %array of two number etc... end

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 .

Sample resulting figures:


The first figure shows the real time random (drunken) walk of the particles if run in Matlab. Different colors correspond to different points in time.

This second figure shows the ratio of the average squared distance between the particles and the time step at that point in time.

The Matlab code:


%ME G0200 Applied Fluid Mechanics %HW#1 Random walk and diffusion %Part B. clear all clc %n =number of points we have; serial number of points %x,y = number of particles on each axis in the rectangular spatial grid %(square if x=y, like here) %p has one line of cells of n length, each cell has a 2 element matrix/array/pair which %are the x and y coordinate of the points in the grid. n here does not mean that %the grid the points are in, is of size (n x n), they're of the size(x x y) % as definied in the begining. n=0

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).

You might also like