# Pendulum in C++

1. Jan 16, 2013

### iox

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 (Text):

// 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 (Text):

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: Jan 16, 2013
2. Jan 16, 2013

### Staff: Mentor

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?

3. Jan 16, 2013

### iox

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 dont 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: Jan 16, 2013
4. Jan 16, 2013

### Staff: Mentor

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.

5. Jan 16, 2013

### iox

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.

6. Jan 16, 2013

### Staff: Mentor

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.

7. Jan 16, 2013

### iox

Thanks, I will try to play a little around that.