# include # include # include # include using namespace std; # include "test_interp_fun.hpp" //****************************************************************************80 double p00_fun ( int prob, double x ) //****************************************************************************80 // // Purpose: // // P00_FUN evaluates the function for any problem. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 February 2012 // // Author: // // John Burkardt // // Parameters: // // Input, int PROB, the number of the desired test problem. // // Input, double X, the point at which the function // is to be evaluated. // // Output, double P00_FUN, the value of the function at X. // { double value; if ( prob == 1 ) { value = p01_fun ( x ); } else if ( prob == 2 ) { value = p02_fun ( x ); } else if ( prob == 3 ) { value = p03_fun ( x ); } else if ( prob == 4 ) { value = p04_fun ( x ); } else if ( prob == 5 ) { value = p05_fun ( x ); } else { cerr << "\n"; cerr << "P00_FUN - Fatal error!\n"; cerr << " Illegal problem number = " << prob << "\n"; exit ( 1 ); } return value; } //****************************************************************************80 void p00_lim ( int prob, double &a, double &b ) //****************************************************************************80 // // Purpose: // // P00_LIM returns the limits of the approximation interval for any problem. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 February 2012 // // Author: // // John Burkardt // // Parameters: // // Input, int PROB, the number of the desired test problem. // // Output, double &A, &B, the lower and upper limits of // the approximation interval. // { if ( prob == 1 ) { p01_lim ( a, b ); } else if ( prob == 2 ) { p02_lim ( a, b ); } else if ( prob == 3 ) { p03_lim ( a, b ); } else if ( prob == 4 ) { p04_lim ( a, b ); } else if ( prob == 5 ) { p05_lim ( a, b ); } else { cerr << "\n"; cerr << "P00_LIM - Fatal error!\n"; cerr << " Illegal problem number = " << prob << "\n"; exit ( 1 ); } return; } //****************************************************************************80 int p00_prob_num ( ) //****************************************************************************80 // // Purpose: // // P00_PROB_NUM returns the number of problems. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 February 2012 // // Author: // // John Burkardt // // Parameters: // // Output, int PROB_NUM, the number of problems. // { int prob_num; prob_num = 5; return prob_num; } //****************************************************************************80 void p00_story ( int prob ) //****************************************************************************80 // // Purpose: // // P00_STORY prints the "story" for any problem. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 February 2012 // // Author: // // John Burkardt // // Parameters: // // None // { if ( prob == 1 ) { p01_story ( ); } else if ( prob == 2 ) { p02_story ( ); } else if ( prob == 3 ) { p03_story ( ); } else if ( prob == 4 ) { p04_story ( ); } else if ( prob == 5 ) { p05_story ( ); } else { cerr << "\n"; cerr << "P00_STORY - Fatal error!\n"; cerr << " Unexpected input value of PROB.\n"; exit ( 1 ); } return; } //****************************************************************************80 string p00_title ( int prob ) //****************************************************************************80 // // Purpose: // // P00_TITLE returns the title of any problem. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 February 2012 // // Author: // // John Burkardt // // Parameters: // // Input, int PROB, the number of the desired test problem. // // Output, string TITLE, the title of the problem. // { string title; if ( prob == 1 ) { title = p01_title ( ); } else if ( prob == 2 ) { title = p02_title ( ); } else if ( prob == 3 ) { title = p03_title ( ); } else if ( prob == 4 ) { title = p04_title ( ); } else if ( prob == 5 ) { title = p05_title ( ); } else { cerr << "\n"; cerr << "P00_TITLE - Fatal error!\n"; cerr << " Illegal problem number = " << prob << "\n"; exit ( 1 ); } return title; } //****************************************************************************80 double p01_fun ( double x ) //****************************************************************************80 // // Purpose: // // P01_FUN evaluates the function for problem 1. // // Discussion: // // This is a famous example, due to Runge. If equally spaced // abscissas are used, the sequence of interpolating polynomials Pn(X) // diverges, in the sense that the max norm of the difference // between Pn(X) and F(X) becomes arbitrarily large as N increases. // // Dimension: // // N = 1 // // Interval: // // -5 <= X <= 5 // // Function: // // 1 / ( X * X + 1 ) // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Input, double X, the point at which the function // is to be evaluated. // // Output, double P01_FUN, the value of the function at X. // { double value; value = 1.0 / ( x * x + 1.0 ); return value; } //****************************************************************************80 void p01_lim ( double &a, double &b ) //****************************************************************************80 // // Purpose: // // P01_LIM returns the limits of the approximation interval for problem 1. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Output, double &A, &B, the limits of the interval // of approximation. // { a = -5.0; b = 5.0; return; } //****************************************************************************80 void p01_story ( ) //****************************************************************************80 // // Purpose: // // P01_STORY prints the "story" for problem 1. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // None // { cout << "\n"; cout << " This is a famous example, due to Runge. If equally spaced\n"; cout << " abscissas are used, the sequence of interpolating polynomials Pn(X)\n"; cout << " diverges, in the sense that the max norm of the difference\n"; cout << " between Pn(X) and F(X) becomes arbitrarily large as N increases.\n"; return; } //****************************************************************************80 string p01_title ( ) //****************************************************************************80 // // Purpose: // // P01_TITLE returns the title of problem 1. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Output, string TITLE, the title of the problem. // { string title; title = "Runge example, f(x) = 1 / ( x * x + 1 ), [-5,5]"; return title; } //****************************************************************************80 double p02_fun ( double x ) //****************************************************************************80 // // Purpose: // // P02_FUN evaluates the function for problem 2. // // Discussion: // // This example is due to Bernstein. If equally spaced // abscissas are used, the sequence of interpolating polynomials Pn(X) // only converges to F(X) at -1, 0, and 1. // // Dimension: // // N = 1 // // Interval: // // -1 <= X <= 1 // // Function: // // ABS ( X ) // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Input, double X, the point at which the function // is to be evaluated. // // Output, double P02_FUN, the value of the function at X. // { double value; if ( x < 0.0 ) { value = - x; } else { value = x; } return value; } //****************************************************************************80 void p02_lim ( double &a, double &b ) //****************************************************************************80 // // Purpose: // // P02_LIM returns the limits of the approximation interval for problem 2. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Output, double &A, &B, the limits of the interval // of approximation. // { a = -1.0; b = 1.0; return; } //****************************************************************************80 void p02_story ( ) //****************************************************************************80 // // Purpose: // // P02_STORY prints the "story" for problem 2. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // None // { cout << "\n"; cout << " This example is due to Bernstein.\n"; cout << " If equally spaced abscissas are used, the sequence of interpolating\n"; cout << " polynomials Pn(X) only converges to F(X) at -1, 0, and 1.\n"; return; } //****************************************************************************80 string p02_title ( ) //****************************************************************************80 // // Purpose: // // P02_TITLE returns the title of problem 2. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Output, string TITLE, the title of the problem. // { string title; title = "Bernstein example, f(x) = abs ( x ), [-1,1]"; return title; } //****************************************************************************80 double p03_fun ( double x ) //****************************************************************************80 // // Purpose: // // P03_FUN evaluates the function for problem 3. // // Discussion: // // This is a step function with a jump from 0 to 1 at 0. // // Dimension: // // N = 1 // // Interval: // // -1 <= X <= 1 // // Function: // // F(X) = 0 if X < 0 // 1 if 0 < X. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Input, double X, the point at which the function // is to be evaluated. // // Output, double P03_FUN, the value of the function at X. // { double value; if ( x < 0.0 ) { value = 0.0; } else { value = 1.0; } return value; } //****************************************************************************80 void p03_lim ( double &a, double &b ) //****************************************************************************80 // // Purpose: // // P03_LIM returns the limits of the approximation interval for problem 3. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Output, double &A, &B, the limits of the interval // of approximation. // { a = -1.0; b = 1.0; return; } //****************************************************************************80 void p03_story ( ) //****************************************************************************80 // // Purpose: // // P03_STORY prints the "story" for problem 3. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // None // { cout << "\n"; cout << " The step function is discontinuous.\n"; cout << " Attempts to approximate this function by high degree polynomials\n"; cout << " will rapidly diverge.\n"; return; } //****************************************************************************80 string p03_title ( ) //****************************************************************************80 // // Purpose: // // P03_TITLE returns the title of problem 3. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Output, string TITLE, the title of the problem. // { string title; title = "Step function, f jumps from 0 to 1 at 0."; return title; } //****************************************************************************80 double p04_fun ( double x ) //****************************************************************************80 // // Purpose: // // P04_FUN evaluates the function for problem 4. // // Discussion: // // This function is highly oscillatory near X = 0. // // Dimension: // // N = 1 // // Interval: // // 0 <= X <= 1 // // Function: // // F(X) = sqrt ( x * ( 1 - x ) ) * sin ( 2.1 * pi / ( x + 0.05 ) ) // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Input, double X, the point at which the function // is to be evaluated. // // Output, double P04_FUN, the value of the function at X. // { double pi = 3.141592653589793; double value; value = sqrt ( x * ( 1.0 - x ) ) * sin ( 2.1 * pi / ( x + 0.05 ) ); return value; } //****************************************************************************80 void p04_lim ( double &a, double &b ) //****************************************************************************80 // // Purpose: // // P04_LIM returns the limits of the approximation interval for problem 4. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Output, double &A, &B, the limits of the interval // of approximation. // { a = 0.0; b = 1.0; return; } //****************************************************************************80 void p04_story ( ) //****************************************************************************80 // // Purpose: // // P04_STORY prints the "story" for problem 4. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // None // { cout << "\n"; cout << " The Doppler function is continuous, but highly oscillatory\n"; cout << " near the value X = 0.\n"; return; } //****************************************************************************80 string p04_title ( ) //****************************************************************************80 // // Purpose: // // P04_TITLE returns the title of problem 4. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 August 2011 // // Author: // // John Burkardt // // Parameters: // // Output, string TITLE, the title of the problem. // { string title; title = "Doppler function, highly oscillatory near X = 0."; return title; } //****************************************************************************80 double p05_fun ( double x ) //****************************************************************************80 // // Purpose: // // P05_FUN evaluates the function for problem 5. // // Discussion: // // This example is difficult to interpolate because it has a piecewise // definition, and the character of the function changes dramatically // from piece to piece. // // Dimension: // // N = 1 // // Interval: // // 0 <= X <= 10 // // Function: // // F(X) = max ( sin(x) + sin(x^2), 1 - abs(x-5)/5 ) // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 February 2012 // // Author: // // John Burkardt // // Parameters: // // Input, double X, the point at which the function // is to be evaluated. // // Output, double P05_FUN, the value of the function at X. // { double value; value = r8_max_copy ( sin ( x ) + sin ( x * x ), 1.0 - r8_abs_copy ( x - 5.0 ) / 5.0 ); return value; } //****************************************************************************80 void p05_lim ( double &a, double &b ) //****************************************************************************80 // // Purpose: // // P05_LIM returns the limits of the approximation interval for problem 5. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 February 2012 // // Author: // // John Burkardt // // Parameters: // // Output, double &A, &B, the limits of the interval // of approximation. // { a = 0.0; b = 10.0; return; } //****************************************************************************80 void p05_story ( ) //****************************************************************************80 // // Purpose: // // P05_STORY prints the "story" for problem 5. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 February 2012 // // Author: // // John Burkardt // // Parameters: // // None // { cout << "\n"; cout << " This example is very difficult to interpolate.\n"; cout << " It is essentially defined as a piecewise function,\n"; cout << " alternating between a straight line and a sinusoidal curve.\n"; return; } //****************************************************************************80 string p05_title ( ) //****************************************************************************80 // // Purpose: // // P05_TITLE returns the title of problem 5. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 February 2012 // // Author: // // John Burkardt // // Parameters: // // Output, string TITLE, the title of the problem. // { string title; title = "Rabbit ears, f(x) = max(sin(x)+sin(x^2),1-abs(x-5)/5), [0,10]"; return title; } //****************************************************************************80 double r8_abs_copy ( double x ) //****************************************************************************80 // // Purpose: // // R8_ABS returns the absolute value of an R8. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 November 2006 // // Author: // // John Burkardt // // Parameters: // // Input, double X, the quantity whose absolute value is desired. // // Output, double R8_ABS, the absolute value of X. // { double value; if ( 0.0 <= x ) { value = + x; } else { value = - x; } return value; } //****************************************************************************80 double r8_max_copy ( double x, double y ) //****************************************************************************80 // // Purpose: // // R8_MAX returns the maximum of two R8's. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 18 August 2004 // // Author: // // John Burkardt // // Parameters: // // Input, double X, Y, the quantities to compare. // // Output, double R8_MAX, the maximum of X and Y. // { double value; if ( y < x ) { value = x; } else { value = y; } return value; }