# include # include # include # include # include "zero_laguerre.h" 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 ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: zero_laguerre_test() tests zero_laguerre(). Licensing: This code is distributed under the MIT license. Modified: 30 March 2024 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "zero_laguerre_test():\n" ); printf ( " C version\n" ); printf ( " Test zero_laguerre().\n" ); test01 ( ); test02 ( ); test03 ( ); /* Terminate. */ printf ( "\n" ); printf ( "zero_laguerre_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void test01 ( ) /******************************************************************************/ /* 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; printf ( "\n" ); printf ( "test01():\n" ); printf ( " p(x)=(x+3)*(x+3)*(x-2)\n" ); zero_laguerre ( x0, degree, abserr, kmax, func01, &x, &ierror, &k ); printf ( "\n" ); if ( ierror != 0 ) { printf ( " Iteration failed with ierror = %d\n", ierror ); } else { printf ( " Iteration steps taken: %d\n", k ); printf ( " Estimated root X = %g\n", x ); printf ( " F(X) = %g\n", func01 ( x, 0 ) ); } return; } /******************************************************************************/ double func01 ( double x, int ider ) /******************************************************************************/ /* 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; } /******************************************************************************/ void test02 ( ) /******************************************************************************/ /* 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; printf ( "\n" ); printf ( "test02():\n" ); printf ( " p(x) = x^3 - 2x - 5\n" ); zero_laguerre ( x0, degree, abserr, kmax, func02, &x, &ierror, &k ); printf ( "\n" ); if ( ierror != 0 ) { printf ( " Iteration failed with ierror = %d\n", ierror ); } else { printf ( " Iteration steps taken: %d\n", k ); printf ( " Estimated root X = %g\n", x ); printf ( " F(X) = %g\n", func02 ( x, 0 ) ); } return; } /******************************************************************************/ double func02 ( double x, int ider ) /******************************************************************************/ /* 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; } /******************************************************************************/ void test03 ( ) /******************************************************************************/ /* 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; printf ( "\n" ); printf ( "test03():\n" ); printf ( " p(x) = x^5 + 2x^4 + 3x^3 + 4x^2 + 5x + 6\n" ); zero_laguerre ( x0, degree, abserr, kmax, func03, &x, &ierror, &k ); printf ( "\n" ); if ( ierror != 0 ) { printf ( " Iteration failed with ierror = %d\n", ierror ); } else { printf ( " Iteration steps taken: %d\n", k ); printf ( " Estimated root X = %g\n", x ); printf ( " F(X) = %g\n", func03 ( x, 0 ) ); } return; } /******************************************************************************/ double func03 ( double x, int ider ) /******************************************************************************/ /* 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; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # 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 }