- #1
franznietzsche
- 1,504
- 6
I'm trying to use the differential equations for the motion of a pendulum to create a program that predcits the pendulums position.
It uses the [tex] x = x_0 + v*t + 1/2a*t^s [/tex] and is written in C++.
the code is:
#include<iostream.h>
#include<stdlib.h>
#include<pendulumfunctions.hpp>
int main()
{
cout << "Enter a value for g: ";
cin >> g;
cout << "\nEnter an initial angular position:";
cin >> iang;
ang = iang;
y = ang;
cout << "\nEnter the pendulum length: ";
cin >> l;
cout << "\n";
system("PAUSE");
t = 0;
av = 0;
while (t<=10)
{
ang = y;
av = w;
cout << "Time: " << t << "\tVelocity: " << velocity(g, l, ang) << "\tPosition: " << position(g, l, ang) << endl;
t = t + (10.0/2500.0);
}
system("PAUSE");
return 0;
}
PendulumFunctions.hpp :
#include<math.h>
typedef double dou;
dou g, l, ang, iang, av, C, v, y, w, t;
dou aacceleration(dou g, dou l, dou ang) //Function for angular acceleration, radians/sec
{
dou aa;
aa = g/l*sin(ang);
return (aa);
}
dou avelocity(dou g, dou l, dou ang)
{
if (t==0)
return av;
else
{
w = av + (10.0/2500.0)*aacceleration(g, l, ang);
return (av);
}
}
dou velocity(dou g, dou l, dou ang)
{
v = avelocity(g, l, ang)*l;
return (v);
}
dou position(dou g, dou l, dou ang)
{
if (t==0)
return iang;
else
{
y = ang + 10.0/2500.0*avelocity(g, l, ang) + 0.5*(10.0/2500.0)*(10.0/2500.0)*aacceleration(g, l, ang);
return (y);
}
}
Where (10.0/2500.0) is the time increment from point to point, ang = angle, etc.
NOw my question is when i run this, the answer starts out cycling as normal, but then the maximum ampltude and period sloy increase until at the final postion t = 10 seconds, the pendulum has swung all the way out to -18 radians, something that makes no sense for a conservative system driven only by gravity. Can anyone help with this?
It uses the [tex] x = x_0 + v*t + 1/2a*t^s [/tex] and is written in C++.
the code is:
#include<iostream.h>
#include<stdlib.h>
#include<pendulumfunctions.hpp>
int main()
{
cout << "Enter a value for g: ";
cin >> g;
cout << "\nEnter an initial angular position:";
cin >> iang;
ang = iang;
y = ang;
cout << "\nEnter the pendulum length: ";
cin >> l;
cout << "\n";
system("PAUSE");
t = 0;
av = 0;
while (t<=10)
{
ang = y;
av = w;
cout << "Time: " << t << "\tVelocity: " << velocity(g, l, ang) << "\tPosition: " << position(g, l, ang) << endl;
t = t + (10.0/2500.0);
}
system("PAUSE");
return 0;
}
PendulumFunctions.hpp :
#include<math.h>
typedef double dou;
dou g, l, ang, iang, av, C, v, y, w, t;
dou aacceleration(dou g, dou l, dou ang) //Function for angular acceleration, radians/sec
{
dou aa;
aa = g/l*sin(ang);
return (aa);
}
dou avelocity(dou g, dou l, dou ang)
{
if (t==0)
return av;
else
{
w = av + (10.0/2500.0)*aacceleration(g, l, ang);
return (av);
}
}
dou velocity(dou g, dou l, dou ang)
{
v = avelocity(g, l, ang)*l;
return (v);
}
dou position(dou g, dou l, dou ang)
{
if (t==0)
return iang;
else
{
y = ang + 10.0/2500.0*avelocity(g, l, ang) + 0.5*(10.0/2500.0)*(10.0/2500.0)*aacceleration(g, l, ang);
return (y);
}
}
Where (10.0/2500.0) is the time increment from point to point, ang = angle, etc.
NOw my question is when i run this, the answer starts out cycling as normal, but then the maximum ampltude and period sloy increase until at the final postion t = 10 seconds, the pendulum has swung all the way out to -18 radians, something that makes no sense for a conservative system driven only by gravity. Can anyone help with this?