double secant ( double x0, double x1, double f ( double x ) ) { int step; double fx0, fx1, fx2, x2; // // Initialization. // step = 0; fx0 = f ( x0 ); fx1 = f ( x1 ); // // Iteration loop. // while ( true ) { x2 = ( fx1 * x0 - fx0 * x1 ) / ( fx1 - fx0 ); fx2 = f ( x2 ); step = step + 1; // // Can we accept the new point? // if ( fabs ( fx2 ) < 0.000001 ) { break; } // // Quit if we take too many steps! // if ( 25 <= step ) { cerr << "SECANT - Too many steps!\n"; exit ( 1 ); } // // Prepare for next step. // x0 = x1; fx0 = fx1; x1 = x2; fx1 = fx2; } return x2; }