    PROGRAM nag_lin_lsq_ex01

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

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_lin_lsq, ONLY : nag_lin_lsq_sol, nag_lin_lsq_sol_svd
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND, MIN
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, m, n, ns, rank
      REAL (wp) :: std_err_b, std_err_ls, tol
      ! .. Local Arrays ..
      REAL (wp), ALLOCATABLE :: a(:,:), b(:), sigma(:), x_b(:), x_ls(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_lin_lsq_ex01'

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) m, n
      ns = MIN(m,n)

      ALLOCATE (a(m,n),b(m),sigma(ns),x_b(n),x_ls(n)) ! Allocate storage

      READ (nag_std_in,*) (a(i,:),i=1,m)
      READ (nag_std_in,*) b

      ! Compute the minimum norm solution with tol = 0.005

      tol = 0.005_wp

      CALL nag_lin_lsq_sol(a,b,x_ls,sigma=sigma,tol=tol,rank=rank, &
       std_err=std_err_ls)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'(1X,A,F7.4)') 'Minimum norm solution with tol =', &
       tol
      WRITE (nag_std_out,'(2X,F7.4)') x_ls
      WRITE (nag_std_out,'(1X,A,I7,A,F7.4)') 'rank =', rank, &
       '  standard error =', std_err_ls
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) 'Singular values of A'
      WRITE (nag_std_out,'(2X,F7.4)') sigma

      ! Compute the minimum norm solution with tol = 0.0005

      tol = 0.0005_wp

      CALL nag_lin_lsq_sol_svd(a(:ns,:),sigma,b,x_ls,tol=tol,rank=rank, &
       std_err=std_err_ls)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'(1X,A,F7.4)') 'Minimum norm solution with tol =', &
       tol
      WRITE (nag_std_out,'(2X,F7.4)') x_ls
      WRITE (nag_std_out,'(1X,A,I7,A,F7.4)') 'rank =', rank, &
       '  standard error =', std_err_ls

      ! Compute a basic solution with tol = 0.005

      tol = 0.005_wp

      CALL nag_lin_lsq_sol_svd(a(:ns,:),sigma,b,x_b,solution='Basic',tol=tol, &
       rank=rank,std_err=std_err_b)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'(1X,A,F7.4)') 'Basic solution with tol =', tol
      WRITE (nag_std_out,'(2X,F7.4)') x_b
      WRITE (nag_std_out,'(1X,A,I7,A,F7.4)') 'rank =', rank, &
       '  standard error =', std_err_b

      DEALLOCATE (a,b,sigma,x_b,x_ls) ! Deallocate storage

    END PROGRAM nag_lin_lsq_ex01
