# include # include # include # include # include using namespace std; # include "zero_laguerre.hpp" int main ( ); void test01 ( ); double func01 ( double x, int ider ); void test02 ( ); double func02 ( double x, int ider ); void test03 ( ); double func03 ( double x, int ider ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // zero_laguerre_test() tests zero_laguerre(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 March 2024 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "zero_laguerre_test():\n"; cout << " C++ version\n"; cout << " Test zero_laguerre().\n"; test01 ( ); test02 ( ); test03 ( ); // // Terminate. // cout << "\n"; cout << "zero_laguerre_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void test01 ( ) //****************************************************************************80 // // Purpose: // // test01() runs the tests on a polynomial function. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 March 2024 // // Author: // // John Burkardt // { double abserr; int degree; int ierror; int k; int kmax; double x; double x0; // // Give a starting point. // x0 = 1.0; // // The polynomial degree. // degree = 3; // // Set the error tolerance. // abserr = 0.00001; // // KMAX is the maximum number of iterations. // kmax = 30; cout << "\n"; cout << "test01():\n"; cout << " p(x)=(x+3)*(x+3)*(x-2)\n"; zero_laguerre ( x0, degree, abserr, kmax, func01, x, ierror, k ); cout << "\n"; if ( ierror != 0 ) { cout << " Iteration failed with ierror = " << ierror << "\n"; } else { cout << " Iteration steps taken: " << k << "\n"; cout << " Estimated root X = " << x << "\n"; cout << " F(X) = " << func01 ( x, 0 ) << "\n"; } return; } //****************************************************************************80 double func01 ( double x, int ider ) //****************************************************************************80 // // Purpose: // // func01() computes the function value for the first test. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 March 2024 // // Author: // // John Burkardt // // Input: // // double X, the point at which the evaluation is to take place. // // int IDER, specifies what is to be evaluated: // 0, evaluate the function. // 1, evaluate the first derivative. // 2, evaluate the second derivative. // // Output: // // double FUNC01, the value of the function or derivative. // { double value; if ( ider == 0 ) { value = ( x + 3.0 ) * ( x + 3.0 ) * ( x - 2.0 ); } else if ( ider == 1 ) { value = ( x + 3.0 ) * ( 3.0 * x - 1.0 ); } else if ( ider == 2 ) { value = 6.0 * x + 8.0; } return value; } //****************************************************************************80 void test02 ( ) //****************************************************************************80 // // Purpose: // // test02() runs the tests on the Newton polynomial. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 March 2024 // // Author: // // John Burkardt // { double abserr; int degree; int ierror; int k; int kmax; double x; double x0; // // Give a starting point. // x0 = 1.0; // // The polynomial degree. // degree = 3; // // Set the error tolerance. // abserr = 0.00001; // // KMAX is the maximum number of iterations. // kmax = 30; cout << "\n"; cout << "test02():\n"; cout << " p(x) = x^3 - 2x - 5\n"; zero_laguerre ( x0, degree, abserr, kmax, func02, x, ierror, k ); cout << "\n"; if ( ierror != 0 ) { cout << " Iteration failed with ierror = " << ierror << "\n"; } else { cout << " Iteration steps taken: " << k << "\n"; cout << " Estimated root X = " << x << "\n"; cout << " F(X) = " << func02 ( x, 0 ) << "\n"; } return; } //****************************************************************************80 double func02 ( double x, int ider ) //****************************************************************************80 // // Purpose: // // func02() computes the function value for the Newton polynomial. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 March 2024 // // Author: // // John Burkardt // // Input: // // double X, the point at which the evaluation is to take place. // // int IDER, specifies what is to be evaluated: // 0, evaluate the function. // 1, evaluate the first derivative. // 2, evaluate the second derivative. // // Output: // // double FUNC02, the value of the function or derivative. // { double value; if ( ider == 0 ) { value = x*x*x - 2.0 * x - 5.0; } else if ( ider == 1 ) { value = 3.0 * x*x - 2.0; } else if ( ider == 2 ) { value = 6.0 * x; } return value; } //****************************************************************************80 void test03 ( ) //****************************************************************************80 // // Purpose: // // test03() runs the tests on the 123456 polynomial. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 March 2024 // // Author: // // John Burkardt // { double abserr; int degree; int ierror; int k; int kmax; double x; double x0; // // Give a starting point. // x0 = 1.0; // // The polynomial degree. // degree = 5; // // Set the error tolerance. // abserr = 0.00001; // // KMAX is the maximum number of iterations. // kmax = 30; cout << "\n"; cout << "test03():\n"; cout << " p(x) = x^5 + 2x^4 + 3x^3 + 4x^2 + 5x + 6\n"; zero_laguerre ( x0, degree, abserr, kmax, func03, x, ierror, k ); cout << "\n"; if ( ierror != 0 ) { cout << " Iteration failed with ierror = " << ierror << "\n"; } else { cout << " Iteration steps taken: " << k << "\n"; cout << " Estimated root X = " << x << "\n"; cout << " F(X) = " << func03 ( x, 0 ) << "\n"; } return; } //****************************************************************************80 double func03 ( double x, int ider ) //****************************************************************************80 // // Purpose: // // func03() computes the function value for the 123456 polynomial. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 March 2024 // // Author: // // John Burkardt // // Input: // // double X, the point at which the evaluation is to take place. // // int IDER, specifies what is to be evaluated: // 0, evaluate the function. // 1, evaluate the first derivative. // 2, evaluate the second derivative. // // Output: // // double FUNC03, the value of the function or derivative. // { double value; double x2; double x3; double x4; double x5; x2 = x * x; x3 = x2 * x; x4 = x3 * x; x5 = x4 * x; if ( ider == 0 ) { value = x5 + 2.0 * x4 + 3.0 * x3 + 4.0 * x2 + 5.0 * x + 6.0; } else if ( ider == 1 ) { value = 5.0 * x4 + 8.0 * x3 + 9.0 * x2 + 8.0 * x + 5.0; } else if ( ider == 2 ) { value = 20.0 * x3 + 24.0 * x2 + 18.0 * x + 8.0; } return value; } //****************************************************************************80 void timestamp ( ) //****************************************************************************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 MIT license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }