Numerical integration of an harmonic oscillator using java

Click For Summary

Discussion Overview

The discussion revolves around the numerical integration of a harmonic oscillator using Java. Participants analyze the implementation of kinematic equations in a programming loop and seek to identify issues related to the rapid increase of calculated values.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach to modeling a harmonic oscillator using kinematic equations, detailing the calculations for force, acceleration, speed, and position updates.
  • Another participant questions the use of the average acceleration formula in the loop, seeking clarification on its purpose.
  • There is a discussion about the structure of the loop, with inquiries about its type and conditions for termination, particularly regarding the time increment (dt).
  • A participant identifies that the choice of dt being too large may contribute to the runaway values observed in the simulation.
  • Some participants humorously comment on the suitability of Java for numerical computations and the ability to write Fortran in any language.

Areas of Agreement / Disagreement

Participants express differing views on the appropriateness of the dt value and the programming language used. There is no consensus on the best practices for numerical integration in this context, and the discussion remains unresolved regarding optimal implementation strategies.

Contextual Notes

Participants do not fully explore the implications of their chosen dt value on the accuracy of the simulation. There is also a lack of clarity on the loop's termination conditions and how they impact the results.

Who May Find This Useful

Readers interested in numerical methods for simulating physical systems, programming in Java, or those facing challenges with kinematic equations in computational contexts may find this discussion relevant.

user123897
Messages
3
Reaction score
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
user123897 said:
a = ((f / m) + a) / 2;
what's this ?
 
  • Like
Likes   Reactions: vanhees71
BvU said:
what's this ?
average acceleration. note that the a in the right side is the previous acceleration.
 
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?
 
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
 
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!
 
10 milliseconds isn't bad. 10 seconds is. :smile:
 
The main problem is using Java for numerics, I guess ;-)). SCNR.
 
Real programmers can write Fortran in any language.
 
  • Like
Likes   Reactions: anorlunda and vanhees71

Similar threads

  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 50 ·
2
Replies
50
Views
6K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 6 ·
Replies
6
Views
7K
Replies
13
Views
2K
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
7K