back to Computational Physics home page

Introduction to Computing: Bonkers, using Octave

First part:

Write a function

function [ v1, v2 ] = collide(  u1, u2 , im1, im2  ) 
that returns the new velocities of two particles with inverse-masses im1 and im2, after an elastic collision. (Momentum is always conserved, in any collision between two isolated bodies. An elastic collision is one in which energy is conserved as well as momentum.) The particles are moving in one dimension only. The initial velocities u1 and u2 can have any value, positive or negative or zero; im1 and im2 can have any positive value, or zero (zero corresponds to the inverse-mass of an enormous wall). [The inverse-masses are simply im1 = 1/m1, im2 = 1/m2.]

Work out what this function should be. Think about special cases, then generalize. Check your answer on the special cases. Check that your general answer does indeed conserve momentum and energy.

Solution: collide.m, and a testing script: CHECKcollide.m.

[an error occurred while processing this directive]

Follow-up question: the function collide is a linear map from velocity space to velocity space. So we could represent it as a matrix. What does the matrix look like? What are its eigenvectors and eigenvalues?

Second part:

Simulate a collection of N particles with different masses, bouncing off each other, and moving in one dimension. For the final part of this project, it's important that the particles should all have different masses (for example, ranging between 1 and 9 mass units). But initially when developing your program it might be wise to check what happens in the special case where they are equal.

Collect data at every time Tc, 2Tc, 3Tc, ... If you are making a movie, you will want to have Tc a little smaller than the typical time between collisions. If you are just gathering statistics and not making a movie, you can use a bigger Tc -- perhaps N times bigger is reasonable, where N is the number of particles.

Run the simulation for a long time, and then plot histograms of the velocities and positions of all the different masses. Pick two particles, having the biggest and smallest masses, and look at the histograms of their velocities. Make a theory to describe these histograms.


Here are histograms of velocities using 400 samples. The histogram with velocities ranging from -8 to 8 is for a particle with mass 9. The histogram with velocities ranging from -24 to 24 is for a particle with mass 1.

To make good histograms you will want 1000s of samples. When making histograms, you may find the octave function histo useful. However, in my experience, this function does not do what I really want. Its bin sizes are too big. I therefore offer to you my histogram routine, djcmhisto.m, which also returns a Gaussian Fit to the histogram, and tells you the mean and standard deviation of each column.

You can see a simple example use of this histogram program here (TESTdjcmhisto.m).

Solution

A solution is in bounce1.m.

Here are examples of the types of graph that are interesting to plot. In order to not give away the results that you will get for a simulation with lots of masses, with masses ranging from 1 to 9, I show here some small histograms and scatterplots for a system with just two balls.

(1) (most important): Histogram of velocity of each particle
(2) scatter plot of positions of particles, or histograms of positions.
(3) scatter plot of position versus velocity.

bounce1.m is invoked by RUNbounce0.m

bounce1.m
[an error occurred while processing this directive]

bounce1.m is invoked by RUNbounce0.m

RUNbounce0.m

[an error occurred while processing this directive]

Another solution

Here's a slightly fancier solution in which the particles have a finite radius, to make better looking movies.
bounce2.m is invoked by cradle2.m

bounce2.m
[an error occurred while processing this directive]

bounce2.m is invoked by cradle2.m

cradle2.m

[an error occurred while processing this directive]