#include #include #include using namespace std; int main() { double t, dt; double ts, y0, p0; int nsteps; string filename; cout << "Input initial position (y0):" << endl; cin >> y0; cout << "Input initial momentum:" << endl; cin >> p0; cout << "Input step (dt):" << endl; cin >> dt; // cout << "Input number of steps:" << endl; // cin >> nsteps; // Alternatively: double tmax; cout << "Maximum time (tmax):" << endl; cin >> tmax; nsteps = int(tmax/dt); cout << "Input File name: "<< endl; cin >> filename; //////////////////////////////////////////////////// double p = p0, y = y0; double pi = acos(-1.); double w = -4.*pi*pi; double ek, ep; ofstream fout; fout.open(filename.c_str()); if(!fout) { cout << "Error: Unable to open file" << endl; exit(-1); } for(int n = 1; n <= nsteps; n++){ y += p*dt; p += w*y*dt; ek = 0.5*p*p; ep = 2*pi*pi*y*y; fout << n*dt << " " << y << " " << p << " " << ek << " " << ep << " " << ek+ep << endl; } fout.close(); return 0; }