1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

B Numerical integration of an harmonic oscillator using java

  1. Aug 9, 2016 #1
    Hi, im trying to analyze the an harmonic oscillator using kinematics.
    first i calculate the force applied by the spring (f = (-x)*k)
    then i calculate the acceleration (a = f/m)
    then speed (v= v0 + v0t + 0.5*a*t^2)
    and finally update x (x = x0+vt)

    this is a simplfied loop of my program (written in java, all values are initialized to 0 expect x = 10)

    {
    x = x + v * dt + 0.5 * a * Math.pow(dt, 2);
    f = k * (-x);
    a = ((f / m) + a) / 2;
    v = v + a * dt;
    a = f / m;
    }

    this loop runs evry dt milliseconds.

    the values of x,v,a,f increase rapidly and aims to infinitey

    can someone understand where i got it wrong?

    thanks for your time :)
     
  2. jcsd
  3. Aug 9, 2016 #2

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    what's this ?
     
  4. Aug 9, 2016 #3
    average acceleration. note that the a in the right side is the previous acceleration.
     
  5. Aug 9, 2016 #4

    QuantumQuest

    User Avatar
    Gold Member

    What kind of loop is this (for, while...) and where are the condition(s) of it, especially the one (or whatever combination), that stop the loop from running indefinitely? From what function do you get dt to count?
     
  6. Aug 10, 2016 #5

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    I see. It's not he problem, though. In fact I can't reproduce your runaway:

    upload_2016-8-10_10-27-5.png
     
  7. Aug 10, 2016 #6
    Figured out the problem. I dt = 10, way too big considering the expected period time.

    Thanks for your help!
     
  8. Aug 10, 2016 #7

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    10 milliseconds isn't bad. 10 seconds is. :smile:
     
  9. Aug 11, 2016 #8

    vanhees71

    User Avatar
    Science Advisor
    2016 Award

    The main problem is using Java for numerics, I guess ;-)). SCNR.
     
  10. Aug 11, 2016 #9

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Real programmers can write Fortran in any language.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Numerical integration of an harmonic oscillator using java
  1. Harmonic Oscillator (Replies: 0)

  2. Harmonic Oscillator (Replies: 2)

  3. Harmonic oscillator (Replies: 6)

Loading...