C++ Harmonic Oscillator (with Force)

AI Thread Summary
A C++ program can be developed to solve the Harmonic Oscillator equation with varying external force functions. Implementing function pointers is suggested for handling different user-defined force functions, allowing for greater flexibility. The initial idea involves using conditional statements to differentiate between various force inputs. However, there are concerns about potential complications since this method was previously applied to simpler tasks. Further consideration and refinement of the approach are necessary to ensure effective implementation.
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:
\ddot{x} + \gamma x = F_{external}(t,x)

With different F 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.
 
Back
Top