Numerical integration of an harmonic oscillator using java

In summary, the conversation discusses the analysis of a harmonic oscillator using kinematics. The process involves calculating the force applied by the spring, acceleration, speed, and updating the position. This is done in a loop with a set time interval. There is an issue with the values increasing rapidly and potentially leading to an infinite loop. It is determined that the problem is caused by using a large time interval and Java for numerics.
  • #1
user123897
3
0
Hi, I am 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 :)
 
Technology news on Phys.org
  • #2
user123897 said:
a = ((f / m) + a) / 2;
what's this ?
 
  • Like
Likes vanhees71
  • #3
BvU said:
what's this ?
average acceleration. note that the a in the right side is the previous acceleration.
 
  • #4
user123897 said:
can someone understand where i got it wrong?

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?
 
  • #5
user123897 said:
average acceleration. note that the a in the right side is the previous acceleration.
I see. It's not he problem, though. In fact I can't reproduce your runaway:

upload_2016-8-10_10-27-5.png
 
  • #6
BvU said:
I see. It's not he problem, though. In fact I can't reproduce your runaway:

View attachment 104489
Figured out the problem. I dt = 10, way too big considering the expected period time.

Thanks for your help!
 
  • #7
10 milliseconds isn't bad. 10 seconds is. :smile:
 
  • #8
The main problem is using Java for numerics, I guess ;-)). SCNR.
 
  • #9
Real programmers can write Fortran in any language.
 
  • Like
Likes anorlunda and vanhees71

1. What is numerical integration and why is it important in the study of harmonic oscillators?

Numerical integration is a method used to approximate the value of a definite integral by dividing the interval into smaller subintervals and using a numerical algorithm to calculate the area under the curve. It is important in studying harmonic oscillators because it allows us to solve the differential equations that describe their motion and accurately predict their behavior over time.

2. How does Java help with numerical integration of a harmonic oscillator?

Java is a programming language that offers a variety of built-in functions and libraries that make it easier to perform numerical integration. These include methods for calculating derivatives, solving differential equations, and implementing numerical algorithms such as the Euler or Runge-Kutta methods.

3. Can you explain the Euler method and how it is used to numerically integrate a harmonic oscillator in Java?

The Euler method is a simple numerical algorithm that approximates the solution to a differential equation by using the derivative at a given point to calculate the value at the next point. In Java, this can be implemented by creating a loop that iterates through small time intervals and updates the position and velocity of the oscillator at each step based on the current values and the derivative function.

4. What are the limitations of using numerical integration in the study of harmonic oscillators?

Numerical integration can introduce errors in the calculation due to its approximation nature. As the time intervals become smaller, the accuracy of the results can improve, but this also increases the computational time. Additionally, if the differential equations describing the harmonic oscillator are highly nonlinear, the numerical algorithms may struggle to accurately predict the behavior over longer periods of time.

5. Are there any alternative methods to numerically integrate a harmonic oscillator besides the Euler method?

Yes, there are several other numerical integration methods that can be used in Java, such as the Runge-Kutta method, which is more accurate and efficient than the Euler method. Other methods include the Verlet algorithm, which is commonly used in molecular dynamics simulations, and the symplectic integrators, which preserve the energy of the system. The choice of method depends on the specific needs and limitations of the study.

Similar threads

  • Programming and Computer Science
Replies
1
Views
628
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
50
Views
4K
  • Introductory Physics Homework Help
Replies
13
Views
560
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Quantum Physics
Replies
1
Views
525
Back
Top