/************************* Program 1.1 ****************************/ /* */ /************************************************************************/ /* Please Note: */ /* */ /* (1) This computer program is written by Tao Pang in conjunction with */ /* his book, "An Introduction to Computational Physics," published */ /* by Cambridge University Press in 1997. */ /* */ /* (2) No warranties, express or implied, are made for this program. */ /* */ /************************************************************************/ #include #include #define NMAX 10001 #define MMAX 500 main() /* Motion of a particle under the force f(x) = -x. N is the number of time steps and M is the time steps skipped between two output points. Copyright (c) Tao Pang 1997. */ { int i; double pi, dt; double t[NMAX], x[NMAX], v[NMAX]; /* Assign constants, initial position, and initial velocity */ pi = 4.0 * atan(1.0); dt = 2.0*pi / (NMAX - 1.0); t[0] = x[0] = 0.0; v[0] = 1.0; /* Recursion for position and velocity */ for (i = 0; i < NMAX; i++) { t[i+1] = dt * (i+1); x[i+1] = x[i] + v[i] * dt; v[i+1] = v[i] - x[i] * dt; if ((i+MMAX) % MMAX == 0) printf("%16.8f %16.8f %16.8f\n", t[i], x[i], v[i]); } }