C++ Harmonic Oscillator (with Force)

Click For Summary
SUMMARY

The discussion centers on implementing a C++ program to solve the Harmonic Oscillator equation \(\ddot{x} + \gamma x = F_{external}(t,x)\). The proposed solution involves using function pointers to handle various force function inputs dynamically based on user specifications. This approach allows for greater flexibility in defining the external force, although the author acknowledges potential complications due to the complexity of the problem compared to previous simpler implementations.

PREREQUISITES
  • Proficiency in C++ programming
  • Understanding of differential equations, specifically the Harmonic Oscillator
  • Familiarity with function pointers in C++
  • Basic knowledge of numerical methods for solving differential equations
NEXT STEPS
  • Explore C++ function pointers and their applications in dynamic function handling
  • Research numerical methods for solving ordinary differential equations (ODEs)
  • Learn about the implementation of the Runge-Kutta method in C++ for ODEs
  • Investigate the use of libraries such as Boost for advanced mathematical computations in C++
USEFUL FOR

C++ developers, physicists, and engineers interested in simulating physical systems, particularly those working with differential equations and dynamic systems modeling.

ChrisVer
Science Advisor
Messages
3,372
Reaction score
465
I was wondering whether it's possible to write a code in C++ that would be able to solve the Harmonic Oscillator:
[itex]\ddot{x} + \gamma x = F_{external}(t,x)[/itex]

With different [itex]F[/itex] function inputs... I thought about creating a function with if clauses, so for different inputs by the user, the force F would be described by a different function...What do you think?
 
Physics news on Phys.org
The whole program surely needs more thought but for the force function, its better to use function pointers. Then you can work with an unspecified function which is later specified by the input from user. But I should think more about that because the program where I used this method was doing a simpler job so there may arise some complications.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K