Background

NumPy comes with a very powerful data type that can be used to do linear algebra called an array. This is a single variable that contains multiple numbers.

They are different from a list, which is also a single variable that contains multiple numbers. The differences are:

Lists:

Arrays:

For now, we’ll only be using this for Cartesian vectors – arrays that have one dimension that is three elements long. Some of the workings of NumPy for arrays of more than one dimension can get hairy (but very powerful).

The best way to learn how to use vectors in NumPy is to fiddle with things yourself and see what they do. You can use the Python REPL to do this; just type python or python3 at the command line.

Here are some things to try:

Once you’ve finished this, modify your orbits code to use NumPy vectors for the position and velocity of the planet (and, if the Sun is not at the origin, the Sun). This can make your code much simpler; I just wrote a simulation that solves the orbit problem in 10 lines of code!

Then, once you’re done, think about what you need to do for the case where both the Sun and the Earth move. Now you’ll need two position variables and two velocities (each of which is a three-component vector), and will need to update both positions and velocities each time.

Work on coding this, and I’ll see you all Tuesday!