! C05PCA Example Program Text ! Mark 24 Release. NAG Copyright 2012. Module c05pcae_mod ! C05PCA 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 :: factor = 100.0_nag_wp Integer, Parameter :: maxfev = 1000, mode = 2, n = 9, & nout = 6, nprint = 0 Integer, Parameter :: nr = n*(n+1)/2 Integer, Parameter :: ldfjac = n Contains Subroutine fcn(n,x,fvec,fjac,ldfjac,iflag,iuser,ruser) ! .. Scalar Arguments .. Integer, Intent (Inout) :: iflag Integer, Intent (In) :: ldfjac, n ! .. Array Arguments .. Real (Kind=nag_wp), Intent (Inout) :: fjac(ldfjac,n), fvec(n), & ruser(*) Real (Kind=nag_wp), Intent (In) :: x(n) Integer, Intent (Inout) :: iuser(*) ! .. Local Scalars .. Integer :: k ! .. Executable Statements .. If (iflag==0) Then ! Insert print statements here when NPRINT is positive. Continue Else If (iflag/=2) Then fvec(1:n) = (ruser(2)+ruser(3)*x(1:n))*x(1:n) - ruser(5) fvec(2:n) = fvec(2:n) + ruser(1)*x(1:(n-1)) fvec(1:(n-1)) = fvec(1:(n-1)) + ruser(4)*x(2:n) Else fjac(1:n,1:n) = 0.0E0_nag_wp fjac(1,1) = ruser(2) + 2.0_nag_wp*ruser(3)*x(1) fjac(1,2) = ruser(4) Do k = 2, n - 1 fjac(k,k) = ruser(2) + 2.0_nag_wp*ruser(3)*x(k) fjac(k,k-1) = ruser(1) fjac(k,k+1) = ruser(4) End Do fjac(n,n-1) = ruser(1) fjac(n,n) = ruser(2) + 2.0_nag_wp*ruser(3)*x(n) End If Return End Subroutine fcn End Module c05pcae_mod Program c05pcae ! C05PCA Example Main Program ! .. Use Statements .. Use nag_library, Only: c05pca, dnrm2, nag_wp, x02ajf Use c05pcae_mod, Only: factor, fcn, ldfjac, maxfev, mode, n, nout, & nprint, nr ! .. Implicit None Statement .. Implicit None ! .. Local Scalars .. Real (Kind=nag_wp) :: fnorm, xtol Integer :: ifail, j, lr, nfev, njev ! .. Local Arrays .. Real (Kind=nag_wp), Allocatable :: diag(:), fjac(:,:), fvec(:), & qtf(:), r(:), x(:) Real (Kind=nag_wp) :: ruser(5), w(1,1) Integer :: iuser(1) ! .. Intrinsic Procedures .. Intrinsic :: real, sqrt ! .. Executable Statements .. Write (nout,*) 'C05PCA Example Program Results' Allocate (diag(n),fjac(ldfjac,n),fvec(n),qtf(n),r(nr),x(n)) ! Store the coefficients describing the system ruser(1:5) = real((/-1,3,-2,-2,-1/),kind=nag_wp) ! The following starting values provide a rough solution. x(1:n) = -1.0E0_nag_wp xtol = sqrt(x02ajf()) diag(1:n) = 1.0_nag_wp ifail = -1 Call c05pca(fcn,n,x,fvec,fjac,ldfjac,xtol,maxfev,diag,mode,factor, & nprint,nfev,njev,r,lr,qtf,w,iuser,ruser,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,99998) 'Number of Jacobian evaluations =', njev 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 c05pcae