    PROGRAM nag_cheb_1d_ex01

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

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_cheb_1d, ONLY : nag_cheb_1d_eval, nag_cheb_1d_fit, &
       nag_cheb_1d_deriv, nag_cheb_1d_intg
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC ABS, KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, j, k, l, m, n
      REAL (wp) :: deriv_1, deriv_2, f_calc, x_max, x_min, x_var
      ! .. Local Arrays ..
      REAL (wp), ALLOCATABLE :: a_deriv_1(:), a_deriv_2(:), a_intg(:), &
       coeff(:,:), f(:), resid(:), wt(:), x(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_cheb_1d_ex01'

      READ (nag_std_in,*)          ! Skip heading in data file

      ! Read the size of data
      READ (nag_std_in,*) m

      ! Read the maximum degree of the polynomial
      READ (nag_std_in,*) n

      ALLOCATE (coeff(0:n,0:n),f(m),x(m),wt(m),resid(0:n),a_deriv_1(0:n-1), &
       a_deriv_2(0:n-2),a_intg(0:n+1)) ! Allocate storage

      ! Read in problem data
      DO i = 1, m
        READ (nag_std_in,*) x(i), f(i), wt(i)
      END DO

      ! Determine the fit for degrees up to n
      CALL nag_cheb_1d_fit(x,f,coeff,wt=wt,resid=resid)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '  Degree    R.M.S.        Chebyshev coeff A(j)'
      WRITE (nag_std_out,*) '           residual       j=0     &
       &  j=1       j=2       j=3       j=4'
      DO i = 0, n
        WRITE (nag_std_out,'(I6,1p,e14.2,2x,0p,6F10.4)') i, resid(i), &
         (coeff(i,j),j=0,i)
      END DO

      x_min = x(1)
      x_max = x(m)

      l = n
      DO k = 1, 2
        ! Determine coefficients for first derivatives
        a_deriv_1(:l-1) = nag_cheb_1d_deriv(coeff(l,:l),x_min,x_max)

        ! Determine coefficients for second derivatives
        a_deriv_2(:l-2) = nag_cheb_1d_deriv(a_deriv_1(:l-1),x_min,x_max)

        ! Determine coefficients for indefinite integral
        a_intg(:l+1) = nag_cheb_1d_intg(coeff(l,:l),x_min,x_max)

        WRITE (nag_std_out,'(//A,i3)') '  Using degree ', l
        WRITE (nag_std_out,'(A)') '  ================'
        WRITE (nag_std_out,'(/A,6I10)') '  Coefficients of     ', (j,j=0,l+1)
        WRITE (nag_std_out,'(A,I2,A,5F10.4)') ' Polynomial of degree', l, &
         '  ', coeff(l,:l)
        WRITE (nag_std_out,'(A,5F10.4)') ' It''s first derivatives  ', &
         a_deriv_1(:l-1)
        WRITE (nag_std_out,'(A,5F10.4)') ' It''s second derivatives ', &
         a_deriv_2(:l-2)
        WRITE (nag_std_out,'(A,6F10.4)') ' It''s indefinite integral', &
         a_intg(:l+1)

        WRITE (nag_std_out,*)
        WRITE (nag_std_out,'(1x,A,I4)') &
         'Polynomial approximation, residuals and derivatives using degree', l
        WRITE (nag_std_out,*)
        WRITE (nag_std_out,*) '  Abscissa     Weight   Ordinate &
         & Polynomial  Residual   1st Deriv  2nd Deriv'
        DO i = 1, m
          x_var = x(i)
          f_calc = nag_cheb_1d_eval(coeff(l,:l),x_var,x_min,x_max)
          deriv_1 = nag_cheb_1d_eval(a_deriv_1(:l-1),x_var,x_min,x_max)
          deriv_2 = nag_cheb_1d_eval(a_deriv_2(:l-2),x_var,x_min,x_max)
          WRITE (nag_std_out,'(4f11.4,e11.2,2f11.4)') x_var, wt(i), f(i), &
           f_calc, ABS(f(i)-f_calc), deriv_1, deriv_2
          IF (i==m) EXIT
          x_var = 0.5_wp*(x(i)+x(i+1))
          f_calc = nag_cheb_1d_eval(coeff(l,:l),x_var,x_min,x_max)
          deriv_1 = nag_cheb_1d_eval(a_deriv_1(:l-1),x_var,x_min,x_max)
          deriv_2 = nag_cheb_1d_eval(a_deriv_2(:l-2),x_var,x_min,x_max)
          WRITE (nag_std_out,'(f11.4,22x,f11.4,11x,2f11.4)') x_var, f_calc, &
           deriv_1, deriv_2
        END DO

        WRITE (nag_std_out,*)
        WRITE (nag_std_out,fmt='(1x,A,F3.1,A,F3.1,A,F10.4)') &
         ' Definite Integral (', x(1), ' .. ', x(m-1), ') = ', &
         nag_cheb_1d_eval(a_intg(:l+1),x(m-1),x_min=x_min,x_max=x_max) - &
         nag_cheb_1d_eval(a_intg(:l+1),x(1),x_min=x_min,x_max=x_max)
        l = l - 1
      END DO

      DEALLOCATE (coeff,f,x,wt,resid,a_deriv_1,a_deriv_2, &
       a_intg)          ! Deallocate storage

    END PROGRAM nag_cheb_1d_ex01
