C driver and function for ChebyU
#include
<stdio.h>
#include
<math.h>
main()
{
/*
Function Name: ChebyU */
/*
Chebyshev Polynomial of second kind, order n */
double
ChebyU(int n, double x, double eps),x,eps;
int
n;
printf("
Chebyshev Polynomial (U) Tests");
x
= 0;
while
( fabs(x) <= 1 )
{
printf("\n\nInput n,x,eps (
|x|>1 to end): ");
scanf("%i%lf%lf",&n,&x,&eps);
if ( fabs(x) > 1 )
{
printf("\nEnd Chebyshev
Polynomial (U) Tests");
exit(0);
}
else
{
printf
("\nU(%i,%lf) = %.9E",n,x,ChebyU(n,x,eps));
} } }
double
ChebyU(int n, double x, double eps)
/*
Chebyshev Polynomial: second kind, order n */
{
double
xNearOne,nPone,nTerm,sign,theta;
if
( n < 1 ) return 1.0; /*
U0 */
if
( n < 2 ) return 2.0*x;
if
( n < 3 ) return -1.0+4.0*x*x;
/* U2 */
if
( n < 4 ) return 4.0*x*(-1.0+2.0*x*x);
/*
For n >= 4 use trig formulas */
/*
First check if |x| near 1 */
xNearOne
= 1 - x*x;
nPone
= n+1;
nTerm
= n*(n+2);
if
( xNearOne < sqrt(120.0*eps)/(nPone*nPone) )
{
/* Within danger zone for accuracy
eps */
/* Sign negative if n is odd & x
is negative:
else sign is positive */
sign = ( (n%2==1) &&
(x<0.0) ) ? -1.0: 1.0;
return
sign*nPone*(1.0 - nTerm*xNearOne/6.0);
}
/*
Outside danger zone for accuracy eps */
theta
= acos(x);
return
sin(nPone*theta)/sin(theta);
}