/************************* Program 3.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 101 main() /* Simplest predictor-corector algorithm applied to a particle in one dimension under an elastic force. Copyright (c) Tao Pang 1997. */ { int i,n,m; double dt,pi; double t[NMAX],x[NMAX],v[NMAX]; n = NMAX; m = 5; pi = 4*atan(1); dt = 2*pi/100; x[0] = 0; t[0] = 0; v[0] = 1; for (i = 0; i < n-1; ++i) { t[i+1] = (i+1)*dt; /* Predictor for position and velocity */ x[i+1] = x[i]+v[i]*dt; v[i+1] = v[i]-x[i]*dt; /* Corrector for position and velocity */ x[i+1] = x[i]+(v[i]+v[i+1])*dt/2; v[i+1] = v[i]-(x[i]+x[i+1])*dt/2; if (i == 0 || (i+1)%m == 0) printf("%16.8lf %16.8lf %16.8lf\n", t[i],x[i],v[i]); } }