    PROGRAM nag_cheb_1d_ex03

      ! 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_con, &
       nag_cheb_1d_deriv
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC ABS, KIND, MAX, MAXVAL, MIN, MINVAL, SUM
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: d, i, l, m, n, np
      REAL (wp) :: deriv, fit, x_max, x_min
      ! .. Local Arrays ..
      INTEGER, ALLOCATABLE :: con_level(:)
      REAL (wp), ALLOCATABLE :: a_deriv(:), coeff(:,:), con_f(:,:), con_x(:), &
       f(:), resid(:), x(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_cheb_1d_ex03'

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

      READ (nag_std_in,*) m, n, l, d
      ALLOCATE (x(m),f(m),con_x(l),con_f(l,0:d),con_level(l),coeff(0:n,0:n), &
       resid(0:n),a_deriv(0:n-1))  ! Allocate storage

      READ (nag_std_in,*) (x(i),f(i),i=1,m)
      DO i = 1, l
        READ (nag_std_in,*) con_level(i), con_x(i), con_f(i,0:con_level(i))
      END DO

      CALL nag_cheb_1d_fit_con(x,f,con_x,con_level,con_f,coeff,resid=resid)

      x_min = MIN(x(1),MINVAL(con_x))
      x_max = MAX(x(m),MAXVAL(con_x))
      np = l + SUM(con_level)

      a_deriv = nag_cheb_1d_deriv(coeff(n,:),x_min,x_max)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) 'Degree  RMS residual'
      WRITE (nag_std_out,'(I5,1PE15.2)') (i,resid(i),i=np,n)
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'(A,5I10)') '  Coefficients of     ', (i,i=0,n)
      WRITE (nag_std_out,'(A,I2,A,5F10.5)') ' Fit of degree', n, '        ', &
       coeff(n,:)
      WRITE (nag_std_out,'(A,5F10.5)') ' It''s first derivatives ', a_deriv

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) &
       '  Evaluation at the data points (using fit of degree 4)'
      WRITE (nag_std_out,*) &
       '       x          f         Fit     Residual  1st derive'
      DO i = 1, m

        fit = nag_cheb_1d_eval(coeff(n,:),x(i),x_min,x_max)
        deriv = nag_cheb_1d_eval(a_deriv,x(i),x_min,x_max)

        WRITE (nag_std_out,'(1X,3F11.4,1PE11.2,0PF11.4)') x(i), f(i), fit, &
         ABS(fit-f(i)), deriv
      END DO

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) &
       '  Evaluation at the constraints (using fit of degree 4)'
      WRITE (nag_std_out,*) &
       '       x          f         Fit     Residual  1st derive  Residual'

      DO i = 1, l

        fit = nag_cheb_1d_eval(coeff(n,:),con_x(i),x_min,x_max)
        deriv = nag_cheb_1d_eval(a_deriv,con_x(i),x_min,x_max)
        IF (con_level(i)==1) THEN

          WRITE (nag_std_out,'(1X,3F11.4,1PE11.2,0PF11.4,1PE11.2)') con_x(i), &
           con_f(i,0), fit, ABS(fit-con_f(i,0)), deriv, ABS(deriv-con_f(i,1))
        ELSE

          WRITE (nag_std_out,'(1X,3F11.4,1PE11.2,0PF11.4)') con_x(i), &
           con_f(i,0), fit, ABS(fit-con_f(i,0)), deriv
        END IF
      END DO

      DEALLOCATE (x,f,con_x,con_f,con_level,coeff,resid, &
       a_deriv)                    ! Deallocate storage

    END PROGRAM nag_cheb_1d_ex03
