# include # include # include # include int main ( ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for SPRING2. // // Discussion: // // This program computes the position and velocity of a particle // on a spring. // // Hooke's law for a spring observes that the restoring force is // proportional to the displacement: F = - k x // // Newton's law relates the force to acceleration: F = m a // // Putting these together, we have // // m * d^2 x/dt^2 = - k * x // // We can add a damping force with coefficient c: // // m * d^2 x/dt^2 = - k * x - c * dx/dt // // If we write this as a pair of first order equations for (x,v), we have // // dx/dt = v // m * dv/dt = - k * x - c * v // // and now we can approximate these values for small time steps. // // The program prints the values T, X, and V for each step. // It is worthwhile trying to turn that output into a plot. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 17 May 2012 // // Author: // // John Burkardt // // Parameters: // // None // { float c; float dt; int i; int j; float k; float m; int n; int p; float t; float t_final; float v; float v_old; float x; float x_old; char z[21]; timestamp ( ); printf ( "\n" ); printf ( "SPRING_ODE\n" ); printf ( " C version\n" ); printf ( " Approximate the solution of a spring equation.\n" ); printf ( " Display the solution with line printer graphics.\n" ); printf ( "\n" ); // // Data // m = 1.0; k = 1.0; c = 0.3; t_final = 20.0; n = 100; dt = t_final / ( float ) ( n ); // // Initial conditions. // x = 1.0; v = 0.0; // // Compute the approximate solution at equally spaced times. // for ( i = 0; i <= n; i++ ) { x_old = x; v_old = v; t = ( float ) ( i ) * t_final / ( float ) ( n ); x = x_old + dt * v_old; v = v_old + ( dt / m ) * ( - k * x_old - c * v_old ); // // Approximate the position of X in [-1,+1] to within 1/10. // printf ( "%g %g %g\n", t, x, v ); } // // Terminate. // printf ( "\n" ); printf ( "SPRING2:\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } //****************************************************************************80 void timestamp ( void ) //****************************************************************************80 // // Purpose: // // TIMESTAMP prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 24 September 2003 // // Author: // // John Burkardt // // Parameters: // // None // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; size_t len; time_t now; now = time ( NULL ); tm = localtime ( &now ); len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); fprintf ( stdout, "%s\n", time_buffer ); return; # undef TIME_SIZE }