! E04FCF Example Program Text ! Mark 23 Release. NAG Copyright 2011. MODULE e04fcfe_mod ! E04FCF 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 = 6*n + m*n + 2*m + n*(n-1)/2 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,iw,liw,w,lw) ! Routine to evaluate the residuals ! .. Implicit None Statement .. IMPLICIT NONE ! .. Scalar Arguments .. INTEGER, INTENT (INOUT) :: iflag INTEGER, INTENT (IN) :: liw, lw, m, n ! .. Array Arguments .. REAL (KIND=nag_wp), INTENT (OUT) :: fvec(m) REAL (KIND=nag_wp), INTENT (INOUT) :: w(lw) REAL (KIND=nag_wp), INTENT (IN) :: xc(n) INTEGER, INTENT (INOUT) :: iw(liw) ! .. Executable Statements .. fvec(1:m) = xc(1) + t(1:m,1)/(xc(2)*t(1:m,2)+xc(3)*t(1:m,3)) - & y(1:m) 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 e04fcfe_mod PROGRAM e04fcfe ! E04FCF Example Main Program ! .. Use Statements .. USE nag_library, ONLY : e04fcf, x02ajf USE e04fcfe_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(m,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,*) 'E04FCF 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 ! Set IPRINT to 1 to obtain output from LSQMON at each iteration iprint = -1 maxcal = 400*n eta = 0.5_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 e04fcf(m,n,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 estimated 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 e04fcfe