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.

8 comments:

  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.

    ReplyDelete
  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

    ReplyDelete
    Replies
    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.

      Delete
    2. please if possible explain the meaning of
      p1 = [position(fun(counter))[0][0],position(fun(counter))[0][1]]
      p2 = [position(fun(counter))[1][0],position(fun(counter))[1][1]]
      p3 = [position(fun(counter))[2][0],position(fun(counter))[2][1]]
      p4 = [position(fun(counter))[3][0],position(fun(counter))[3][1]]

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. hey i'm having a problem programing a spring with potential and gravitational energy using the formula (1/2)Kx^2 + (1/6)Yx^3, i know we have to derive that to get force but i can't seem to add it up to create a cubic or more cubic graph. please help!!!!

    ReplyDelete
  5. Thanks for the lovely simulation codes, man.

    ReplyDelete