C++ Harmonic Oscillator (with Force)

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 3K views
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.