motion.m
(Brownian motion simulation)




The simulation starts with 128 particles at the center of the box with a normal distribution of velocities. (The standard deviation of this distribution corresponds to a particular temperature.)

Typing motion(12) tells the simulator to start the simulation, run it for 12 time-steps ... resulting in a figure such as this one.

12 steps are not enough for any of the particles to reach the sides of the box yet, so all the particles are moving radially outward. A random selection of marker shapes, edge colors, and interior colors are used to simulate different types of molecules. The gray arrows represent the velocities with the arrow lengths being proportional to the particle speed.

The default number of steps is 20, so typing motion (with no arguments) is equivalent to typing motion(20).

With enough steps, the particles will begin hitting the walls at which point they will undergo an elastic collision (i.e. it bounces off with the same energy ... and the angle of reflection equals the angle of incidence).

Collisions between the particles are not simulated, but this simulation would still be accurate for a low-density gas.



Now suppose we click the Walk button. This will start up the simulation from where it left off. Notice that the Walk button has now turned into a STOP button. The simulation will continue to run forever (or until you hit the STOP button).

Also, notice that an "updates per second" report has been added to the title bar. This is useful mostly as a performance measure, so you can compare the performance of different computers, operating systems, or Matlab versions.

While the simulation is still in progress, try changing the speed control and you will see the motion speed up or slow down. This is best thought of as a simulation speed control, but you could also think of it as a temperature adjustment for the simulation. Also, try increasing the # of points slider (or typing a new number into the edit box next to the slider. Suppose for example we increase it from 128 to 228. Then 100 new points will appear to explode from the center of the box and those new particles will quickly become indistinguishable from the points that were already there. These two sliders, as well as their respective labels and edit boxes, were created using the slider pseudo object, which greatly simplifies the coding needed to add these additional components associated with the slider. There are two formats for the pseudo slider (regular and compact) ... and both of the sliders here are of the compact form. For more details about how to use this pseudo object, See the pseudo slider object section

Every time you click the Walk button, the particle velocities are randomized again (always with a Gaussian distribution). To see this clearly, try this experiment. Move the # of points slider all the way to the left (which will select 2 points - the minimum value allowed). Then move the speed slider all the way to the top - the fastest setting. Now click the Walk button and observe the motion pattern. Then click STOP, and then the Walk button again a few times, each time noting the motion pattern changes (dramatically).

Previously I described how to start the simulation with a fixed number of time steps, but it is also possible to start the simulation with an infinite number of steps. To do this type motion(0) or alternatively motion('go') (or the command form motion go). Actually it doesn't matter what character string you use, so, for example, the strings 'run', 'walk', 'start', or 'forever' will work just the same. They all start the program in its dynamic state, and the simulation will continue to run until you click on the STOP button. Note that the last line of the motion function uses funcStart to invoke the walk routine. If we used the simple way of starting the simulation (line 61 -which is commented out, then in the case where the argument is zero (or 'go') the program does not terminate which means we can no longer use the command window until after the STOP button has been pressed. More details about the funcStart auxiliary function can be found here in the Auxiliary functions section.

Typically the lines created by pltinit contain hundreds of points to make up each trace, however, in this example, all 513 lines created by pltinit consist of just a single point (representing a single particle) except for the last trace. The last trace plots 512 arrows, one arrow for each of the 512 particles. Note that the first 512 traces are created by the first two arguments of the pltinit call while the last trace (for the arrows) is created by the last two arguments of the pltinit call. The data for all these traces is set to zero which is just a placeholder, since the real data for all these lines (markers) and arrows are set inside the walk function.

Normally pltinit is limited to creating at most 99 traces because otherwise, the TraceID box would become too large to fit in the figure and still leave a reasonable amount of area for the plot. However, if we disable the TraceID box (which we don't need for this simulation) then there is essentially no limit to the number of traces we can create with the pltinit command. (The TraceID box is disabled by including 'TraceID',[] in the argument list. Note that we could have called plt instead of pltinit for the command that creates the 513 traces. This is because plt recognizes that traces are being created and simply passes the entire parameter list to pltinit. It's a matter of taste (explicit vs. conciseness) which function you want to use to create the traces.

You may wonder why ishandle(S.B) is included as one of the conditions of the while loop. It's there to cause the while loop to terminate when we close the figure window. If we didn't do that, closing the figure window while it was walking would generate a Matlab error message ('Nonexistent handle').

Copyright © 2023
Paul Mennen