1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <fstream>
#include <cmath>
//#include "dislin.h"
class Spring {
public:
float iK, iM, iPosition, iVelocity;
Spring(float aK, float aM, float aPosition, float aVelocity) :
iK(aK), iM(aM), iPosition(aPosition), iVelocity(aVelocity) { }
};
class Trajectory{
float iX[100], iV[100], iSteps;
public:
void propagate (Spring aSpring, int aSteps, float aDt) {
iSteps = aSteps;
iX[0] = aSpring.iPosition;
iV[0] = aSpring.iVelocity;
for (int i = 1; i < aSteps; i++) {
iX[i] = aSpring.iPosition + aSpring.iVelocity * aDt;
iV[i] = aSpring.iVelocity - aSpring.iK / aSpring.iM * aSpring.iPosition *aDt;
aSpring.iPosition = iX[i];
aSpring.iVelocity = iV[i];
}
}
void plot( ) {
//metafl("XWIN");
//qplot(iX ,iV ,iSteps);
ofstream myfile;
myfile.open ("spring.dat");
myfile << iX ,iV ,iSteps << endl;
myfile.close();
}
};
int main( ) {
Spring S1(1.0, 1.0, 1.0, 0.0);
Trajectory T1;
T1.propagate(S1, 80, 2*M_PI/80);
T1.plot( );
}
|