! E04GBF Example Program Text ! Mark 23 Release. NAG Copyright 2011. MODULE e04gbfe_mod ! E04GBF Example Program Module: ! Parameters and User-defined Routines ! .. Use Statements .. USE nag_library, ONLY : nag_wp ! .. Implicit None Statement .. IMPLICIT NONE ! .. Parameters .. REAL (KIND=nag_wp), PARAMETER :: one = 1.0_nag_wp REAL (KIND=nag_wp), PARAMETER :: two = 2.0_nag_wp REAL (KIND=nag_wp), PARAMETER :: zero = 0.0_nag_wp INTEGER, PARAMETER :: inc1 = 1, liw = 1, m = 15, & n = 3, nin = 5, nout = 6, nt = 3 INTEGER, PARAMETER :: ldfjac = m INTEGER, PARAMETER :: ldv = n INTEGER, PARAMETER :: lw = 7*n + m*n + 2*m + n*n CHARACTER (1), PARAMETER :: trans = 'T' ! .. Local Arrays .. REAL (KIND=nag_wp) :: t(m,nt), y(m) CONTAINS SUBROUTINE lsqgrd(m,n,fvec,fjac,ldfjac,g) ! Routine to evaluate gradient of the sum of squares ! .. Use Statements .. USE nag_library, ONLY : dgemv ! .. Implicit None Statement .. IMPLICIT NONE ! .. Scalar Arguments .. INTEGER, INTENT (IN) :: ldfjac, m, n ! .. Array Arguments .. REAL (KIND=nag_wp), INTENT (IN) :: fjac(ldfjac,n), fvec(m) REAL (KIND=nag_wp), INTENT (OUT) :: g(n) ! .. Executable Statements .. ! The NAG name equivalent of dgemv is f06paf CALL dgemv(trans,m,n,one,fjac,ldfjac,fvec,inc1,zero,g,inc1) g(1:n) = two*g(1:n) RETURN END SUBROUTINE lsqgrd SUBROUTINE lsqfun(iflag,m,n,xc,fvec,fjac,ldfjac,iw,liw,w,lw) ! Routine to evaluate the residuals and their 1st derivatives. ! This routine is also suitable for use when E04FCV is used as ! LSQLIN, since it can deal with IFLAG = 0 as well as IFLAG = 2. ! .. Implicit None Statement .. IMPLICIT NONE ! .. Scalar Arguments .. INTEGER, INTENT (INOUT) :: iflag INTEGER, INTENT (IN) :: ldfjac, liw, lw, m, n ! .. Array Arguments .. REAL (KIND=nag_wp), INTENT (INOUT) :: fjac(ldfjac,n), w(lw) REAL (KIND=nag_wp), INTENT (OUT) :: fvec(m) REAL (KIND=nag_wp), INTENT (IN) :: xc(n) INTEGER, INTENT (INOUT) :: iw(liw) ! .. Local Scalars .. REAL (KIND=nag_wp) :: denom, dummy INTEGER :: i ! .. Executable Statements .. DO i = 1, m denom = xc(2)*t(i,2) + xc(3)*t(i,3) fvec(i) = xc(1) + t(i,1)/denom - y(i) IF (iflag/=0) THEN fjac(i,1) = one dummy = -one/(denom*denom) fjac(i,2) = t(i,1)*t(i,2)*dummy fjac(i,3) = t(i,1)*t(i,3)*dummy END IF END DO RETURN END SUBROUTINE lsqfun SUBROUTINE lsqmon(m,n,xc,fvec,fjac,ldfjac,s,igrade,niter,nf,iw,liw,w, & lw) ! Monitoring routine ! .. Use Statements .. USE nag_library, ONLY : ddot ! .. Implicit None Statement .. IMPLICIT NONE ! .. Parameters .. INTEGER, PARAMETER :: ndec = 3 ! .. Scalar Arguments .. INTEGER, INTENT (IN) :: igrade, ldfjac, liw, lw, m, & n, nf, niter ! .. Array Arguments .. REAL (KIND=nag_wp), INTENT (IN) :: fjac(ldfjac,n), fvec(m), & s(n), xc(n) REAL (KIND=nag_wp), INTENT (INOUT) :: w(lw) INTEGER, INTENT (INOUT) :: iw(liw) ! .. Local Scalars .. REAL (KIND=nag_wp) :: fsumsq, gtg INTEGER :: j ! .. Local Arrays .. REAL (KIND=nag_wp) :: g(ndec) ! .. Executable Statements .. ! The NAG name equivalent of ddot is f06eaf fsumsq = ddot(m,fvec,inc1,fvec,inc1) CALL lsqgrd(m,n,fvec,fjac,ldfjac,g) gtg = ddot(n,g,inc1,g,inc1) WRITE (nout,*) WRITE (nout,*) & ' Itn F evals SUMSQ GTG Grade' WRITE (nout,99999) niter, nf, fsumsq, gtg, igrade WRITE (nout,*) WRITE (nout,*) & ' X G Singular values' WRITE (nout,99998) (xc(j),g(j),s(j),j=1,n) RETURN 99999 FORMAT (1X,I4,6X,I5,6X,1P,E13.5,6X,1P,E9.1,6X,I3) 99998 FORMAT (1X,1P,E13.5,10X,1P,E9.1,10X,1P,E9.1) END SUBROUTINE lsqmon END MODULE e04gbfe_mod PROGRAM e04gbfe ! E04GBF Example Main Program ! .. Use Statements .. USE nag_library, ONLY : e04gbf, e04hev, e04yaf, x02ajf USE e04gbfe_mod, ONLY : ldfjac, ldv, liw, lsqfun, lsqgrd, lsqmon, lw, & m, n, nag_wp, nin, nout, nt, t, y ! .. Implicit None Statement .. IMPLICIT NONE ! .. Local Scalars .. REAL (KIND=nag_wp) :: eta, fsumsq, stepmx, xtol INTEGER :: i, ifail, iprint, maxcal, nf, & niter ! .. Local Arrays .. REAL (KIND=nag_wp) :: fjac(ldfjac,n), fvec(m), g(n), & s(n), v(ldv,n), w(lw), x(n) INTEGER :: iw(liw) ! .. Intrinsic Functions .. INTRINSIC sqrt ! .. Executable Statements .. WRITE (nout,*) 'E04GBF Example Program Results' ! Skip heading in data file READ (nin,*) ! Observations of TJ (J = 1, 2, ..., nt) are held in T(I, J) ! (I = 1, 2, . . . , m) DO i = 1, m READ (nin,*) y(i), t(i,1:nt) END DO ! Check LSQFUN by calling E04YAF at an arbitrary point x(1:nt) = (/ 0.19_nag_wp, -1.34_nag_wp, 0.88_nag_wp/) ifail = 0 CALL e04yaf(m,n,lsqfun,x,fvec,fjac,ldfjac,iw,liw,w,lw,ifail) ! Continue setting parameters for E04GBF ! Set IPRINT to 1 to obtain output from LSQMON at each iteration iprint = -1 maxcal = 50*n ! Since E04HEV is being used as LSQLIN, we set ETA to 0.9 eta = 0.9_nag_wp xtol = 10.0_nag_wp*sqrt(x02ajf()) ! We estimate that the minimum will be within 10 units of the ! starting point stepmx = 10.0_nag_wp ! Set up the starting point x(1:nt) = (/ 0.5_nag_wp, 1.0_nag_wp, 1.5_nag_wp/) ifail = -1 CALL e04gbf(m,n,e04hev,lsqfun,lsqmon,iprint,maxcal,eta,xtol,stepmx,x, & fsumsq,fvec,fjac,ldfjac,s,v,ldv,niter,nf,iw,liw,w,lw,ifail) SELECT CASE (ifail) CASE (0,2:) WRITE (nout,*) WRITE (nout,99999) 'On exit, the sum of squares is', fsumsq WRITE (nout,99999) 'at the point', x(1:n) CALL lsqgrd(m,n,fvec,fjac,ldfjac,g) WRITE (nout,99998) 'The corresponding gradient is', g(1:n) WRITE (nout,*) ' (machine dependent)' WRITE (nout,*) 'and the residuals are' WRITE (nout,99997) fvec(1:m) END SELECT 99999 FORMAT (1X,A,3F12.4) 99998 FORMAT (1X,A,1P,3E12.3) 99997 FORMAT (1X,1P,E9.1) END PROGRAM e04gbfe