double newton ( double x0, double f ( double x ), double fp ( double x ) ) { int step; double fx1, x1; // // Initialization. // step = 0; cout << step << " " << x0 << " " << f ( x0 ) << "\n"; // // Iteration loop. // while ( true ) { x1 = x0 - f ( x0 ) / fp ( x0 ); fx1 = f ( x1 ); step = step + 1; cout << step << " " << x1 << " " << f ( x1 ) << "\n"; // // Can we accept the new point? // if ( fabs ( fx1 ) < 0.00000001 ) { break; } // // Quit if we take too many steps! // if ( 15 <= step ) { cerr << "NEWTON - Too many steps!\n"; exit ( 1 ); } // // Prepare for next step. // x0 = x1; } return x1; }