Brownian motion simulation: USAGE: brownian(3) Simulate 3 particles. Using the EraseMode property is appropriate for long sequences of simple plots where the change from frame to frame is minimal. Here is an example showing simulated Brownian motion. Specify a number of particles and a temperature or velocity, such as
function brownian(NParticles ) %Brownian motion simulation: %USAGE: brownian(3) Simulate 3 particles. % Using the EraseMode property is appropriate for long sequences % of simple plots where the change from frame to frame is minimal. % Here is an example showing simulated Brownian motion. % Specify a number of particles % and a temperature or velocity, such as s = .02 % The best values for these two parameters depend upon the speed % of your particular computer. % Generate particles random points with (x,y) coordinates % between -1/2 and +1/2: x = rand(NParticles,1)-0.5; y = rand(NParticles,1)-0.5; % Plot the points in a square with sides at -1 and +1. % Save the handle for the vector of points and % set its EraseMode to xor. % This tells the MATLAB graphics system not to redraw % the entire plot when the coordinates of one point are changed, % but to restore the background color in the vicinity % of the point using an exclusive or operation: h = plot(x,y,'.'); axis([-1 1 -1 1]) axis square grid off set(h,'EraseMode','xor','MarkerSize',18) % Now begin the animation. Here is an infinite while loop, % which you can eventually exit by typing Ctrl+c. % Each time through the loop, add a small amount of normally % distributed random noise to the coordinates of the points. % Then, instead of creating an entirely new plot, % change the XData and YData properties of the original plot: while 1 drawnow pause(0.05) x = x + s*randn(NParticles,1); y = y + s*randn(NParticles,1); set(h,'XData',x,'YData',y) end