! C05NCF Example Program Text ! Mark 24 Release. NAG Copyright 2012. Module c05ncfe_mod ! C05NCF 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 :: epsfcn = 0.0_nag_wp Real (Kind=nag_wp), Parameter :: factor = 100.0_nag_wp Real (Kind=nag_wp), Parameter :: one = 1.0_nag_wp Real (Kind=nag_wp), Parameter :: three = 3.0_nag_wp Real (Kind=nag_wp), Parameter :: two = 2.0_nag_wp Integer, Parameter :: maxfev = 2000, ml = 1, mode = 2, & mu = 1, n = 9, nout = 6, & nprint = 0 Integer, Parameter :: nr = n*(n+1)/2 Integer, Parameter :: ldfjac = n Contains Subroutine fcn(n,x,fvec,iflag) ! .. Scalar Arguments .. Integer, Intent (Inout) :: iflag Integer, Intent (In) :: n ! .. Array Arguments .. Real (Kind=nag_wp), Intent (Inout) :: fvec(n) Real (Kind=nag_wp), Intent (In) :: x(n) ! .. Executable Statements .. If (iflag==0) Then ! Insert print statements here when NPRINT is positive. Continue Else fvec(1:n) = three*x(1:n) - two*x(1:n)*x(1:n) + one fvec(2:n) = fvec(2:n) - x(1:(n-1)) fvec(1:(n-1)) = fvec(1:(n-1)) - two*x(2:n) End If Return End Subroutine fcn End Module c05ncfe_mod Program c05ncfe ! C05NCF Example Main Program ! .. Use Statements .. Use nag_library, Only: c05ncf, dnrm2, nag_wp, x02ajf Use c05ncfe_mod, Only: epsfcn, factor, fcn, ldfjac, maxfev, ml, mode, & mu, n, nout, nprint, nr, one ! .. Implicit None Statement .. Implicit None ! .. Local Scalars .. Real (Kind=nag_wp) :: fnorm, xtol Integer :: ifail, j, lr, nfev ! .. Local Arrays .. Real (Kind=nag_wp), Allocatable :: diag(:), fjac(:,:), fvec(:), & qtf(:), r(:), x(:) Real (Kind=nag_wp) :: w(1,1) ! .. Intrinsic Procedures .. Intrinsic :: sqrt ! .. Executable Statements .. Write (nout,*) 'C05NCF Example Program Results' Allocate (diag(n),fjac(ldfjac,n),fvec(n),qtf(n),r(nr),x(n)) ! The following starting values provide a rough solution. x(1:n) = -one xtol = sqrt(x02ajf()) diag(1:n) = one ifail = -1 Call c05ncf(fcn,n,x,fvec,xtol,maxfev,ml,mu,epsfcn,diag,mode,factor, & nprint,nfev,fjac,ldfjac,r,lr,qtf,w,ifail) Select Case (ifail) Case (0) ! The NAG name equivalent of dnrm2 is f06ejf fnorm = dnrm2(n,fvec,1) Write (nout,*) Write (nout,99999) 'Final 2-norm of the residuals =', fnorm Write (nout,*) Write (nout,99998) 'Number of function evaluations =', nfev Write (nout,*) Write (nout,*) 'Final approximate solution' Write (nout,*) Write (nout,99997)(x(j),j=1,n) Case (2:) Write (nout,*) Write (nout,*) 'Approximate solution' Write (nout,*) Write (nout,99997)(x(j),j=1,n) End Select 99999 Format (1X,A,E12.4) 99998 Format (1X,A,I10) 99997 Format (1X,3F12.4) End Program c05ncfe