Simulating a Pendulum in C++: Seeking Assistance

  • Context: C/C++ 
  • Thread starter Thread starter iox
  • Start date Start date
  • Tags Tags
    Assistance Pendulum
Click For Summary

Discussion Overview

The discussion revolves around simulating a pendulum in C++ using Cartesian coordinates instead of angular coordinates. Participants are exploring issues related to the pendulum's motion, specifically the problem of the pendulum bob "sinking" over time despite oscillating correctly.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach to simulating the pendulum, detailing the calculation of forces including tension and weight.
  • Another participant suggests that the "sinking" motion may be due to a rounding error and inquires about the use of Euler integration.
  • A participant clarifies that the pendulum's bob is falling on the screen, indicating a problem with the y-coordinate increasing too quickly while the swing remains constant.
  • One suggestion is to use more advanced methods like Euler integration or polar coordinates to avoid the sinking issue.
  • A participant questions the limitations of their simple calculation method and its applicability to other scenarios, such as a particle subjected to centripetal force.
  • Another participant explains that the current method of updating velocity and position may be flawed, recommending the use of the average of old and new velocities for better accuracy.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of the current simulation method and the potential causes of the sinking motion. There is no consensus on the best approach to resolve the issue, with multiple competing suggestions presented.

Contextual Notes

Participants mention potential issues with precision and the choice of numerical integration methods, but do not resolve these concerns. The discussion highlights the complexity of simulating dynamic systems accurately.

Who May Find This Useful

Individuals interested in physics simulations, numerical methods in programming, and those facing similar challenges in implementing dynamic systems in C++ may find this discussion beneficial.

iox
Messages
4
Reaction score
0
Hi all,

I'm having some problems trying to simulate a pendulum in C++. I am using x,y coordinates (and not angular coordinates, velocity...) to use the same method than all other dynamics in my program which are:
- Compute sum of all forces
- a = sum(F)
- v += a
- position += v

For the pendulum I'm getting quiet the good movement oscillating from right to left and reverse but the bob is "sinking" little by little.

First Method: Force = Tension + Gravity
Code:
// Weight Force
fWeight->set (0, mg);  // x,y of force vector

// Tension Force: T=-mg.cos(alpha) in tangential/radial coordinates
//       so back in my original coordinates
double Fx = mg*sin(alpha)*cos(alpha);
double Fy = -mg*cos(alpha)*cos(alpha);
fTension->set (Fx, Fy);

// Result Net Force
fResult->set (fWeight->xV() + fTension->xV(), fWeight->yV() + fTension->yV());

Second Method: Force = mg.sin (alpha)
Code:
fResult->set (mg*sin(alpha), AngleRad (alpha));

As I said the 2 methods give the same result: oscillating but falling...
Can you tell me if the way I am computing the total forces is correct ? If you notice something wrong tell me !

Thanks
 
Last edited:
Technology news on Phys.org
by falling do you mean the penduum swing is getting smaller and smaller if so then maybe you have a rounding error creeping in.

Are you using the Euler integration method?

Are you doing anything where you convert double to float and back again?
 
No I mean my pendulum is composed of 2 programming objects, a stick and a circle, and I am doing the calculation on the circle (on which I apply the forces and compute the new coordinates), and the circle is falling on my screen (y coordinate increases too fast). The swing movement seems to stay constant.
And yes maybe I have a problem of precision, I have to check that but I don't know how to do it ^^
But what I want to know first is if the way I compute the total force is correct.
 
Last edited:
That is a problem of your (simple) calculation method - try to use more advanced methods, like Euler integration. Alternatively, use polar coordinates - this avoids any movement of the pendulum by construction. You could do this in an implicit way if you "fix" the distance to the center in each step with some correction factor.
 
What are the limitation of my simple method ? Why is it not working in this case ? or In what cases it is not working ?
Because I have another example of a particle subjected to a centripetal force with an initial velocity. The trajectory is theoretically a circle and I have a spiral.
I will try to fix the distance as you said.
Thanks.
 
Consider a particle at some fixed point, with a known velocity. For the next step, you change the velocity, based on the acceleration, and move the point with the new velocity. But this is wrong - the new velocity will be reached after the time step only, in between you have something between the old and the new velocity. A better method would be to displace the point by the average of the new and the old velocity, for example.
"Euler integration" as keyword should lead to many different options to minimize those errors.
 
Thanks, I will try to play a little around that.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
2
Views
1K
Replies
14
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
7K
Replies
7
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K