#include #include #include #include using namespace std; #define PI (3.141592653589793) #define TIME_SELECTION 0.5 // Name: Daniel Summerhays // Date: Sun Mar 28 18:12:43 EST 2004 //double func(double theta) //{ // return sqrt(2g/L * cos(theta)); //} int main(int argc, char * argv[]) { string filename = "output.dat"; double error, trueSoln; ofstream fout(filename.c_str()); // numeric parameters double a = 0.; double b = TIME_SELECTION+1; int n; double h, t; // variables double theta, y; // y = theta' double preciseValue; // problem constants double c = 0.; double m = 1.; double gL = 6./11.; double gamma = 1.; double omega = sqrt(gL); theta = 1.; y = 0.; h = .000001; n = (int)((b-a)/h); for(int i = 1; i <= n+1; i++) { theta = theta + h*y; y = y + h*(-c/m*y - gL*sin(theta) + gamma*cos(omega*t)); t = a + i*h; if(t == TIME_SELECTION) preciseValue = theta; } for(h = TIME_SELECTION; h >= TIME_SELECTION/((double)(2<<5)); h /= 2){ //double h = .1; n = (int)((b-a)/h); theta = 1.; y = 0.; t = a; // fout << "[[" << t << "," << theta << "]" << endl; cout << "[[" << t << "," << theta << "]" << endl; // fout << "[[" << t << "," << theta << "]"; // cout << "[[" << t << "," << theta << "]"; for(int i = 1; i <= n+1; i++) { theta = theta + h*y; y = y + h*(-c/m*y - gL*sin(theta) + gamma*cos(omega*t)); t = a + i*h; cout << ",[" << t << "," << theta << "]" << endl; if(t == TIME_SELECTION){ //trueSoln = cos(sqrt(gL)*t); error = fabs(theta - preciseValue); cout << endl << "error/n: " << error/(double)n << endl; fout << h << " " << error/(double)n << endl; break; } //if(t == TIME_SELECTION){ // trueSoln = cos(sqrt(gL)*t); // error = theta - trueSoln; // cout << endl << "error/n: " << error/(double)n << endl; // fout << h << " " << error/(double)n << endl; // break; //} } // cout << "]"; // fout << "]"; } return 0; }