simple data file

Hi all,

The following is a code for solving the harmonic oscillator with the Euler Method. My problem is that in lines 30-33 I want to write the data into the file "spring.dat" but it doesn't work at all. I don't see any major errors though. Anybody got an idea what's wrong here?

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( );
}
Last edited on
Looking at those few lines only,

myfile << iX ,iV ,iSteps << endl;


You want:
myfile << iX << iV << iSteps << endl;


Last edited on
OK, but the compiler starts to complain in line 30.
It doesn't look like it should be a problem to me. If you comment out 31-33 and still get errors, then the problem is beyond my knowledge.

Could it be that iX and iV are float arrays, and you don't reference the address in your "myfile <<" ?
Last edited on
I am not sure if it is necessary to do this. I changed line 32 now to

myfile << iX[i] << " " << iV[i] << endl;

but of course this didn't work. Slightly frustrating.
maybe somthing like this ???

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <cmath>
//#include "dislin.h"
#define M_PI 3.14
//using namespace std;
class Spring {
	public:
		float iK, iM, iPosition, iVelocity;
		Spring()
		{

		}
		Spring(float aK, float aM, float aPosition, float aVelocity) 
		{
			iK=aK;
			iM=aM;
			iPosition = aPosition;
			iVelocity = aVelocity;
		}
		//	iK(aK), iM(aM), iPosition(aPosition), iVelocity(aVelocity) {  }
};

class Trajectory{
	float iX[100], iV[100], iSteps;
	public:
		Trajectory()
		{
			
		}
		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);
			std::ofstream myfile;
                        myfile.open("spring.dat");
                       	for (int i = 0; i <= iSteps; i++)
			{			myfile<<"-------------------------------------------------------------------------------------"<<std::endl;
						myfile << "|iX = : "<<iX[i] <<" | iV = :  "<<iV[i]<<"  |Number : " <<i <<"|"<< std::endl;
                        myfile<<"-------------------------------------------------------------------------------------"<<std::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( );
}

Last edited on
That's nice, thanks. It works now. I also forgot the loop for the data file, yay. May I ask why you commented out the namespace directive and instead used the std:: everywhere? Shouldn't this have worked before? I am a little bit puzzled.
I just did that to show you that you left that out and where it was needed.
Last edited on
I always thought "using namespace std" or "std::" is equivalent?
Last edited on
It is in your case but you left it out.
Other wise your code would have ran with a few changes to the plot function in your trajectory class.
Last edited on
Oh, OK, I get it.
Topic archived. No new replies allowed.