MATLAB: Random steps of molecules undergoing Brownian motion

brownian motionmolecules

I have an assignment were I have to develop a computer model that mimics the random steps of molecules undergoing Brownian motion. Using Matlab and the
following criteria:
• Tracks the motion of 300 molecules within a 2-D space, over a series of time steps
• On each time step every molecule will move by a random amount in the x and y directions
• Your model needs to calculate the steps in space for all 300 molecules, store them in an array and repeat this for 100 time steps
• Start the model with all molecules at the [0,0] point
• At each time point plot the position of the molecules in a scatter plot and run this for all time points so that a ‘movie’ of the molecular motion is created
I'm a novice when it comes to Matlab so any help is much appreciated

Best Answer

Basically you can construct arrays for x and y where the row represents that particle, and the column is the step number (time point). So
numParticles = 300;
numSteps = 100;
x = zeros(numParticles, numSteps);
y = zeros(numParticles, numSteps);
Now you need a for loop over the number of steps where you compute a new x and y for all 300 particles and then plot them.
for stepNumber = 2 : numSteps
% Make all 300 particles move to a new location.
for p = 1 : numParticles
x(p, stepNumber) = x(p, stepNumber - 1) + stuff you write.
% Same for y
% Now to make a movie, plot it and call drawnow and pause
plot(x(p, 1:stepNumber), y(p, 1:stepNumber), 'b-');
hold on;
end
hold off;
drawnow;
grid on;
pause(0.4) % Bigger numbers will pause longer and make for a slower movie.
end
Adapt as needed and fill out the rest, like deciding on the delta x and delta y to add. I prefer plot() since you can see the "history" of the path as it wanders around, but you can use scatter(x, y) like the directions told you to, though that won't let you see the history of the path. You can fancy it up with xlabel(), ylabel(), and title().