/* -------------------------------------------------------------------- // Figure 15.28: Torque program: The first step. // Figure 15.36: Torque program, step 2: The try_once() function. // -------------------------------------------------------------------- */ #include "tools.h" #define TRIALS 1000 void try_once( stream s ); void main( void ) { stream fout; /* For the expermental data.*/ int k; /* Loop counter for the process repetition. */ say( "Measuring Torque" ); say( "\n Opening file theta.out for output." ); fout = fopen( "theta.out", "w" ); if (fout == NULL) fatal( "Cannot open theta.out." ); srand( time(NULL) ); /* seed random number generator */ say( "\n Ready to begin torque experiment." ); for (k = 0; k < TRIALS; ++k) try_once( fout ); bye(); } /* ----------------------------------------------------------------------------- // Carry out one experimental trial. // Report an error is result is outside of expected range, otherwise // write the force and resulting twist angle to a file. */ #define MINF 10.0 #define MAXF 30.0 #define EPS .1 /* permitted variation from theoretical value */ #define G 12E06 /* psi, modulus of rigidity for steel */ #define R 0.125 /* inches, radius of shaft */ #define L 12.0 /* inches, length of shaft */ #define b 3.0 /* inches, effective length of handle */ void try_once( stream s ) { const double J = PI * pow( R,4 ) / 2; /* polar moment of inertia. */ double F; /* Force: randomly selected input for experiment.*/ double theta; /* Twist angle; experimental output. */ double theta_t; /* Twist angle; theoretical value. */ F = randy() * (MAXF-MINF) + MINF; /* Get and scale random force.*/ printf( "%g\n", F); /* Send force to apparatus.*/ scanf( "%lf", &theta); /* Receive measured result.*/ theta_t = (F * b * L) / (J * G); if (theta > theta_t - EPS && theta < theta_t + EPS) fprintf( s, "%f %f\n", F, theta ); else say( " Angle %g outside possible range for force %g.", theta, F ); }