/*************************************************************************/
/* Program file name: motion.c                                           */
/*                                                                       */
/* © Tao Pang 2006                                                       */
/*                                                                       */
/* Last modified: June 2, 2006                                           */
/*                                                                       */
/* (1) This C program is created for the book, "An Introduction to       */
/*     Computational Physics, 2nd Edition," written by Tao Pang and      */
/*     published by Cambridge University Press on January 19, 2006.      */
/*                                                                       */
/* (2) No warranties, express or implied, are made for this program.     */
/*                                                                       */
/*************************************************************************/

#include <stdio.h>
#include <math.h>
#define n 10000
#define j 200

/* An example of studying the motion of a particle in
   one dimension under an elastic force. */

main() {
  int i;
  double x[n+1], v[n+1];

/* Assign time step and initial position and velocity */

  double pi = 4*atan(1);
  double dt = 2*pi/n;
  x[0] = 0;
  v[0] = 1;

/* Calculate other position and velocity recursively */

  for (i=0; i<n; i++) {
    x[i+1] = x[i]+v[i]*dt;
    v[i+1] = v[i]-x[i]*dt;
  }

/* Output the result in every j time steps */

  double t = 0;
  double jdt = j*dt;
  for (i=0; i<n; i+=j) {
    printf("%16.8f %16.8f %16.8f\n", t, x[i], v[i]);
    t += jdt;
  }
}
