Durham Hackathon

A short intro to numerical simulation

View on GitHub

Previous step

How to bounce a whole lot of balls

Can we simulate many balls? Of course we can. We only need an extra loop that runs over all the balls.

We should then check if they have collided and bounce back if they have.

This requires two loops, one loop over all balls to update their velocities and positions, and a second loop to check for collisions. Note that there are ways of making this computation more efficient, but for now two for loops will be fine.

The new velocities after a collision of two balls are calculated using conservation of momentum, so the new velocity of the first ball should be the old velocity of the second, and vice versa.

Add bounce:


 

At this point you can either move on to the more advanced topics listed in the index or you can customise your simulation.

The easiest thing to start with is changing the colors. Pick your own in the list. The list is not exhaustive, there are other colors you could add. Or you can change the order of the list, with a few balls only the first colors are used.

The next thing is to change the shape of the ball. Perhaps you would prefer to show squares? Or turtles?

You can even change the background color by setting the background color window.bgcolor("orange")

For the documentation explaining all the options the turtle library provides look here. Like many python projects, turtle is well-documented.

Fancy a hint? You can find the solution here.

Next step