Friday, 5 December 2014

Basic physics and Python: simple harmonic motion

Here is simple harmonic motion simulation with a spring and a bouncing ball.
Springs are a classic example of harmonic motion, on Wikipedia you can get a grasp of the basics. Among other assumption, in my simulation I’ve assumed an ideal spring and that there is no friction (and therefore the motion will not stop by itself) however, if you like, you can implement friction easily.

Here is the spring simulation

Below is the bouncing ball:

A simpler example of simple harmonic motion with a spring (video):

Hope this was interesting.


  1. Thanks. I'd like to write an animation program that simulates the forces of gravitational attraction between two bodies. First have to learn how to use animate.

  2. p1 = [position(fun(counter))[0][0],position(fun(counter))[0][1]] can i know what does this line do ? im new to python , i search for plt.gca and i still dun get it

    1. That's some horribly inefficient and convoluted code on my behalf I'm afraid! :D that line simply returns p1 from the position function, you could replace it with p1 = position(fun(counter))[0]. p1 is one of the four points you need to draw a rectangle.

      plt.gca does nothing, it's a comment.