# include # include # include # include # include "test_interp_fun.h" /******************************************************************************/ double p00_fun ( int prob, double x ) /******************************************************************************/ /* Purpose: P00_FUN evaluates the function for any problem. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 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 { fprintf ( stderr, "\n" ); fprintf ( stderr, "P00_FUN - Fatal error!\n" ); fprintf ( stderr, " Illegal problem number = %d\n", prob ); exit ( 1 ); } return value; } /******************************************************************************/ void p00_lim ( int prob, double *a, double *b ) /******************************************************************************/ /* Purpose: P00_LIM returns the limits of the approximation interval for any problem. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 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 { fprintf ( stderr, "\n" ); fprintf ( stderr, "P00_LIM - Fatal error!\n" ); fprintf ( stderr, " Illegal problem number = %d\n", prob ); exit ( 1 ); } return; } /******************************************************************************/ int p00_prob_num ( ) /******************************************************************************/ /* Purpose: P00_PROB_NUM returns the number of problems. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, int PROB_NUM, the number of problems. */ { int prob_num; prob_num = 5; return prob_num; } /******************************************************************************/ void p00_story ( int prob ) /******************************************************************************/ /* Purpose: P00_STORY prints the "story" for any problem. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 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 { fprintf ( stderr, "\n" ); fprintf ( stderr, "P00_STORY - Fatal error!\n" ); fprintf ( stderr, " Illegal problem number = %d\n", prob ); exit ( 1 ); } return; } /******************************************************************************/ void p00_title ( int prob, char *title ) /******************************************************************************/ /* Purpose: P00_TITLE returns the title of any problem. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Input, int PROB, the number of the desired test problem. Output, char *TITLE, the title of the problem. */ { if ( prob == 1 ) { p01_title ( title ); } else if ( prob == 2 ) { p02_title ( title ); } else if ( prob == 3 ) { p03_title ( title ); } else if ( prob == 4 ) { p04_title ( title ); } else if ( prob == 5 ) { p05_title ( title ); } else { fprintf ( stderr, "\n" ); fprintf ( stderr, "P00_TITLE - Fatal error!\n" ); fprintf ( stderr, " Illegal problem number = %d\n", prob ); exit ( 1 ); } return; } /******************************************************************************/ double p01_fun ( double x ) /******************************************************************************/ /* 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: 18 February 2012 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; } /******************************************************************************/ void p01_lim ( double *a, double *b ) /******************************************************************************/ /* Purpose: P01_LIM returns the limits of the approximation interval for problem 1. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, double *A, *B, the limits of the interval of approximation. */ { *a = -5.0; *b = 5.0; return; } /******************************************************************************/ void p01_story ( ) /******************************************************************************/ /* Purpose: P01_STORY prints the "story" for problem 1. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: None */ { printf ( "\n" ); printf ( " This is a famous example, due to Runge. If equally spaced\n" ); printf ( " abscissas are used, the sequence of interpolating polynomials Pn(X)\n" ); printf ( " diverges, in the sense that the max norm of the difference\n" ); printf ( " between Pn(X) and F(X) becomes arbitrarily large as N increases.\n" ); return; } /******************************************************************************/ void p01_title ( char *title ) /******************************************************************************/ /* Purpose: P01_TITLE returns the title of problem 1. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, char *TITLE, the title of the problem. */ { strcpy ( title, "Runge example, f(x) = 1 / ( x * x + 1 ), [-5,5]" ); return; } /******************************************************************************/ double p02_fun ( double x ) /******************************************************************************/ /* 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: 18 February 2012 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; } /******************************************************************************/ void p02_lim ( double *a, double *b ) /******************************************************************************/ /* Purpose: P02_LIM returns the limits of the approximation interval for problem 2. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, double *A, *B, the limits of the interval of approximation. */ { *a = -1.0; *b = 1.0; return; } /******************************************************************************/ void p02_story ( ) /******************************************************************************/ /* Purpose: P02_STORY prints the "story" for problem 2. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: None */ { printf ( "\n" ); printf ( " This example is due to Bernstein.\n" ); printf ( " If equally spaced abscissas are used, the sequence of interpolating\n" ); printf ( " polynomials Pn(X) only converges to F(X) at -1, 0, and 1.\n" ); return; } /******************************************************************************/ void p02_title ( char *title ) /******************************************************************************/ /* Purpose: P02_TITLE returns the title of problem 2. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, char *TITLE, the title of the problem. */ { strcpy ( title, "Bernstein example, f(x) = abs ( x ), [-1,1]" ); return; } /******************************************************************************/ double p03_fun ( double x ) /******************************************************************************/ /* 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: 18 February 2012 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; } /******************************************************************************/ void p03_lim ( double *a, double *b ) /******************************************************************************/ /* Purpose: P03_LIM returns the limits of the approximation interval for problem 3. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, double *A, *B, the limits of the interval of approximation. */ { *a = -1.0; *b = 1.0; return; } /******************************************************************************/ void p03_story ( ) /******************************************************************************/ /* Purpose: P03_STORY prints the "story" for problem 3. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: None */ { printf ( "\n" ); printf ( " The step function is discontinuous.\n" ); printf ( " Attempts to approximate this function by high degree polynomials\n" ); printf ( " will rapidly diverge.\n" ); return; } /******************************************************************************/ void p03_title ( char *title ) /******************************************************************************/ /* Purpose: P03_TITLE returns the title of problem 3. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, char *TITLE, the title of the problem. */ { strcpy ( title, "Step function, f jumps from 0 to 1 at 0." ); return; } /******************************************************************************/ double p04_fun ( double x ) /******************************************************************************/ /* 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: 18 February 2012 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; } /******************************************************************************/ void p04_lim ( double *a, double *b ) /******************************************************************************/ /* Purpose: P04_LIM returns the limits of the approximation interval for problem 4. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, double *A, *B, the limits of the interval of approximation. */ { *a = 0.0; *b = 1.0; return; } /******************************************************************************/ void p04_story ( ) /******************************************************************************/ /* Purpose: P04_STORY prints the "story" for problem 4. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: None */ { printf ( "\n" ); printf ( " The Doppler function is continuous, but highly oscillatory\n" ); printf ( " near the value X = 0.\n" ); return; } /******************************************************************************/ void p04_title ( char *title ) /******************************************************************************/ /* Purpose: P04_TITLE returns the title of problem 4. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, char *TITLE, the title of the problem. */ { strcpy ( title, "Doppler function, highly oscillatory near X = 0." ); return; } /******************************************************************************/ double p05_fun ( double x ) /******************************************************************************/ /* 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: 18 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; } /******************************************************************************/ void p05_lim ( double *a, double *b ) /******************************************************************************/ /* Purpose: P05_LIM returns the limits of the approximation interval for problem 5. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, double *A, *B, the limits of the interval of approximation. */ { *a = 0.0; *b = 10.0; return; } /******************************************************************************/ void p05_story ( ) /******************************************************************************/ /* Purpose: P05_STORY prints the "story" for problem 5. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: None */ { printf ( "\n" ); printf ( " This example is very difficult to interpolate.\n" ); printf ( " It is essentially defined as a piecewise function,\n" ); printf ( " alternating between a straight line and a sinusoidal curve.\n" ); return; } /******************************************************************************/ void p05_title ( char *title ) /******************************************************************************/ /* Purpose: P05_TITLE returns the title of problem 5. Licensing: This code is distributed under the GNU LGPL license. Modified: 18 February 2012 Author: John Burkardt Parameters: Output, char *TITLE, the title of the problem. */ { strcpy ( title, "Rabbit ears, f(x) = max(sin(x)+sin(x^2),1-abs(x-5)/5), [0,10]" ); return; } /******************************************************************************/ double r8_abs_copy ( double x ) /******************************************************************************/ /* 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; } /******************************************************************************/ double r8_max_copy ( double x, double y ) /******************************************************************************/ /* 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; }