# include # include # include # include # include "quad_serial.h" int main ( int argc, char *argv[] ); double f ( double x ); double cpu_time ( ); void timestamp ( ); /******************************************************************************/ int main ( int argc, char *argv[] ) /******************************************************************************/ /* Purpose: quad_serial_test() tests quad_serial(). Licensing: This code is distributed under the MIT license. Modified: 18 October 2024 Author: John Burkardt */ { double a = 0.0; double b = 10.0; double error; double exact = 0.49936338107645674464; int n = 10000000; double q; double wtime; double wtime1; double wtime2; timestamp ( ); printf ( "\n" ); printf ( "quad_serial():\n" ); printf ( " C version\n" ); printf ( " Estimate the integral of f(x) from A to B.\n" ); printf ( " f(x) = 50 / ( pi * ( 2500 * x * x + 1 ) ).\n" ); printf ( "\n" ); printf ( " A = %f\n", a ); printf ( " B = %f\n", b ); printf ( " N = %d\n", n ); wtime1 = cpu_time ( ); q = quad_serial ( f, a, b, n ); wtime2 = cpu_time ( ); error = fabs ( q - exact ); wtime = wtime2 - wtime1; printf ( "\n" ); printf ( " Exact = %24.16f\n", exact ); printf ( " Estimate = %24.16f\n", q ); printf ( " Error = %e\n", error ); printf ( " Time = %f\n", wtime ); /* Terminate. */ printf ( "\n" ); printf ( "quad_serial():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /*******************************************************************************/ double f ( double x ) /*******************************************************************************/ /* Purpose: F evaluates the function. Licensing: This code is distributed under the MIT license. Modified: 18 October 2024 Author: John Burkardt Input: double X, the argument. Output: double F, the value of the function. */ { double pi = 3.141592653589793; double value; value = 50.0 / ( pi * ( 2500.0 * x * x + 1.0 ) ); return value; } /*******************************************************************************/ double cpu_time ( void ) /*******************************************************************************/ /* Purpose: CPU_TIME reports the total CPU time for a program. Modified: 27 September 2005 Author: John Burkardt Parameters: Output, double CPU_TIME, the current total elapsed CPU time in second. */ { double value; value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC; return value; } /******************************************************************************/ void timestamp ( void ) /******************************************************************************/ /* 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 MIT license. Modified: 24 September 2003 Author: John Burkardt Parameters: None */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }