    PROGRAM nag_cheb_1d_ex02

      ! Example Program Text for nag_cheb_1d
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_out
      USE nag_cheb_1d, ONLY : nag_cheb_1d_eval, nag_cheb_1d_interp
      USE nag_math_constants, ONLY : nag_pi
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC ABS, COS, EXP, KIND, REAL
      ! .. Parameters ..
      INTEGER, PARAMETER :: n = 10
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, j
      REAL (wp) :: pi, x_max, x_min
      ! .. Local Arrays ..
      REAL (wp) :: a(0:n), f(0:n), fit(0:n), x(0:n)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_cheb_1d_ex02'

      pi = nag_pi(0.0_wp)
      x_min = -1.4_wp
      x_max = 2.6_wp

      ! Evaluating
      x = 0.5_wp*(x_max-x_min)*COS((pi/REAL(n,kind=wp))*(/(i,i=0,n)/)) + &
       0.5_wp*(x_max+x_min)

      ! Evaluating  f(x)
      f = EXP(0.5_wp*x-0.3_wp)

      a = nag_cheb_1d_interp(f)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '         Chebyshev'
      WRITE (nag_std_out,*) '  j   coefficient a(j)'
      WRITE (nag_std_out,'(1X,I3,F14.7)') (j,a(j),j=0,n)

      fit = nag_cheb_1d_eval(a(0:6),x,x_min=x_min,x_max=x_max)
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) ' Fit using polynomial of degree 6'
      WRITE (nag_std_out,*) '    Abscissa   Ordinate      Fit     Residual'
      WRITE (nag_std_out,fmt='(1x, 3f11.4,E12.3)') (x(i),f(i),fit(i),ABS(f( &
       i)-fit(i)),i=0,n)

    END PROGRAM nag_cheb_1d_ex02
