Simulating a Pendulum in C++: Seeking Assistance

In summary, the conversation discusses the problem of simulating a pendulum in C++ using two different methods to calculate the total force. The results show an oscillating movement but the pendulum is gradually falling. The limitation of the simple method is pointed out and the suggestion to use Euler integration or polar coordinates is made to improve the accuracy. The conversation ends with the agreement to try out different options to minimize the errors.
  • #1
iox
4
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
  • #2
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
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:
  • #4
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
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
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
Thanks, I will try to play a little around that.
 

Related to Simulating a Pendulum in C++: Seeking Assistance

1. How can I simulate a pendulum using C++?

To simulate a pendulum in C++, you can use the principles of physics and mathematical equations to calculate the position and velocity of the pendulum at each time step. This can be done by using a loop to update the pendulum's position and velocity over time.

2. What are the key components to consider when simulating a pendulum in C++?

The key components to consider when simulating a pendulum in C++ include the length of the pendulum, the mass of the pendulum bob, the initial angle and velocity, and the gravitational force acting on the pendulum. These factors will affect the movement and behavior of the pendulum.

3. How do I visualize the simulation of a pendulum in C++?

You can visualize the simulation of a pendulum in C++ by plotting the position of the pendulum over time using a graphing library or by using a 3D graphics library to display the movement of the pendulum in a virtual environment.

4. Can I add friction or air resistance to the pendulum simulation?

Yes, you can add friction or air resistance to the pendulum simulation by incorporating these forces into the equations used to calculate the position and velocity of the pendulum. This will result in a more realistic simulation of the pendulum's movement.

5. Are there any helpful resources or libraries for simulating a pendulum in C++?

Yes, there are several helpful resources and libraries for simulating a pendulum in C++. Some popular libraries include OpenGL for 3D graphics, SFML for 2D graphics, and ODE for physics simulations. There are also online tutorials and code repositories available to assist with the implementation of a pendulum simulation in C++.

Similar threads

Replies
1
Views
180
  • Introductory Physics Homework Help
Replies
9
Views
793
  • Introductory Physics Homework Help
Replies
3
Views
1K
Replies
8
Views
1K
  • Mechanics
Replies
2
Views
931
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Advanced Physics Homework Help
Replies
7
Views
4K
  • Introductory Physics Homework Help
Replies
7
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
2K
Back
Top