program main !*****************************************************************************80 ! !! test_nls_test() tests test_nls(). ! ! Discussion: ! ! This sample program demonstrates how the problems can ! be used to test an algorithm or package that solves nonlinear ! least squares problems. ! ! In this case, we use the problems to test the performance of one of ! the MINPACK routines, LMDER1. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 December 2008 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) real ( kind = rk ), parameter :: abstol = 0.00001D+00 real ( kind = rk ), allocatable, dimension ( : ) :: f external fcn real ( kind = rk ), allocatable, dimension (:,:) :: fjac real ( kind = rk ) fnrm real ( kind = rk ), allocatable, dimension ( : ) :: g integer info integer known integer m integer, parameter :: maxit = 30 integer mprob integer n integer nprob integer nprob2 character ( len = 80 ) title real ( kind = rk ), allocatable, dimension ( : ) :: x real ( kind = rk ) xnrm common /comprb/ nprob2 call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'test_nls_test():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' Test test_nls().' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Show how the sample problems can be used.' write ( *, '(a)' ) ' In this example, we use the sample problems with the' write ( *, '(a)' ) ' MINPACK routine LMDER1.' ! ! Get the number of problems. ! call p00_mprob ( mprob ) ! ! Solve each problem ! do nprob = 1, mprob nprob2 = nprob ! ! Get the size of the problem. ! call p00_mn ( nprob, m, n ) if ( m == 0 .or. n == 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'There are no more problems!' exit end if if ( m < 0 ) then m = abs ( m ) end if if ( n < 0 ) then n = abs ( n ) end if ! ! Get the title of the problem. ! call p00_title ( nprob, title ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of equations M = ', m write ( *, '(a,i6)' ) ' Number of variables N = ', n write ( *, '(a)' ) ' ' ! ! Allocate space. ! allocate ( f(1:m) ) allocate ( fjac(1:m,1:n) ) allocate ( g(1:n) ) allocate ( x(1:n) ) ! ! Get, and describe, starting point ! call p00_start ( nprob, n, x ) if ( n <= 10 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Starting point X:' write ( *, '(a)' ) ' ' write ( *, '(5g14.6)' ) x(1:n) else xnrm = sqrt ( sum ( x(1:n)**2 ) ) write ( *, '(a)') ' ' write ( *, '(a,g14.6)' ) ' ||X|| = ', xnrm end if call p00_f ( nprob, m, n, x, f ) if ( m <= 10 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' F(X):' write ( *, '(a)' ) ' ' write ( *, '(5g14.6)' ) f(1:m) else fnrm = sqrt ( sum ( f(1:m)**2 ) ) write ( *, '(a)' ) ' ' write ( *, '(a,g14.6)' ) ' ||F(X)|| = ', fnrm end if if ( n <= 10 ) then call p00_g ( nprob, m, n, x, g ) call r8vec_print ( n, g, ' The least squares gradient:' ) end if ! ! Call MINPACK routine LMDER1 to minimize F. ! call lmder1 ( fcn, m, n, x, f, fjac, m, abstol, info ) ! ! Report results. ! write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) 'LMDER1 return flag INFO = ', info if ( info == 0 ) then write ( *, '(a)' ) 'Improper input parameters.' else if ( info == 1 ) then write ( *, '(a)' ) 'Relative error in the sum of squares is at most TOL.' else if ( info == 2 ) then write ( *, '(a)' ) 'Relative error in X is at most TOL.' else if ( info == 3 ) then write ( *, '(a)' ) 'Relative error in X and sum of squares at most TOL.' else if ( info == 4 ) then write ( *, '(a)' ) 'FVEC is orthogonal to the columns of the jacobian.' else if ( info == 5 ) then write ( *, '(a)' ) 'Too many calls to FCN with IFLAG = 1.' else if ( info == 6 ) then write ( *, '(a)' ) 'TOL is too small, can''t improve sum of squares.' else if ( info == 7 ) then write ( *, '(a)' ) 'TOL is too small, can''t improve X further.' end if ! ! Describe final point ! if ( n <= 10 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Final point X:' write ( *, '(a)' ) ' ' write ( *, '(5g14.6)' ) x(1:n) else xnrm = sqrt ( sum ( x(1:n)**2 ) ) write ( *, '(a)' ) ' ' write ( *, '(a,g14.6)' ) ' ||X|| = ',xnrm end if if ( m <= 10 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' F(X):' write ( *, '(a)' ) ' ' write ( *, '(5g14.6)' ) f(1:m) else fnrm = sqrt ( sum ( f(1:m)**2 ) ) write ( *, '(a)' ) ' ' write ( *, '(a,g14.6)' ) ' ||F(X)|| = ', fnrm end if if ( n <= 10 ) then call p00_g ( nprob, m, n, x, g ) call r8vec_print ( n, g, ' The least squares gradient:' ) end if if ( m <= 5 ) then call p00_j ( nprob, m, n, x, fjac ) call r8mat_print ( m, n, fjac, ' Jacobian matrix:' ) end if ! ! Report true solution, if known. ! call p00_sol ( nprob, m, n, known, x ) if ( known == 1 ) then if ( n <= 10 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Solution X:' write ( *, '(a)' ) ' ' write ( *, '(5g14.6)' ) x(1:n) else xnrm = sqrt ( sum ( x(1:n)**2 ) ) write ( *, '(a)' ) ' ' write ( *, '(a,g14.6)' ) ' ||X|| = ',xnrm end if call p00_f ( nprob, m, n, x, f ) if ( m <= 10 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' F(X):' write ( *, '(a)' ) ' ' write ( *, '(5g14.6)' ) f(1:m) else fnrm = sqrt ( sum ( f(1:m)**2 ) ) write ( *, '(a)' ) ' ' write ( *, '(a,g14.6)' ) ' ||F(X)|| = ', fnrm end if if ( n <= 10 ) then call p00_g ( nprob, m, n, x, g ) call r8vec_print ( n, g, ' The least squares gradient:' ) end if if ( m <= 5 ) then call p00_j ( nprob, m, n, x, fjac ) call r8mat_print ( m, n, fjac, ' Jacobian matrix:' ) end if end if deallocate ( f ) deallocate ( fjac ) deallocate ( g ) deallocate ( x ) end do ! ! Terminate. ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'test_nls_test():' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) ' ' call timestamp ( ) stop 0 end subroutine fcn ( m, n, x, f, fjac, ldfjac, iflag ) !*****************************************************************************80 ! !! FCN is a user-supplied routine to evaluate the function and jacobian. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 December 2008 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of equations. ! ! Input, integer N, the number of variables. ! ! Input, real ( kind = rk ) X(N), the point where evaluation is desired. ! ! Output, real ( kind = rk ) F(M), the value of the function, if requested. ! ! Output, real ( kind = rk ) FJAC(LDFJAC,N), the value of the M by N ! jacobian matrix, if requested. ! ! Input, integer LDFJAC, the leading dimension of FJAC. ! ! Input, integer IFLAG, is 1 if F is to be evaluated; otherwise, ! FJAC is to be evaluated. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer ldfjac integer m integer n real ( kind = rk ) f(m) real ( kind = rk ) fjac(ldfjac,n) integer iflag integer nprob real ( kind = rk ) x(n) ! ! This common block is just a trick to allow the main program to ! specify a particular problem. ! common /comprb/ nprob if ( iflag == 1 ) then call p00_f ( nprob, m, n, x, f ) else call p00_j ( nprob, m, n, x, fjac ) end if return end subroutine r8_fake_use ( x ) !*****************************************************************************80 ! !! r8_fake_use() pretends to use an R8 variable. ! ! Discussion: ! ! Some compilers will issue a warning if a variable is unused. ! Sometimes there's a good reason to include a variable in a program, ! but not to use it. Calling this function with that variable as ! the argument will shut the compiler up. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 April 2020 ! ! Author: ! ! John Burkardt ! ! Input: ! ! real ( kind = rk8 ) X, the variable to be "used". ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) x if ( x /= x ) then write ( *, '(a)' ) ' r8_fake_use(): variable is NAN.' end if return end subroutine r8mat_print ( m, n, a, title ) !*****************************************************************************80 ! !! r8mat_print() prints an R8MAT. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 20 May 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows in A. ! ! Input, integer N, the number of columns in A. ! ! Input, real ( kind = rk ) A(M,N), the matrix. ! ! Input, character ( len = * ) TITLE, a title to be printed. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) character ( len = * ) title call r8mat_print_some ( m, n, a, 1, 1, m, n, title ) return end subroutine r8mat_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ) !*****************************************************************************80 ! !! r8mat_print_some() prints some of a double precision matrix. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 04 November 2003 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns. ! ! Input, real ( kind = rk ) A(M,N), an M by N matrix to be printed. ! ! Input, integer ILO, JLO, the first row and column to print. ! ! Input, integer IHI, JHI, the last row and column to print. ! ! Input, character ( len = * ) TITLE, an optional title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer, parameter :: incx = 5 integer m integer n real ( kind = rk ) a(m,n) character ( len = 14 ) ctemp(incx) integer i integer i2hi integer i2lo integer ihi integer ilo integer inc integer j integer j2 integer j2hi integer j2lo integer jhi integer jlo character ( len = * ) title if ( 0 < len_trim ( title ) ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if do j2lo = max ( jlo, 1 ), min ( jhi, n ), incx j2hi = j2lo + incx - 1 j2hi = min ( j2hi, n ) j2hi = min ( j2hi, jhi ) inc = j2hi + 1 - j2lo write ( *, '(a)' ) ' ' do j = j2lo, j2hi j2 = j + 1 - j2lo write ( ctemp(j2), '(i7,7x)') j end do write ( *, '('' Col '',5a14)' ) ctemp(1:inc) write ( *, '(a)' ) ' Row' write ( *, '(a)' ) ' ' i2lo = max ( ilo, 1 ) i2hi = min ( ihi, m ) do i = i2lo, i2hi do j2 = 1, inc j = j2lo - 1 + j2 write ( ctemp(j2), '(g14.6)' ) a(i,j) end do write ( *, '(i5,1x,5a14)' ) i, ( ctemp(j), j = 1, inc ) end do end do write ( *, '(a)' ) ' ' return end subroutine r8vec_print ( n, a, title ) !*****************************************************************************80 ! !! r8vec_print() prints an R8VEC. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 December 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of components of the vector. ! ! Input, real ( kind = rk ) A(N), the vector to be printed. ! ! Input, character ( len = * ) TITLE, a title to be printed first. ! TITLE may be blank. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n) integer i character ( len = * ) title if ( title /= ' ' ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if write ( *, '(a)' ) ' ' do i = 1, n write ( *, '(i8,g14.6)' ) i, a(i) end do return end subroutine timestamp ( ) !*****************************************************************************80 ! !! TIMESTAMP prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 18 May 2013 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end