Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Euler method for modeling simple harmonic oscillation

  1. Feb 9, 2015 #1
    Hello!

    An assignment for my computational modeling course is to demonstrate the use of the Standard Euler method for modeling a simple harmonic oscillator; in this case, a mass attached to the end of a spring.

    I have the two coupled first-order differential equations satisfying hookes law: dx/dt = v, and dv/dt = -(k/m)*x

    The numerical solutions of which are v(t + dt) = v(t) - k*x(t)*dt / m, and x(t + dt) = x(t) + v(t)*dt to model the velocity and position, respectively.

    In a computer program, I've represented this as:

    x = x + v*dt;
    v = v - k*x*dt / m;
    t = t + dt;

    with dt = 0.04, m = 1, k = 1, and initial values v = 0, t = 0, and x = 5.

    The Purpose of the exercise is to demonstrate how the standard Euler Method is non-stable and results in non-conserved energy.

    When iterating the above Euler method for sufficiently large periods of time, I've expected x to grow larger after each period but my numerical method above is acting like a conserved-energy Improved Euler method (Euler-cromer)? Please see attached plot.
     

    Attached Files:

    Last edited: Feb 9, 2015
  2. jcsd
  3. Feb 10, 2015 #2

    Quantum Defect

    User Avatar
    Homework Helper
    Gold Member

    Your plot shows only a few cycles, where the method should probably be "good enough." What is the energy as a function of time? Why not plot the energy error as a function of time? Error = PE_0 - (KE + PE) -- When you plot the difference, it will be much more obvious that the system is non-conservative.
     
  4. Feb 10, 2015 #3

    DrClaude

    User Avatar

    Staff: Mentor

    This does not implement the system of equations you have. Pay close attention to the time-dependence of the variables.
     
  5. Feb 10, 2015 #4

    DrClaude

    User Avatar

    Staff: Mentor

    Euler's method is bad enough that the error is clearly noticeable even after two cycles!
     
  6. Feb 10, 2015 #5

    Quantum Defect

    User Avatar
    Homework Helper
    Gold Member

    ... I missed that!
     
  7. Feb 10, 2015 #6
    Thank you!

    Modification:

    e = (v*v/2) + (x*x/2);
    a = -k*x / m;
    x = x + v*dt;
    v = v + a*dt;
    t = t + dt;

    Plotting e, t and x produces the following:
     

    Attached Files:

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Euler method for modeling simple harmonic oscillation
  1. Euler's Method (Replies: 2)

  2. Euler equation (Replies: 5)

Loading...