PHYEX 0.9.5
PHYsique EXternalisée
Loading...
Searching...
No Matches
minpack.F90
Go to the documentation of this file.
1!!$ Minpack Copyright Notice (1999) University of Chicago. All rights reserved
2!!$
3!!$ Redistribution and use in source and binary forms, with or
4!!$ without modification, are permitted provided that the
5!!$ following conditions are met:
6!!$
7!!$ 1. Redistributions of source code must retain the above
8!!$ copyright notice, this list of conditions and the following
9!!$ disclaimer.
10!!$
11!!$ 2. Redistributions in binary form must reproduce the above
12!!$ copyright notice, this list of conditions and the following
13!!$ disclaimer in the documentation and/or other materials
14!!$ provided with the distribution.
15!!$
16!!$ 3. The end-user documentation included with the
17!!$ redistribution, if any, must include the following
18!!$ acknowledgment:
19!!$
20!!$ "This product includes software developed by the
21!!$ University of Chicago, as Operator of Argonne National
22!!$ Laboratory."
23!!$
24!!$ Alternately, this acknowledgment may appear in the software
25!!$ itself, if and wherever such third-party acknowledgments
26!!$ normally appear.
27!!$
28!!$ 4. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
29!!$ WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
30!!$ UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
31!!$ THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
32!!$ IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
33!!$ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
34!!$ OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
35!!$ OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
36!!$ USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
37!!$ THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
38!!$ DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
39!!$ UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
40!!$ BE CORRECTED.
41!!$
42!!$ 5. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
43!!$ HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
44!!$ ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
45!!$ INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
46!!$ ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
47!!$ PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
48!!$ SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
49!!$ (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
50!!$ EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
51!!$ POSSIBILITY OF SUCH LOSS OR DAMAGES.
52
53subroutine chkder ( m, n, x, fvec, fjac, ldfjac, xp, fvecp, mode, err )
54
55!*****************************************************************************80
56!
57!! CHKDER checks the gradients of M functions of N variables.
58!
59! Discussion:
60!
61! CHKDER checks the gradients of M nonlinear functions in N variables,
62! evaluated at a point X, for consistency with the functions themselves.
63!
64! The user calls CHKDER twice, first with MODE = 1 and then with MODE = 2.
65!
66! MODE = 1.
67! On input,
68! X contains the point of evaluation.
69! On output,
70! XP is set to a neighboring point.
71!
72! Now the user must evaluate the function and gradients at X, and the
73! function at XP. Then the subroutine is called again:
74!
75! MODE = 2.
76! On input,
77! FVEC contains the function values at X,
78! FJAC contains the function gradients at X.
79! FVECP contains the functions evaluated at XP.
80! On output,
81! ERR contains measures of correctness of the respective gradients.
82!
83! The subroutine does not perform reliably if cancellation or
84! rounding errors cause a severe loss of significance in the
85! evaluation of a function. Therefore, none of the components
86! of X should be unusually small (in particular, zero) or any
87! other value which may cause loss of significance.
88!
89! Licensing:
90!
91! This code is distributed under the GNU LGPL license.
92!
93! Modified:
94!
95! 06 April 2010
96!
97! Author:
98!
99! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
100! FORTRAN90 version by John Burkardt.
101!
102! Reference:
103!
104! Jorge More, Burton Garbow, Kenneth Hillstrom,
105! User Guide for MINPACK-1,
106! Technical Report ANL-80-74,
107! Argonne National Laboratory, 1980.
108!
109! Parameters:
110!
111! Input, integer ( kind = 4 ) M, is the number of functions.
112!
113! Input, integer ( kind = 4 ) N, is the number of variables.
114!
115! Input, real ( kind = 8 ) X(N), the point at which the jacobian is to be
116! evaluated.
117!
118! Input, real ( kind = 8 ) FVEC(M), is used only when MODE = 2.
119! In that case, it should contain the function values at X.
120!
121! Input, real ( kind = 8 ) FJAC(LDFJAC,N), an M by N array. When MODE = 2,
122! FJAC(I,J) should contain the value of dF(I)/dX(J).
123!
124! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of FJAC.
125! LDFJAC must be at least M.
126!
127! Output, real ( kind = 8 ) XP(N), on output with MODE = 1, is a neighboring
128! point of X, at which the function is to be evaluated.
129!
130! Input, real ( kind = 8 ) FVECP(M), on input with MODE = 2, is the function
131! value at XP.
132!
133! Input, integer ( kind = 4 ) MODE, should be set to 1 on the first call, and
134! 2 on the second.
135!
136! Output, real ( kind = 8 ) ERR(M). On output when MODE = 2, ERR contains
137! measures of correctness of the respective gradients. If there is no
138! severe loss of significance, then if ERR(I):
139! = 1.0D+00, the I-th gradient is correct,
140! = 0.0D+00, the I-th gradient is incorrect.
141! > 0.5D+00, the I-th gradient is probably correct.
142! < 0.5D+00, the I-th gradient is probably incorrect.
143!
144 implicit none
145
146 integer ( kind = 4 ), intent(in) :: ldfjac
147 integer ( kind = 4 ), intent(in) :: m
148 integer ( kind = 4 ), intent(in) :: n
149
150 real ( kind = 8 ) eps
151 real ( kind = 8 ) epsf
152 real ( kind = 8 ) epslog
153 real ( kind = 8 ) epsmch
154 real ( kind = 8 ), intent(out) :: err(m)
155 real ( kind = 8 ), intent(in) :: fjac(ldfjac,n)
156 real ( kind = 8 ), intent(in) :: fvec(m)
157 real ( kind = 8 ), intent(in) :: fvecp(m)
158 integer ( kind = 4 ) i
159 integer ( kind = 4 ) j
160 integer ( kind = 4 ), intent(in) :: mode
161 real ( kind = 8 ) temp
162 real ( kind = 8 ), intent(in) :: x(n)
163 real ( kind = 8 ), intent(out) :: xp(n)
164
165 epsmch = epsilon( epsmch )
166 eps = sqrt( epsmch )
167!
168! MODE = 1.
169!
170 if ( mode == 1 ) then
171
172 do j = 1, n
173 temp = eps * abs( x(j) )
174 if ( temp == 0.0d+00 ) then
175 temp = eps
176 end if
177 xp(j) = x(j) + temp
178 end do
179!
180! MODE = 2.
181!
182 else if ( mode == 2 ) then
183
184 epsf = 100.0d+00 * epsmch
185 epslog = log10( eps )
186
187 err = 0.0d+00
188
189 do j = 1, n
190 temp = abs( x(j) )
191 if ( temp == 0.0d+00 ) then
192 temp = 1.0d+00
193 end if
194 err(1:m) = err(1:m) + temp * fjac(1:m,j)
195 end do
196
197 do i = 1, m
198
199 temp = 1.0d+00
200
201 if ( fvec(i) /= 0.0d+00 .and. fvecp(i) /= 0.0d+00 .and. &
202 abs( fvecp(i)-fvec(i)) >= epsf * abs( fvec(i) ) ) then
203 temp = eps * abs( (fvecp(i)-fvec(i)) / eps - err(i) ) &
204 / ( abs( fvec(i) ) + abs( fvecp(i) ) )
205 end if
206
207 err(i) = 1.0d+00
208
209 if ( epsmch < temp .and. temp < eps ) then
210 err(i) = ( log10( temp ) - epslog ) / epslog
211 end if
212
213 if ( eps <= temp ) then
214 err(i) = 0.0d+00
215 end if
216
217 end do
218
219 end if
220
221 return
222end
223subroutine dogleg ( n, r, lr, diag, qtb, delta, x )
224
225!*****************************************************************************80
226!
227!! DOGLEG finds the minimizing combination of Gauss-Newton and gradient steps.
228!
229! Discussion:
230!
231! Given an M by N matrix A, an N by N nonsingular diagonal
232! matrix D, an M-vector B, and a positive number DELTA, the
233! problem is to determine the convex combination X of the
234! Gauss-Newton and scaled gradient directions that minimizes
235! (A*X - B) in the least squares sense, subject to the
236! restriction that the euclidean norm of D*X be at most DELTA.
237!
238! This subroutine completes the solution of the problem
239! if it is provided with the necessary information from the
240! QR factorization of A. That is, if A = Q*R, where Q has
241! orthogonal columns and R is an upper triangular matrix,
242! then DOGLEG expects the full upper triangle of R and
243! the first N components of Q'*B.
244!
245! Licensing:
246!
247! This code is distributed under the GNU LGPL license.
248!
249! Modified:
250!
251! 06 April 2010
252!
253! Author:
254!
255! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
256! FORTRAN90 version by John Burkardt.
257!
258! Reference:
259!
260! Jorge More, Burton Garbow, Kenneth Hillstrom,
261! User Guide for MINPACK-1,
262! Technical Report ANL-80-74,
263! Argonne National Laboratory, 1980.
264!
265! Parameters:
266!
267! Input, integer ( kind = 4 ) N, the order of the matrix R.
268!
269! Input, real ( kind = 8 ) R(LR), the upper triangular matrix R stored
270! by rows.
271!
272! Input, integer ( kind = 4 ) LR, the size of the R array, which must be
273! no less than (N*(N+1))/2.
274!
275! Input, real ( kind = 8 ) DIAG(N), the diagonal elements of the matrix D.
276!
277! Input, real ( kind = 8 ) QTB(N), the first N elements of the vector Q'* B.
278!
279! Input, real ( kind = 8 ) DELTA, is a positive upper bound on the
280! euclidean norm of D*X(1:N).
281!
282! Output, real ( kind = 8 ) X(N), the desired convex combination of the
283! Gauss-Newton direction and the scaled gradient direction.
284!
285 implicit none
286
287 integer ( kind = 4 ), intent(in) :: lr
288 integer ( kind = 4 ), intent(in) :: n
289
290 real ( kind = 8 ) alpha
291 real ( kind = 8 ) bnorm
292 real ( kind = 8 ), intent(in) :: delta
293 real ( kind = 8 ), intent(in) :: diag(n)
294 real ( kind = 8 ) enorm
295 real ( kind = 8 ) epsmch
296 real ( kind = 8 ) gnorm
297 integer ( kind = 4 ) i
298 integer ( kind = 4 ) j
299 integer ( kind = 4 ) jj
300 integer ( kind = 4 ) k
301 integer ( kind = 4 ) l
302 real ( kind = 8 ) qnorm
303 real ( kind = 8 ), intent(in) :: qtb(n)
304 real ( kind = 8 ), intent(in) :: r(lr)
305 real ( kind = 8 ) sgnorm
306 real ( kind = 8 ) sum2
307 real ( kind = 8 ) temp
308 real ( kind = 8 ) wa1(n)
309 real ( kind = 8 ) wa2(n)
310 real ( kind = 8 ), intent(out) :: x(n)
311
312 epsmch = epsilon( epsmch )
313!
314! Calculate the Gauss-Newton direction.
315!
316 jj = ( n * ( n + 1 ) ) / 2 + 1
317
318 do k = 1, n
319
320 j = n - k + 1
321 jj = jj - k
322 l = jj + 1
323 sum2 = 0.0d+00
324
325 do i = j + 1, n
326 sum2 = sum2 + r(l) * x(i)
327 l = l + 1
328 end do
329
330 temp = r(jj)
331
332 if ( temp == 0.0d+00 ) then
333
334 l = j
335 do i = 1, j
336 temp = max( temp, abs( r(l)) )
337 l = l + n - i
338 end do
339
340 if ( temp == 0.0d+00 ) then
341 temp = epsmch
342 else
343 temp = epsmch * temp
344 end if
345
346 end if
347
348 x(j) = ( qtb(j) - sum2 ) / temp
349
350 end do
351!
352! Test whether the Gauss-Newton direction is acceptable.
353!
354 wa1(1:n) = 0.0d+00
355 wa2(1:n) = diag(1:n) * x(1:n)
356 qnorm = enorm( n, wa2 )
357
358 if ( qnorm <= delta ) then
359 return
360 end if
361!
362! The Gauss-Newton direction is not acceptable.
363! Calculate the scaled gradient direction.
364!
365 l = 1
366 do j = 1, n
367 temp = qtb(j)
368 do i = j, n
369 wa1(i) = wa1(i) + r(l) * temp
370 l = l + 1
371 end do
372 wa1(j) = wa1(j) / diag(j)
373 end do
374!
375! Calculate the norm of the scaled gradient.
376! Test for the special case in which the scaled gradient is zero.
377!
378 gnorm = enorm( n, wa1 )
379 sgnorm = 0.0d+00
380 alpha = delta / qnorm
381
382 if ( gnorm /= 0.0d+00 ) then
383!
384! Calculate the point along the scaled gradient which minimizes the quadratic.
385!
386 wa1(1:n) = ( wa1(1:n) / gnorm ) / diag(1:n)
387
388 l = 1
389 do j = 1, n
390 sum2 = 0.0d+00
391 do i = j, n
392 sum2 = sum2 + r(l) * wa1(i)
393 l = l + 1
394 end do
395 wa2(j) = sum2
396 end do
397
398 temp = enorm( n, wa2 )
399 sgnorm = ( gnorm / temp ) / temp
400!
401! Test whether the scaled gradient direction is acceptable.
402!
403 alpha = 0.0d+00
404!
405! The scaled gradient direction is not acceptable.
406! Calculate the point along the dogleg at which the quadratic is minimized.
407!
408 if ( sgnorm < delta ) then
409
410 bnorm = enorm( n, qtb )
411 temp = ( bnorm / gnorm ) * ( bnorm / qnorm ) * ( sgnorm / delta )
412 temp = temp - ( delta / qnorm ) * ( sgnorm / delta) ** 2 &
413 + sqrt( ( temp - ( delta / qnorm ) ) ** 2 &
414 + ( 1.0d+00 - ( delta / qnorm ) ** 2 ) &
415 * ( 1.0d+00 - ( sgnorm / delta ) ** 2 ) )
416
417 alpha = ( ( delta / qnorm ) * ( 1.0d+00 - ( sgnorm / delta ) ** 2 ) ) &
418 / temp
419
420 end if
421
422 end if
423!
424! Form appropriate convex combination of the Gauss-Newton
425! direction and the scaled gradient direction.
426!
427 temp = ( 1.0d+00 - alpha ) * min( sgnorm, delta )
428
429 x(1:n) = temp * wa1(1:n) + alpha * x(1:n)
430
431 return
432end
433function enorm ( n, x )
434
435!*****************************************************************************80
436!
437!! ENORM computes the Euclidean norm of a vector.
438!
439! Discussion:
440!
441! This is an extremely simplified version of the original ENORM
442! routine, which has been renamed to "ENORM2".
443!
444! Licensing:
445!
446! This code is distributed under the GNU LGPL license.
447!
448! Modified:
449!
450! 06 April 2010
451!
452! Author:
453!
454! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
455! FORTRAN90 version by John Burkardt.
456!
457! Reference:
458!
459! Jorge More, Burton Garbow, Kenneth Hillstrom,
460! User Guide for MINPACK-1,
461! Technical Report ANL-80-74,
462! Argonne National Laboratory, 1980.
463!
464! Parameters:
465!
466! Input, integer ( kind = 4 ) N, is the length of the vector.
467!
468! Input, real ( kind = 8 ) X(N), the vector whose norm is desired.
469!
470! Output, real ( kind = 8 ) ENORM, the Euclidean norm of the vector.
471!
472 implicit none
473
474 integer ( kind = 4 ), intent(in) :: n
475 real ( kind = 8 ), intent(in) :: x(n)
476 real ( kind = 8 ) enorm
477
478 enorm = sqrt( sum( x(1:n) ** 2 ))
479
480 return
481end
482function enorm2 ( n, x )
483
484!*****************************************************************************80
485!
486!! ENORM2 computes the Euclidean norm of a vector.
487!
488! Discussion:
489!
490! This routine was named ENORM. It has been renamed "ENORM2",
491! and a simplified routine has been substituted.
492!
493! The Euclidean norm is computed by accumulating the sum of
494! squares in three different sums. The sums of squares for the
495! small and large components are scaled so that no overflows
496! occur. Non-destructive underflows are permitted. Underflows
497! and overflows do not occur in the computation of the unscaled
498! sum of squares for the intermediate components.
499!
500! The definitions of small, intermediate and large components
501! depend on two constants, RDWARF and RGIANT. The main
502! restrictions on these constants are that RDWARF^2 not
503! underflow and RGIANT^2 not overflow.
504!
505! Licensing:
506!
507! This code is distributed under the GNU LGPL license.
508!
509! Modified:
510!
511! 06 April 2010
512!
513! Author:
514!
515! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
516! FORTRAN90 version by John Burkardt.
517!
518! Reference:
519!
520! Jorge More, Burton Garbow, Kenneth Hillstrom,
521! User Guide for MINPACK-1
522! Argonne National Laboratory,
523! Argonne, Illinois.
524!
525! Parameters:
526!
527! Input, integer ( kind = 4 ) N, is the length of the vector.
528!
529! Input, real ( kind = 8 ) X(N), the vector whose norm is desired.
530!
531! Output, real ( kind = 8 ) ENORM2, the Euclidean norm of the vector.
532!
533 implicit none
534
535 integer ( kind = 4 ), intent(in) :: n
536
537 real ( kind = 8 ) agiant
538 real ( kind = 8 ) enorm2
539 integer ( kind = 4 ) i
540 real ( kind = 8 ) rdwarf
541 real ( kind = 8 ) rgiant
542 real ( kind = 8 ) s1
543 real ( kind = 8 ) s2
544 real ( kind = 8 ) s3
545 real ( kind = 8 ), intent(in) :: x(n)
546 real ( kind = 8 ) xabs
547 real ( kind = 8 ) x1max
548 real ( kind = 8 ) x3max
549
550 rdwarf = sqrt( tiny( rdwarf ) )
551 rgiant = sqrt( huge( rgiant ) )
552
553 s1 = 0.0d+00
554 s2 = 0.0d+00
555 s3 = 0.0d+00
556 x1max = 0.0d+00
557 x3max = 0.0d+00
558 agiant = rgiant / real( n, kind = 8 )
559
560 do i = 1, n
561
562 xabs = abs( x(i) )
563
564 if ( xabs <= rdwarf ) then
565
566 if ( x3max < xabs ) then
567 s3 = 1.0d+00 + s3 * ( x3max / xabs ) ** 2
568 x3max = xabs
569 else if ( xabs /= 0.0d+00 ) then
570 s3 = s3 + ( xabs / x3max ) ** 2
571 end if
572
573 else if ( agiant <= xabs ) then
574
575 if ( x1max < xabs ) then
576 s1 = 1.0d+00 + s1 * ( x1max / xabs ) ** 2
577 x1max = xabs
578 else
579 s1 = s1 + ( xabs / x1max ) ** 2
580 end if
581
582 else
583
584 s2 = s2 + xabs ** 2
585
586 end if
587
588 end do
589!
590! Calculation of norm.
591!
592 if ( s1 /= 0.0d+00 ) then
593
594 enorm2 = x1max * sqrt( s1 + ( s2 / x1max ) / x1max )
595
596 else if ( s2 /= 0.0d+00 ) then
597
598 if ( x3max <= s2 ) then
599 enorm2 = sqrt( s2 * ( 1.0d+00 + ( x3max / s2 ) * ( x3max * s3 ) ) )
600 else
601 enorm2 = sqrt( x3max * ( ( s2 / x3max ) + ( x3max * s3 ) ) )
602 end if
603
604 else
605
606 enorm2 = x3max * sqrt( s3 )
607
608 end if
609
610 return
611end
612subroutine fdjac1 ( fcn, n, x, fvec, fjac, ldfjac, iflag, ml, mu, epsfcn )
613
614!*****************************************************************************80
615!
616!! FDJAC1 estimates an N by N jacobian matrix using forward differences.
617!
618! Discussion:
619!
620! This subroutine computes a forward-difference approximation
621! to the N by N jacobian matrix associated with a specified
622! problem of N functions in N variables. If the jacobian has
623! a banded form, then function evaluations are saved by only
624! approximating the nonzero terms.
625!
626! Licensing:
627!
628! This code is distributed under the GNU LGPL license.
629!
630! Modified:
631!
632! 06 April 2010
633!
634! Author:
635!
636! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
637! FORTRAN90 version by John Burkardt.
638!
639! Reference:
640!
641! Jorge More, Burton Garbow, Kenneth Hillstrom,
642! User Guide for MINPACK-1,
643! Technical Report ANL-80-74,
644! Argonne National Laboratory, 1980.
645!
646! Parameters:
647!
648! Input, external FCN, the name of the user-supplied subroutine which
649! calculates the functions. The routine should have the form:
650! subroutine fcn ( n, x, fvec, iflag )
651! integer ( kind = 4 ) n
652! real ( kind = 8 ) fvec(n)
653! integer ( kind = 4 ) iflag
654! real ( kind = 8 ) x(n)
655! If IFLAG = 0 on input, then FCN is only being called to allow the user
656! to print out the current iterate.
657! If IFLAG = 1 on input, FCN should calculate the functions at X and
658! return this vector in FVEC.
659! To terminate the algorithm, FCN may set IFLAG negative on return.
660!
661! Input, integer ( kind = 4 ) N, the number of functions and variables.
662!
663! Input, real ( kind = 8 ) X(N), the point where the jacobian is evaluated.
664!
665! Input, real ( kind = 8 ) FVEC(N), the functions evaluated at X.
666!
667! Output, real ( kind = 8 ) FJAC(LDFJAC,N), the N by N approximate
668! jacobian matrix.
669!
670! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of FJAC, which
671! must not be less than N.
672!
673! Output, integer ( kind = 4 ) IFLAG, is an error flag returned by FCN.
674! If FCN returns a nonzero value of IFLAG, then this routine returns
675! immediately to the calling program, with the value of IFLAG.
676!
677! Input, integer ( kind = 4 ) ML, MU, specify the number of subdiagonals and
678! superdiagonals within the band of the jacobian matrix. If the
679! jacobian is not banded, set ML and MU to N-1.
680!
681! Input, real ( kind = 8 ) EPSFCN, is used in determining a suitable step
682! length for the forward-difference approximation. This approximation
683! assumes that the relative errors in the functions are of the order of
684! EPSFCN. If EPSFCN is less than the machine precision, it is assumed that
685! the relative errors in the functions are of the order of the machine
686! precision.
687!
688 implicit none
689
690 integer ( kind = 4 ), intent(in) :: ldfjac
691 integer ( kind = 4 ), intent(in) :: n
692
693 real ( kind = 8 ) eps
694 real ( kind = 8 ), intent(in) :: epsfcn
695 real ( kind = 8 ) epsmch
696 external fcn
697 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
698 real ( kind = 8 ), intent(in) :: fvec(n)
699 real ( kind = 8 ) h
700 integer ( kind = 4 ) i
701 integer ( kind = 4 ), intent(out) :: iflag
702 integer ( kind = 4 ) j
703 integer ( kind = 4 ) k
704 integer ( kind = 4 ), intent(in) :: ml
705 integer ( kind = 4 ) msum
706 integer ( kind = 4 ), intent(in) :: mu
707 real ( kind = 8 ) temp
708 real ( kind = 8 ) wa1(n)
709 real ( kind = 8 ) wa2(n)
710 real ( kind = 8 ), intent(inout) :: x(n)
711
712 epsmch = epsilon( epsmch )
713
714 eps = sqrt( max( epsfcn, epsmch ) )
715 msum = ml + mu + 1
716!
717! Computation of dense approximate jacobian.
718!
719 if ( n <= msum ) then
720
721 do j = 1, n
722
723 temp = x(j)
724 h = eps * abs( temp )
725 if ( h == 0.0d+00 ) then
726 h = eps
727 end if
728
729 iflag = 1
730 x(j) = temp + h
731 call fcn ( n, x, wa1, iflag )
732
733 if ( iflag < 0 ) then
734 exit
735 end if
736
737 x(j) = temp
738 fjac(1:n,j) = ( wa1(1:n) - fvec(1:n) ) / h
739
740 end do
741
742 else
743!
744! Computation of banded approximate jacobian.
745!
746 do k = 1, msum
747
748 do j = k, n, msum
749 wa2(j) = x(j)
750 h = eps * abs( wa2(j) )
751 if ( h == 0.0d+00 ) then
752 h = eps
753 end if
754 x(j) = wa2(j) + h
755 end do
756
757 iflag = 1
758 call fcn ( n, x, wa1, iflag )
759
760 if ( iflag < 0 ) then
761 exit
762 end if
763
764 do j = k, n, msum
765
766 x(j) = wa2(j)
767
768 h = eps * abs( wa2(j) )
769 if ( h == 0.0d+00 ) then
770 h = eps
771 end if
772
773 fjac(1:n,j) = 0.0d+00
774
775 do i = 1, n
776 if ( j - mu <= i .and. i <= j + ml ) then
777 fjac(i,j) = ( wa1(i) - fvec(i) ) / h
778 end if
779 end do
780
781 end do
782
783 end do
784
785 end if
786
787 return
788end
789subroutine fdjac2 ( fcn, m, n, x, fvec, fjac, ldfjac, iflag, epsfcn )
790
791!*****************************************************************************80
792!
793!! FDJAC2 estimates an M by N jacobian matrix using forward differences.
794!
795! Discussion:
796!
797! This subroutine computes a forward-difference approximation
798! to the M by N jacobian matrix associated with a specified
799! problem of M functions in N variables.
800!
801! Licensing:
802!
803! This code is distributed under the GNU LGPL license.
804!
805! Modified:
806!
807! 06 April 2010
808!
809! Author:
810!
811! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
812! FORTRAN90 version by John Burkardt.
813!
814! Reference:
815!
816! Jorge More, Burton Garbow, Kenneth Hillstrom,
817! User Guide for MINPACK-1,
818! Technical Report ANL-80-74,
819! Argonne National Laboratory, 1980.
820!
821! Parameters:
822!
823! Input, external FCN, the name of the user-supplied subroutine which
824! calculates the functions. The routine should have the form:
825! subroutine fcn ( m, n, x, fvec, iflag )
826! integer ( kind = 4 ) n
827! real ( kind = 8 ) fvec(m)
828! integer ( kind = 4 ) iflag
829! real ( kind = 8 ) x(n)
830!
831! If IFLAG = 0 on input, then FCN is only being called to allow the user
832! to print out the current iterate.
833! If IFLAG = 1 on input, FCN should calculate the functions at X and
834! return this vector in FVEC.
835! To terminate the algorithm, FCN may set IFLAG negative on return.
836!
837! Input, integer ( kind = 4 ) M, is the number of functions.
838!
839! Input, integer ( kind = 4 ) N, is the number of variables.
840! N must not exceed M.
841!
842! Input, real ( kind = 8 ) X(N), the point where the jacobian is evaluated.
843!
844! Input, real ( kind = 8 ) FVEC(M), the functions evaluated at X.
845!
846! Output, real ( kind = 8 ) FJAC(LDFJAC,N), the M by N approximate
847! jacobian matrix.
848!
849! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of FJAC,
850! which must not be less than M.
851!
852! Output, integer ( kind = 4 ) IFLAG, is an error flag returned by FCN.
853! If FCN returns a nonzero value of IFLAG, then this routine returns
854! immediately to the calling program, with the value of IFLAG.
855!
856! Input, real ( kind = 8 ) EPSFCN, is used in determining a suitable
857! step length for the forward-difference approximation. This approximation
858! assumes that the relative errors in the functions are of the order of
859! EPSFCN. If EPSFCN is less than the machine precision, it is assumed that
860! the relative errors in the functions are of the order of the machine
861! precision.
862!
863 implicit none
864
865 integer ( kind = 4 ), intent(in) :: ldfjac
866 integer ( kind = 4 ), intent(in) :: m
867 integer ( kind = 4 ), intent(in) :: n
868
869 real ( kind = 8 ) eps
870 real ( kind = 8 ), intent(in) :: epsfcn
871 real ( kind = 8 ) epsmch
872 external fcn
873 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
874 real ( kind = 8 ), intent(in) :: fvec(m)
875 real ( kind = 8 ) h
876 integer ( kind = 4 ), intent(out) :: iflag
877 integer ( kind = 4 ) j
878 real ( kind = 8 ) temp
879 real ( kind = 8 ) wa(m)
880 real ( kind = 8 ), intent(inout) :: x(n)
881
882 epsmch = epsilon( epsmch )
883
884 eps = sqrt( max( epsfcn, epsmch ) )
885
886 do j = 1, n
887
888 temp = x(j)
889 h = eps * abs( temp )
890 if ( h == 0.0d+00 ) then
891 h = eps
892 end if
893
894 iflag = 1
895 x(j) = temp + h
896 call fcn ( m, n, x, wa, iflag )
897
898 if ( iflag < 0 ) then
899 exit
900 end if
901
902 x(j) = temp
903 fjac(1:m,j) = ( wa(1:m) - fvec(1:m) ) / h
904
905 end do
906
907 return
908end
909subroutine hybrd ( fcn, n, x, fvec, xtol, maxfev, ml, mu, epsfcn, diag, mode, &
910 factor, nprint, info, nfev, fjac, ldfjac, r, lr, qtf )
911
912!*****************************************************************************80
913!
914!! HYBRD seeks a zero of N nonlinear equations in N variables.
915!
916! Discussion:
917!
918! HYBRD finds a zero of a system of N nonlinear functions in N variables
919! by a modification of the Powell hybrid method. The user must provide a
920! subroutine which calculates the functions. The jacobian is
921! then calculated by a forward-difference approximation.
922!
923! Licensing:
924!
925! This code is distributed under the GNU LGPL license.
926!
927! Modified:
928!
929! 06 April 2010
930!
931! Author:
932!
933! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
934! FORTRAN90 version by John Burkardt.
935!
936! Reference:
937!
938! Jorge More, Burton Garbow, Kenneth Hillstrom,
939! User Guide for MINPACK-1,
940! Technical Report ANL-80-74,
941! Argonne National Laboratory, 1980.
942!
943! Parameters:
944!
945! Input, external FCN, the name of the user-supplied subroutine which
946! calculates the functions. The routine should have the form:
947! subroutine fcn ( n, x, fvec, iflag )
948! integer ( kind = 4 ) n
949! real ( kind = 8 ) fvec(n)
950! integer ( kind = 4 ) iflag
951! real ( kind = 8 ) x(n)
952!
953! If IFLAG = 0 on input, then FCN is only being called to allow the user
954! to print out the current iterate.
955! If IFLAG = 1 on input, FCN should calculate the functions at X and
956! return this vector in FVEC.
957! To terminate the algorithm, FCN may set IFLAG negative on return.
958!
959! Input, integer ( kind = 4 ) N, the number of functions and variables.
960!
961! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
962! estimate of the solution vector. On output X contains the final
963! estimate of the solution vector.
964!
965! Output, real ( kind = 8 ) FVEC(N), the functions evaluated at the output X.
966!
967! Input, real ( kind = 8 ) XTOL. Termination occurs when the relative error
968! between two consecutive iterates is at most XTOL. XTOL should be
969! nonnegative.
970!
971! Input, integer ( kind = 4 ) MAXFEV. Termination occurs when the number of
972! calls to FCN is at least MAXFEV by the end of an iteration.
973!
974! Input, integer ( kind = 4 ) ML, MU, specify the number of subdiagonals and
975! superdiagonals within the band of the jacobian matrix. If the jacobian
976! is not banded, set ML and MU to at least n - 1.
977!
978! Input, real ( kind = 8 ) EPSFCN, is used in determining a suitable step
979! length for the forward-difference approximation. This approximation
980! assumes that the relative errors in the functions are of the order of
981! EPSFCN. If EPSFCN is less than the machine precision, it is assumed that
982! the relative errors in the functions are of the order of the machine
983! precision.
984!
985! Input/output, real ( kind = 8 ) DIAG(N). If MODE = 1, then DIAG is set
986! internally. If MODE = 2, then DIAG must contain positive entries that
987! serve as multiplicative scale factors for the variables.
988!
989! Input, integer ( kind = 4 ) MODE, scaling option.
990! 1, variables will be scaled internally.
991! 2, scaling is specified by the input DIAG vector.
992!
993! Input, real ( kind = 8 ) FACTOR, determines the initial step bound. This
994! bound is set to the product of FACTOR and the euclidean norm of DIAG*X if
995! nonzero, or else to FACTOR itself. In most cases, FACTOR should lie
996! in the interval (0.1, 100) with 100 the recommended value.
997!
998! Input, integer ( kind = 4 ) NPRINT, enables controlled printing of
999! iterates if it is positive. In this case, FCN is called with IFLAG = 0 at
1000! the beginning of the first iteration and every NPRINT iterations thereafter
1001! and immediately prior to return, with X and FVEC available
1002! for printing. If NPRINT is not positive, no special calls
1003! of FCN with IFLAG = 0 are made.
1004!
1005! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
1006! execution, INFO is set to the (negative) value of IFLAG.
1007! See the description of FCN.
1008! Otherwise, INFO is set as follows:
1009! 0, improper input parameters.
1010! 1, relative error between two consecutive iterates is at most XTOL.
1011! 2, number of calls to FCN has reached or exceeded MAXFEV.
1012! 3, XTOL is too small. No further improvement in the approximate
1013! solution X is possible.
1014! 4, iteration is not making good progress, as measured by the improvement
1015! from the last five jacobian evaluations.
1016! 5, iteration is not making good progress, as measured by the improvement
1017! from the last ten iterations.
1018!
1019! Output, integer ( kind = 4 ) NFEV, the number of calls to FCN.
1020!
1021! Output, real ( kind = 8 ) FJAC(LDFJAC,N), an N by N array which contains
1022! the orthogonal matrix Q produced by the QR factorization of the final
1023! approximate jacobian.
1024!
1025! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of FJAC.
1026! LDFJAC must be at least N.
1027!
1028! Output, real ( kind = 8 ) R(LR), the upper triangular matrix produced by
1029! the QR factorization of the final approximate jacobian, stored rowwise.
1030!
1031! Input, integer ( kind = 4 ) LR, the size of the R array, which must be no
1032! less than (N*(N+1))/2.
1033!
1034! Output, real ( kind = 8 ) QTF(N), contains the vector Q'*FVEC.
1035!
1036 implicit none
1037
1038 integer ( kind = 4 ), intent(in) :: ldfjac
1039 integer ( kind = 4 ), intent(in) :: lr
1040 integer ( kind = 4 ), intent(in) :: n
1041
1042 real ( kind = 8 ) actred
1043 real ( kind = 8 ) delta
1044 real ( kind = 8 ), intent(inout) :: diag(n)
1045 real ( kind = 8 ) enorm
1046 real ( kind = 8 ), intent(in) :: epsfcn
1047 real ( kind = 8 ) epsmch
1048 real ( kind = 8 ), intent(in) :: factor
1049 external fcn
1050 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
1051 real ( kind = 8 ) fnorm
1052 real ( kind = 8 ) fnorm1
1053 real ( kind = 8 ), intent(out) :: fvec(n)
1054 integer ( kind = 4 ) i
1055 integer ( kind = 4 ) iflag
1056 integer ( kind = 4 ), intent(out) :: info
1057 integer ( kind = 4 ) iter
1058 integer ( kind = 4 ) iwa(1)
1059 integer ( kind = 4 ) j
1060 logical jeval
1061 integer ( kind = 4 ) l
1062 integer ( kind = 4 ), intent(in) :: maxfev
1063 integer ( kind = 4 ), intent(in) :: ml
1064 integer ( kind = 4 ), intent(in) :: mode
1065 integer ( kind = 4 ) msum
1066 integer ( kind = 4 ), intent(in) :: mu
1067 integer ( kind = 4 ) ncfail
1068 integer ( kind = 4 ) nslow1
1069 integer ( kind = 4 ) nslow2
1070 integer ( kind = 4 ) ncsuc
1071 integer ( kind = 4 ), intent(out) :: nfev
1072 integer ( kind = 4 ), intent(in) :: nprint
1073 logical pivot
1074 real ( kind = 8 ) pnorm
1075 real ( kind = 8 ) prered
1076 real ( kind = 8 ), intent(out) :: qtf(n)
1077 real ( kind = 8 ), intent(out) :: r(lr)
1078 real ( kind = 8 ) ratio
1079 logical sing
1080 real ( kind = 8 ) sum2
1081 real ( kind = 8 ) temp
1082 real ( kind = 8 ) wa1(n)
1083 real ( kind = 8 ) wa2(n)
1084 real ( kind = 8 ) wa3(n)
1085 real ( kind = 8 ) wa4(n)
1086 real ( kind = 8 ), intent(inout) :: x(n)
1087 real ( kind = 8 ) xnorm
1088 real ( kind = 8 ), intent(in) :: xtol
1089
1090 epsmch = epsilon( epsmch )
1091
1092 info = 0
1093 iflag = 0
1094 nfev = 0
1095!
1096! Check the input parameters for errors.
1097!
1098 if ( n <= 0 ) then
1099 return
1100 else if ( xtol < 0.0d+00 ) then
1101 return
1102 else if ( maxfev <= 0 ) then
1103 return
1104 else if ( ml < 0 ) then
1105 return
1106 else if ( mu < 0 ) then
1107 return
1108 else if ( factor <= 0.0d+00 ) then
1109 return
1110 else if ( ldfjac < n ) then
1111 return
1112 else if ( lr < ( n * ( n + 1 ) ) / 2 ) then
1113 return
1114 end if
1115
1116 if ( mode == 2 ) then
1117
1118 do j = 1, n
1119 if ( diag(j) <= 0.0d+00 ) then
1120 go to 300
1121 end if
1122 end do
1123
1124 end if
1125!
1126! Evaluate the function at the starting point
1127! and calculate its norm.
1128!
1129 iflag = 1
1130 call fcn ( n, x, fvec, iflag )
1131 nfev = 1
1132
1133 if ( iflag < 0 ) then
1134 go to 300
1135 end if
1136
1137 fnorm = enorm( n, fvec )
1138!
1139! Determine the number of calls to FCN needed to compute the jacobian matrix.
1140!
1141 msum = min( ml + mu + 1, n )
1142!
1143! Initialize iteration counter and monitors.
1144!
1145 iter = 1
1146 ncsuc = 0
1147 ncfail = 0
1148 nslow1 = 0
1149 nslow2 = 0
1150!
1151! Beginning of the outer loop.
1152!
115330 continue
1154
1155 jeval = .true.
1156!
1157! Calculate the jacobian matrix.
1158!
1159 iflag = 2
1160 call fdjac1 ( fcn, n, x, fvec, fjac, ldfjac, iflag, ml, mu, epsfcn )
1161
1162 nfev = nfev + msum
1163
1164 if ( iflag < 0 ) then
1165 go to 300
1166 end if
1167!
1168! Compute the QR factorization of the jacobian.
1169!
1170 pivot = .false.
1171 call qrfac ( n, n, fjac, ldfjac, pivot, iwa, 1, wa1, wa2 )
1172!
1173! On the first iteration, if MODE is 1, scale according
1174! to the norms of the columns of the initial jacobian.
1175!
1176 if ( iter == 1 ) then
1177
1178 if ( mode /= 2 ) then
1179
1180 diag(1:n) = wa2(1:n)
1181 do j = 1, n
1182 if ( wa2(j) == 0.0d+00 ) then
1183 diag(j) = 1.0d+00
1184 end if
1185 end do
1186
1187 end if
1188!
1189! On the first iteration, calculate the norm of the scaled X
1190! and initialize the step bound DELTA.
1191!
1192 wa3(1:n) = diag(1:n) * x(1:n)
1193 xnorm = enorm( n, wa3 )
1194 delta = factor * xnorm
1195 if ( delta == 0.0d+00 ) then
1196 delta = factor
1197 end if
1198
1199 end if
1200!
1201! Form Q' * FVEC and store in QTF.
1202!
1203 qtf(1:n) = fvec(1:n)
1204
1205 do j = 1, n
1206
1207 if ( fjac(j,j) /= 0.0d+00 ) then
1208 temp = - dot_product( qtf(j:n), fjac(j:n,j) ) / fjac(j,j)
1209 qtf(j:n) = qtf(j:n) + fjac(j:n,j) * temp
1210 end if
1211
1212 end do
1213!
1214! Copy the triangular factor of the QR factorization into R.
1215!
1216 sing = .false.
1217
1218 do j = 1, n
1219 l = j
1220 do i = 1, j - 1
1221 r(l) = fjac(i,j)
1222 l = l + n - i
1223 end do
1224 r(l) = wa1(j)
1225 if ( wa1(j) == 0.0d+00 ) then
1226 sing = .true.
1227 end if
1228 end do
1229!
1230! Accumulate the orthogonal factor in FJAC.
1231!
1232 call qform ( n, n, fjac, ldfjac )
1233!
1234! Rescale if necessary.
1235!
1236 if ( mode /= 2 ) then
1237 do j = 1, n
1238 diag(j) = max( diag(j), wa2(j) )
1239 end do
1240 end if
1241!
1242! Beginning of the inner loop.
1243!
1244180 continue
1245!
1246! If requested, call FCN to enable printing of iterates.
1247!
1248 if ( 0 < nprint ) then
1249 iflag = 0
1250 if ( mod( iter - 1, nprint ) == 0 ) then
1251 call fcn ( n, x, fvec, iflag )
1252 end if
1253 if ( iflag < 0 ) then
1254 go to 300
1255 end if
1256 end if
1257!
1258! Determine the direction P.
1259!
1260 call dogleg ( n, r, lr, diag, qtf, delta, wa1 )
1261!
1262! Store the direction P and X + P.
1263! Calculate the norm of P.
1264!
1265 wa1(1:n) = - wa1(1:n)
1266 wa2(1:n) = x(1:n) + wa1(1:n)
1267 wa3(1:n) = diag(1:n) * wa1(1:n)
1268
1269 pnorm = enorm( n, wa3 )
1270!
1271! On the first iteration, adjust the initial step bound.
1272!
1273 if ( iter == 1 ) then
1274 delta = min( delta, pnorm )
1275 end if
1276!
1277! Evaluate the function at X + P and calculate its norm.
1278!
1279 iflag = 1
1280 call fcn ( n, wa2, wa4, iflag )
1281 nfev = nfev + 1
1282
1283 if ( iflag < 0 ) then
1284 go to 300
1285 end if
1286
1287 fnorm1 = enorm( n, wa4 )
1288!
1289! Compute the scaled actual reduction.
1290!
1291 actred = -1.0d+00
1292 if ( fnorm1 < fnorm ) then
1293 actred = 1.0d+00 - ( fnorm1 / fnorm ) ** 2
1294 endif
1295!
1296! Compute the scaled predicted reduction.
1297!
1298 l = 1
1299 do i = 1, n
1300 sum2 = 0.0d+00
1301 do j = i, n
1302 sum2 = sum2 + r(l) * wa1(j)
1303 l = l + 1
1304 end do
1305 wa3(i) = qtf(i) + sum2
1306 end do
1307
1308 temp = enorm( n, wa3 )
1309 prered = 0.0d+00
1310 if ( temp < fnorm ) then
1311 prered = 1.0d+00 - ( temp / fnorm ) ** 2
1312 end if
1313!
1314! Compute the ratio of the actual to the predicted reduction.
1315!
1316 ratio = 0.0d+00
1317 if ( 0.0d+00 < prered ) then
1318 ratio = actred / prered
1319 end if
1320!
1321! Update the step bound.
1322!
1323 if ( ratio < 0.1d+00 ) then
1324
1325 ncsuc = 0
1326 ncfail = ncfail + 1
1327 delta = 0.5d+00 * delta
1328
1329 else
1330
1331 ncfail = 0
1332 ncsuc = ncsuc + 1
1333
1334 if ( 0.5d+00 <= ratio .or. 1 < ncsuc ) then
1335 delta = max( delta, pnorm / 0.5d+00 )
1336 end if
1337
1338 if ( abs( ratio - 1.0d+00 ) <= 0.1d+00 ) then
1339 delta = pnorm / 0.5d+00
1340 end if
1341
1342 end if
1343!
1344! Test for successful iteration.
1345!
1346! Successful iteration.
1347! Update X, FVEC, and their norms.
1348!
1349 if ( 0.0001d+00 <= ratio ) then
1350 x(1:n) = wa2(1:n)
1351 wa2(1:n) = diag(1:n) * x(1:n)
1352 fvec(1:n) = wa4(1:n)
1353 xnorm = enorm( n, wa2 )
1354 fnorm = fnorm1
1355 iter = iter + 1
1356 end if
1357!
1358! Determine the progress of the iteration.
1359!
1360 nslow1 = nslow1 + 1
1361 if ( 0.001d+00 <= actred ) then
1362 nslow1 = 0
1363 end if
1364
1365 if ( jeval ) then
1366 nslow2 = nslow2 + 1
1367 end if
1368
1369 if ( 0.1d+00 <= actred ) then
1370 nslow2 = 0
1371 end if
1372!
1373! Test for convergence.
1374!
1375 if ( delta <= xtol * xnorm .or. fnorm == 0.0d+00 ) then
1376 info = 1
1377 end if
1378
1379 if ( info /= 0 ) then
1380 go to 300
1381 end if
1382!
1383! Tests for termination and stringent tolerances.
1384!
1385 if ( maxfev <= nfev ) then
1386 info = 2
1387 end if
1388
1389 if ( 0.1d+00 * max( 0.1d+00 * delta, pnorm ) <= epsmch * xnorm ) then
1390 info = 3
1391 end if
1392
1393 if ( nslow2 == 5 ) then
1394 info = 4
1395 end if
1396
1397 if ( nslow1 == 10 ) then
1398 info = 5
1399 end if
1400
1401 if ( info /= 0 ) then
1402 go to 300
1403 end if
1404!
1405! Criterion for recalculating jacobian approximation
1406! by forward differences.
1407!
1408 if ( ncfail == 2 ) then
1409 go to 290
1410 end if
1411!
1412! Calculate the rank one modification to the jacobian
1413! and update QTF if necessary.
1414!
1415 do j = 1, n
1416 sum2 = dot_product( wa4(1:n), fjac(1:n,j) )
1417 wa2(j) = ( sum2 - wa3(j) ) / pnorm
1418 wa1(j) = diag(j) * ( ( diag(j) * wa1(j) ) / pnorm )
1419 if ( 0.0001d+00 <= ratio ) then
1420 qtf(j) = sum2
1421 end if
1422 end do
1423!
1424! Compute the QR factorization of the updated jacobian.
1425!
1426 call r1updt ( n, n, r, lr, wa1, wa2, wa3, sing )
1427 call r1mpyq ( n, n, fjac, ldfjac, wa2, wa3 )
1428 call r1mpyq ( 1, n, qtf, 1, wa2, wa3 )
1429!
1430! End of the inner loop.
1431!
1432 jeval = .false.
1433 go to 180
1434
1435 290 continue
1436!
1437! End of the outer loop.
1438!
1439 go to 30
1440
1441 300 continue
1442!
1443! Termination, either normal or user imposed.
1444!
1445 if ( iflag < 0 ) then
1446 info = iflag
1447 end if
1448
1449 iflag = 0
1450
1451 if ( 0 < nprint ) then
1452 call fcn ( n, x, fvec, iflag )
1453 end if
1454
1455 return
1456end
1457subroutine hybrd1 ( fcn, n, x, fvec, tol, info )
1458
1459!*****************************************************************************80
1460!
1461!! HYBRD1 seeks a zero of N nonlinear equations in N variables.
1462!
1463! Discussion:
1464!
1465! HYBRD1 finds a zero of a system of N nonlinear functions in N variables
1466! by a modification of the Powell hybrid method. This is done by using the
1467! more general nonlinear equation solver HYBRD. The user must provide a
1468! subroutine which calculates the functions. The jacobian is then
1469! calculated by a forward-difference approximation.
1470!
1471! Licensing:
1472!
1473! This code is distributed under the GNU LGPL license.
1474!
1475! Modified:
1476!
1477! 19 August 2016
1478!
1479! Author:
1480!
1481! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
1482! FORTRAN90 version by John Burkardt.
1483!
1484! Reference:
1485!
1486! Jorge More, Burton Garbow, Kenneth Hillstrom,
1487! User Guide for MINPACK-1,
1488! Technical Report ANL-80-74,
1489! Argonne National Laboratory, 1980.
1490!
1491! Parameters:
1492!
1493! Input, external FCN, the name of the user-supplied subroutine which
1494! calculates the functions. The routine should have the form:
1495! subroutine fcn ( n, x, fvec, iflag )
1496! integer ( kind = 4 ) n
1497! real ( kind = 8 ) fvec(n)
1498! integer ( kind = 4 ) iflag
1499! real ( kind = 8 ) x(n)
1500! If IFLAG = 0 on input, then FCN is only being called to allow the user
1501! to print out the current iterate.
1502! If IFLAG = 1 on input, FCN should calculate the functions at X and
1503! return this vector in FVEC.
1504! To terminate the algorithm, FCN may set IFLAG negative on return.
1505!
1506! Input, integer ( kind = 4 ) N, the number of functions and variables.
1507!
1508! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
1509! estimate of the solution vector. On output X contains the final
1510! estimate of the solution vector.
1511!
1512! Output, real ( kind = 8 ) FVEC(N), the functions evaluated at the output X.
1513!
1514! Input, real ( kind = 8 ) TOL. Termination occurs when the algorithm
1515! estimates that the relative error between X and the solution is at
1516! most TOL. TOL should be nonnegative.
1517!
1518! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
1519! execution, INFO is set to the (negative) value of IFLAG. See the
1520! description of FCN.
1521! Otherwise, INFO is set as follows:
1522! 0, improper input parameters.
1523! 1, algorithm estimates that the relative error between X and the
1524! solution is at most TOL.
1525! 2, number of calls to FCN has reached or exceeded 200*(N+1).
1526! 3, TOL is too small. No further improvement in the approximate
1527! solution X is possible.
1528! 4, the iteration is not making good progress.
1529!
1530 implicit none
1531
1532 integer ( kind = 4 ), intent(in) :: n
1533
1534 real ( kind = 8 ) diag(n)
1535 real ( kind = 8 ) epsfcn
1536 real ( kind = 8 ) factor
1537 external fcn
1538 real ( kind = 8 ) fjac(n,n)
1539 real ( kind = 8 ), intent(out) :: fvec(n)
1540 integer ( kind = 4 ), intent(out) :: info
1541 integer ( kind = 4 ) ldfjac
1542 integer ( kind = 4 ) lr
1543 integer ( kind = 4 ) maxfev
1544 integer ( kind = 4 ) ml
1545 integer ( kind = 4 ) mode
1546 integer ( kind = 4 ) mu
1547 integer ( kind = 4 ) nfev
1548 integer ( kind = 4 ) nprint
1549 real ( kind = 8 ) qtf(n)
1550 real ( kind = 8 ) r((n*(n+1))/2)
1551 real ( kind = 8 ), intent(in) :: tol
1552 real ( kind = 8 ), intent(inout) :: x(n)
1553 real ( kind = 8 ) xtol
1554
1555 if ( n <= 0 ) then
1556 info = 0
1557 return
1558 end if
1559
1560 if ( tol < 0.0d+00 ) then
1561 info = 0
1562 return
1563 end if
1564
1565 xtol = tol
1566 maxfev = 200 * ( n + 1 )
1567 ml = n - 1
1568 mu = n - 1
1569 epsfcn = 0.0d+00
1570 diag(1:n) = 1.0d+00
1571 mode = 2
1572 factor = 100.0d+00
1573 nprint = 0
1574 info = 0
1575 nfev = 0
1576 fjac(1:n,1:n) = 0.0d+00
1577 ldfjac = n
1578 r(1:(n*(n+1))/2) = 0.0d+00
1579 lr = ( n * ( n + 1 ) ) / 2
1580 qtf(1:n) = 0.0d+00
1581
1582 call hybrd ( fcn, n, x, fvec, xtol, maxfev, ml, mu, epsfcn, diag, mode, &
1583 factor, nprint, info, nfev, fjac, ldfjac, r, lr, qtf )
1584
1585 if ( info == 5 ) then
1586 info = 4
1587 end if
1588
1589 return
1590end
1591subroutine hybrj ( fcn, n, x, fvec, fjac, ldfjac, xtol, maxfev, diag, mode, &
1592 factor, nprint, info, nfev, njev, r, lr, qtf )
1593
1594!*****************************************************************************80
1595!
1596!! HYBRJ seeks a zero of N nonlinear equations in N variables.
1597!
1598! Discussion:
1599!
1600! HYBRJ finds a zero of a system of N nonlinear functions in N variables
1601! by a modification of the Powell hybrid method. The user must provide a
1602! subroutine which calculates the functions and the jacobian.
1603!
1604! Licensing:
1605!
1606! This code is distributed under the GNU LGPL license.
1607!
1608! Modified:
1609!
1610! 06 April 2010
1611!
1612! Author:
1613!
1614! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
1615! FORTRAN90 version by John Burkardt.
1616!
1617! Reference:
1618!
1619! Jorge More, Burton Garbow, Kenneth Hillstrom,
1620! User Guide for MINPACK-1,
1621! Technical Report ANL-80-74,
1622! Argonne National Laboratory, 1980.
1623!
1624! Parameters:
1625!
1626! Input, external FCN, the name of the user-supplied subroutine which
1627! calculates the functions and the jacobian. FCN should have the form:
1628!
1629! subroutine fcn ( n, x, fvec, fjac, ldfjac, iflag )
1630! integer ( kind = 4 ) ldfjac
1631! integer ( kind = 4 ) n
1632! real ( kind = 8 ) fjac(ldfjac,n)
1633! real ( kind = 8 ) fvec(n)
1634! integer ( kind = 4 ) iflag
1635! real ( kind = 8 ) x(n)
1636!
1637! If IFLAG = 0 on input, then FCN is only being called to allow the user
1638! to print out the current iterate.
1639! If IFLAG = 1 on input, FCN should calculate the functions at X and
1640! return this vector in FVEC.
1641! If IFLAG = 2 on input, FCN should calculate the jacobian at X and
1642! return this matrix in FJAC.
1643! To terminate the algorithm, FCN may set IFLAG negative on return.
1644!
1645! Input, integer ( kind = 4 ) N, the number of functions and variables.
1646!
1647! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
1648! estimate of the solution vector. On output X contains the final
1649! estimate of the solution vector.
1650!
1651! Output, real ( kind = 8 ) FVEC(N), the functions evaluated at the output X.
1652!
1653! Output, real ( kind = 8 ) FJAC(LDFJAC,N), an N by N matrix, containing
1654! the orthogonal matrix Q produced by the QR factorization
1655! of the final approximate jacobian.
1656!
1657! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of the
1658! array FJAC. LDFJAC must be at least N.
1659!
1660! Input, real ( kind = 8 ) XTOL. Termination occurs when the relative error
1661! between two consecutive iterates is at most XTOL. XTOL should be
1662! nonnegative.
1663!
1664! Input, integer ( kind = 4 ) MAXFEV. Termination occurs when the number of
1665! calls to FCN is at least MAXFEV by the end of an iteration.
1666!
1667! Input/output, real ( kind = 8 ) DIAG(N). If MODE = 1, then DIAG is set
1668! internally. If MODE = 2, then DIAG must contain positive entries that
1669! serve as multiplicative scale factors for the variables.
1670!
1671! Input, integer ( kind = 4 ) MODE, scaling option.
1672! 1, variables will be scaled internally.
1673! 2, scaling is specified by the input DIAG vector.
1674!
1675! Input, real ( kind = 8 ) FACTOR, determines the initial step bound. This
1676! bound is set to the product of FACTOR and the euclidean norm of DIAG*X if
1677! nonzero, or else to FACTOR itself. In most cases, FACTOR should lie
1678! in the interval (0.1, 100) with 100 the recommended value.
1679!
1680! Input, integer ( kind = 4 ) NPRINT, enables controlled printing of iterates
1681! if it is positive. In this case, FCN is called with IFLAG = 0 at the
1682! beginning of the first iteration and every NPRINT iterations thereafter
1683! and immediately prior to return, with X and FVEC available
1684! for printing. If NPRINT is not positive, no special calls
1685! of FCN with IFLAG = 0 are made.
1686!
1687! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
1688! execution, INFO is set to the (negative) value of IFLAG.
1689! See the description of FCN. Otherwise, INFO is set as follows:
1690! 0, improper input parameters.
1691! 1, relative error between two consecutive iterates is at most XTOL.
1692! 2, number of calls to FCN with IFLAG = 1 has reached MAXFEV.
1693! 3, XTOL is too small. No further improvement in
1694! the approximate solution X is possible.
1695! 4, iteration is not making good progress, as measured by the
1696! improvement from the last five jacobian evaluations.
1697! 5, iteration is not making good progress, as measured by the
1698! improvement from the last ten iterations.
1699!
1700! Output, integer ( kind = 4 ) NFEV, the number of calls to FCN
1701! with IFLAG = 1.
1702!
1703! Output, integer ( kind = 4 ) NJEV, the number of calls to FCN
1704! with IFLAG = 2.
1705!
1706! Output, real ( kind = 8 ) R(LR), the upper triangular matrix produced
1707! by the QR factorization of the final approximate jacobian, stored rowwise.
1708!
1709! Input, integer ( kind = 4 ) LR, the size of the R array, which must
1710! be no less than (N*(N+1))/2.
1711!
1712! Output, real ( kind = 8 ) QTF(N), contains the vector Q'*FVEC.
1713!
1714 implicit none
1715
1716 integer ( kind = 4 ), intent(in) :: ldfjac
1717 integer ( kind = 4 ), intent(in) :: lr
1718 integer ( kind = 4 ), intent(in) :: n
1719
1720 real ( kind = 8 ) actred
1721 real ( kind = 8 ) delta
1722 real ( kind = 8 ), intent(inout) :: diag(n)
1723 real ( kind = 8 ) enorm
1724 real ( kind = 8 ) epsmch
1725 real ( kind = 8 ), intent(in) :: factor
1726 external fcn
1727 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
1728 real ( kind = 8 ) fnorm
1729 real ( kind = 8 ) fnorm1
1730 real ( kind = 8 ), intent(out) :: fvec(n)
1731 integer ( kind = 4 ) i
1732 integer ( kind = 4 ) iflag
1733 integer ( kind = 4 ), intent(out) :: info
1734 integer ( kind = 4 ) iter
1735 integer ( kind = 4 ) iwa(1)
1736 integer ( kind = 4 ) j
1737 logical jeval
1738 integer ( kind = 4 ) l
1739 integer ( kind = 4 ), intent(in) :: maxfev
1740 integer ( kind = 4 ), intent(in) :: mode
1741 integer ( kind = 4 ) ncfail
1742 integer ( kind = 4 ) nslow1
1743 integer ( kind = 4 ) nslow2
1744 integer ( kind = 4 ) ncsuc
1745 integer ( kind = 4 ), intent(out) :: nfev
1746 integer ( kind = 4 ), intent(out) :: njev
1747 integer ( kind = 4 ), intent(in) :: nprint
1748 logical pivot
1749 real ( kind = 8 ) pnorm
1750 real ( kind = 8 ) prered
1751 real ( kind = 8 ), intent(out) :: qtf(n)
1752 real ( kind = 8 ), intent(out) :: r(lr)
1753 real ( kind = 8 ) ratio
1754 logical sing
1755 real ( kind = 8 ) sum2
1756 real ( kind = 8 ) temp
1757 real ( kind = 8 ) wa1(n)
1758 real ( kind = 8 ) wa2(n)
1759 real ( kind = 8 ) wa3(n)
1760 real ( kind = 8 ) wa4(n)
1761 real ( kind = 8 ), intent(inout) :: x(n)
1762 real ( kind = 8 ) xnorm
1763 real ( kind = 8 ), intent(in) :: xtol
1764
1765 epsmch = epsilon( epsmch )
1766
1767 info = 0
1768 iflag = 0
1769 nfev = 0
1770 njev = 0
1771!
1772! Check the input parameters for errors.
1773!
1774 if ( n <= 0 ) then
1775 if ( iflag < 0 ) then
1776 info = iflag
1777 end if
1778 iflag = 0
1779 if ( 0 < nprint ) then
1780 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1781 end if
1782 return
1783 end if
1784
1785 if ( ldfjac < n .or. &
1786 xtol < 0.0d+00 .or. &
1787 maxfev <= 0 .or. &
1788 factor <= 0.0d+00 .or. &
1789 lr < ( n * ( n + 1 ) ) / 2 ) then
1790 if ( iflag < 0 ) then
1791 info = iflag
1792 end if
1793 iflag = 0
1794 if ( 0 < nprint ) then
1795 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1796 end if
1797 return
1798 end if
1799
1800 if ( mode == 2 ) then
1801 do j = 1, n
1802 if ( diag(j) <= 0.0d+00 ) then
1803 if ( iflag < 0 ) then
1804 info = iflag
1805 end if
1806 iflag = 0
1807 if ( 0 < nprint ) then
1808 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1809 end if
1810 return
1811 end if
1812 end do
1813 end if
1814!
1815! Evaluate the function at the starting point and calculate its norm.
1816!
1817 iflag = 1
1818 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1819 nfev = 1
1820
1821 if ( iflag < 0 ) then
1822 if ( iflag < 0 ) then
1823 info = iflag
1824 end if
1825 iflag = 0
1826 if ( 0 < nprint ) then
1827 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1828 end if
1829 return
1830 end if
1831
1832 fnorm = enorm( n, fvec )
1833!
1834! Initialize iteration counter and monitors.
1835!
1836 iter = 1
1837 ncsuc = 0
1838 ncfail = 0
1839 nslow1 = 0
1840 nslow2 = 0
1841!
1842! Beginning of the outer loop.
1843!
1844 do
1845
1846 jeval = .true.
1847!
1848! Calculate the jacobian matrix.
1849!
1850 iflag = 2
1851 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1852 njev = njev + 1
1853
1854 if ( iflag < 0 ) then
1855 info = iflag
1856 iflag = 0
1857 if ( 0 < nprint ) then
1858 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1859 end if
1860 return
1861 end if
1862!
1863! Compute the QR factorization of the jacobian.
1864!
1865 pivot = .false.
1866 call qrfac ( n, n, fjac, ldfjac, pivot, iwa, 1, wa1, wa2 )
1867!
1868! On the first iteration, if MODE is 1, scale according
1869! to the norms of the columns of the initial jacobian.
1870!
1871 if ( iter == 1 ) then
1872
1873 if ( mode /= 2 ) then
1874 diag(1:n) = wa2(1:n)
1875 do j = 1, n
1876 if ( wa2(j) == 0.0d+00 ) then
1877 diag(j) = 1.0d+00
1878 end if
1879 end do
1880 end if
1881!
1882! On the first iteration, calculate the norm of the scaled X
1883! and initialize the step bound DELTA.
1884!
1885 wa3(1:n) = diag(1:n) * x(1:n)
1886 xnorm = enorm( n, wa3 )
1887 delta = factor * xnorm
1888 if ( delta == 0.0d+00 ) then
1889 delta = factor
1890 end if
1891
1892 end if
1893!
1894! Form Q'*FVEC and store in QTF.
1895!
1896 qtf(1:n) = fvec(1:n)
1897
1898 do j = 1, n
1899 if ( fjac(j,j) /= 0.0d+00 ) then
1900 sum2 = 0.0d+00
1901 do i = j, n
1902 sum2 = sum2 + fjac(i,j) * qtf(i)
1903 end do
1904 temp = - sum2 / fjac(j,j)
1905 do i = j, n
1906 qtf(i) = qtf(i) + fjac(i,j) * temp
1907 end do
1908 end if
1909 end do
1910!
1911! Copy the triangular factor of the QR factorization into R.
1912!
1913 sing = .false.
1914
1915 do j = 1, n
1916 l = j
1917 do i = 1, j - 1
1918 r(l) = fjac(i,j)
1919 l = l + n - i
1920 end do
1921 r(l) = wa1(j)
1922 if ( wa1(j) == 0.0d+00 ) then
1923 sing = .true.
1924 end if
1925 end do
1926!
1927! Accumulate the orthogonal factor in FJAC.
1928!
1929 call qform ( n, n, fjac, ldfjac )
1930!
1931! Rescale if necessary.
1932!
1933 if ( mode /= 2 ) then
1934 do j = 1, n
1935 diag(j) = max( diag(j), wa2(j) )
1936 end do
1937 end if
1938!
1939! Beginning of the inner loop.
1940!
1941 do
1942!
1943! If requested, call FCN to enable printing of iterates.
1944!
1945 if ( 0 < nprint ) then
1946
1947 iflag = 0
1948 if ( mod( iter - 1, nprint ) == 0 ) then
1949 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1950 end if
1951
1952 if ( iflag < 0 ) then
1953 info = iflag
1954 iflag = 0
1955 if ( 0 < nprint ) then
1956 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1957 end if
1958 return
1959 end if
1960
1961 end if
1962!
1963! Determine the direction P.
1964!
1965 call dogleg ( n, r, lr, diag, qtf, delta, wa1 )
1966!
1967! Store the direction P and X + P.
1968! Calculate the norm of P.
1969!
1970 wa1(1:n) = - wa1(1:n)
1971 wa2(1:n) = x(1:n) + wa1(1:n)
1972 wa3(1:n) = diag(1:n) * wa1(1:n)
1973 pnorm = enorm( n, wa3 )
1974!
1975! On the first iteration, adjust the initial step bound.
1976!
1977 if ( iter == 1 ) then
1978 delta = min( delta, pnorm )
1979 end if
1980!
1981! Evaluate the function at X + P and calculate its norm.
1982!
1983 iflag = 1
1984 call fcn ( n, wa2, wa4, fjac, ldfjac, iflag )
1985 nfev = nfev + 1
1986
1987 if ( iflag < 0 ) then
1988 info = iflag
1989 iflag = 0
1990 if ( 0 < nprint ) then
1991 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
1992 end if
1993 return
1994 end if
1995
1996 fnorm1 = enorm( n, wa4 )
1997!
1998! Compute the scaled actual reduction.
1999!
2000 actred = -1.0d+00
2001 if ( fnorm1 < fnorm ) then
2002 actred = 1.0d+00 - ( fnorm1 / fnorm ) ** 2
2003 end if
2004!
2005! Compute the scaled predicted reduction.
2006!
2007 l = 1
2008 do i = 1, n
2009 sum2 = 0.0d+00
2010 do j = i, n
2011 sum2 = sum2 + r(l) * wa1(j)
2012 l = l + 1
2013 end do
2014 wa3(i) = qtf(i) + sum2
2015 end do
2016
2017 temp = enorm( n, wa3 )
2018 prered = 0.0d+00
2019 if ( temp < fnorm ) then
2020 prered = 1.0d+00 - ( temp / fnorm ) ** 2
2021 end if
2022!
2023! Compute the ratio of the actual to the predicted reduction.
2024!
2025 if ( 0.0d+00 < prered ) then
2026 ratio = actred / prered
2027 else
2028 ratio = 0.0d+00
2029 end if
2030!
2031! Update the step bound.
2032!
2033 if ( ratio < 0.1d+00 ) then
2034
2035 ncsuc = 0
2036 ncfail = ncfail + 1
2037 delta = 0.5d+00 * delta
2038
2039 else
2040
2041 ncfail = 0
2042 ncsuc = ncsuc + 1
2043
2044 if ( 0.5d+00 <= ratio .or. 1 < ncsuc ) then
2045 delta = max( delta, pnorm / 0.5d+00 )
2046 end if
2047
2048 if ( abs( ratio - 1.0d+00 ) <= 0.1d+00 ) then
2049 delta = pnorm / 0.5d+00
2050 end if
2051
2052 end if
2053!
2054! Test for successful iteration.
2055!
2056
2057!
2058! Successful iteration.
2059! Update X, FVEC, and their norms.
2060!
2061 if ( 0.0001d+00 <= ratio ) then
2062 x(1:n) = wa2(1:n)
2063 wa2(1:n) = diag(1:n) * x(1:n)
2064 fvec(1:n) = wa4(1:n)
2065 xnorm = enorm( n, wa2 )
2066 fnorm = fnorm1
2067 iter = iter + 1
2068 end if
2069!
2070! Determine the progress of the iteration.
2071!
2072 nslow1 = nslow1 + 1
2073 if ( 0.001d+00 <= actred ) then
2074 nslow1 = 0
2075 end if
2076
2077 if ( jeval ) then
2078 nslow2 = nslow2 + 1
2079 end if
2080
2081 if ( 0.1d+00 <= actred ) then
2082 nslow2 = 0
2083 end if
2084!
2085! Test for convergence.
2086!
2087 if ( delta <= xtol * xnorm .or. fnorm == 0.0d+00 ) then
2088 info = 1
2089 end if
2090
2091 if ( info /= 0 ) then
2092 iflag = 0
2093 if ( 0 < nprint ) then
2094 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
2095 end if
2096 return
2097 end if
2098!
2099! Tests for termination and stringent tolerances.
2100!
2101 if ( maxfev <= nfev ) then
2102 info = 2
2103 end if
2104
2105 if ( 0.1d+00 * max( 0.1d+00 * delta, pnorm ) <= epsmch * xnorm ) then
2106 info = 3
2107 end if
2108
2109 if ( nslow2 == 5 ) then
2110 info = 4
2111 end if
2112
2113 if ( nslow1 == 10 ) then
2114 info = 5
2115 end if
2116
2117 if ( info /= 0 ) then
2118 iflag = 0
2119 if ( 0 < nprint ) then
2120 call fcn ( n, x, fvec, fjac, ldfjac, iflag )
2121 end if
2122 return
2123 end if
2124!
2125! Criterion for recalculating jacobian.
2126!
2127 if ( ncfail == 2 ) then
2128 exit
2129 end if
2130!
2131! Calculate the rank one modification to the jacobian
2132! and update QTF if necessary.
2133!
2134 do j = 1, n
2135 sum2 = dot_product( wa4(1:n), fjac(1:n,j) )
2136 wa2(j) = ( sum2 - wa3(j) ) / pnorm
2137 wa1(j) = diag(j) * ( ( diag(j) * wa1(j) ) / pnorm )
2138 if ( 0.0001d+00 <= ratio ) then
2139 qtf(j) = sum2
2140 end if
2141 end do
2142!
2143! Compute the QR factorization of the updated jacobian.
2144!
2145 call r1updt ( n, n, r, lr, wa1, wa2, wa3, sing )
2146 call r1mpyq ( n, n, fjac, ldfjac, wa2, wa3 )
2147 call r1mpyq ( 1, n, qtf, 1, wa2, wa3 )
2148!
2149! End of the inner loop.
2150!
2151 jeval = .false.
2152
2153 end do
2154!
2155! End of the outer loop.
2156!
2157 end do
2158
2159end
2160subroutine hybrj1 ( fcn, n, x, fvec, fjac, ldfjac, tol, info )
2161
2162!*****************************************************************************80
2163!
2164!! HYBRJ1 seeks a zero of N equations in N variables by Powell's method.
2165!
2166! Discussion:
2167!
2168! HYBRJ1 finds a zero of a system of N nonlinear functions in N variables
2169! by a modification of the Powell hybrid method. This is done by using the
2170! more general nonlinear equation solver HYBRJ. The user
2171! must provide a subroutine which calculates the functions
2172! and the jacobian.
2173!
2174! Licensing:
2175!
2176! This code is distributed under the GNU LGPL license.
2177!
2178! Modified:
2179!
2180! 06 April 2010
2181!
2182! Author:
2183!
2184! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
2185! FORTRAN90 version by John Burkardt.
2186!
2187! Reference:
2188!
2189! Jorge More, Burton Garbow, Kenneth Hillstrom,
2190! User Guide for MINPACK-1,
2191! Technical Report ANL-80-74,
2192! Argonne National Laboratory, 1980.
2193!
2194! Parameters:
2195!
2196! Input, external FCN, the name of the user-supplied subroutine which
2197! calculates the functions and the jacobian. FCN should have the form:
2198! subroutine fcn ( n, x, fvec, fjac, ldfjac, iflag )
2199! integer ( kind = 4 ) ldfjac
2200! integer ( kind = 4 ) n
2201! real ( kind = 8 ) fjac(ldfjac,n)
2202! real ( kind = 8 ) fvec(n)
2203! integer ( kind = 4 ) iflag
2204! real ( kind = 8 ) x(n)
2205!
2206! If IFLAG = 0 on input, then FCN is only being called to allow the user
2207! to print out the current iterate.
2208! If IFLAG = 1 on input, FCN should calculate the functions at X and
2209! return this vector in FVEC.
2210! If IFLAG = 2 on input, FCN should calculate the jacobian at X and
2211! return this matrix in FJAC.
2212! To terminate the algorithm, FCN may set IFLAG negative on return.
2213!
2214! Input, integer ( kind = 4 ) N, the number of functions and variables.
2215!
2216! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
2217! estimate of the solution vector. On output X contains the final
2218! estimate of the solution vector.
2219!
2220! Output, real ( kind = 8 ) FVEC(N), the functions evaluated at the output X.
2221!
2222! Output, real ( kind = 8 ) FJAC(LDFJAC,N), an N by N array which contains
2223! the orthogonal matrix Q produced by the QR factorization of the final
2224! approximate jacobian.
2225!
2226! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of FJAC.
2227! LDFJAC must be at least N.
2228!
2229! Input, real ( kind = 8 ) TOL. Termination occurs when the algorithm
2230! estimates that the relative error between X and the solution is at most
2231! TOL. TOL should be nonnegative.
2232!
2233! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
2234! execution, INFO is set to the (negative) value of IFLAG. See description
2235! of FCN. Otherwise, INFO is set as follows:
2236! 0, improper input parameters.
2237! 1, algorithm estimates that the relative error between X and the
2238! solution is at most TOL.
2239! 2, number of calls to FCN with IFLAG = 1 has reached 100*(N+1).
2240! 3, TOL is too small. No further improvement in the approximate
2241! solution X is possible.
2242! 4, iteration is not making good progress.
2243!
2244 implicit none
2245
2246 integer ( kind = 4 ), intent(in) :: ldfjac
2247 integer ( kind = 4 ), intent(in) :: n
2248
2249 real ( kind = 8 ) diag(n)
2250 real ( kind = 8 ) factor
2251 external fcn
2252 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
2253 real ( kind = 8 ), intent(out) :: fvec(n)
2254 integer ( kind = 4 ), intent(out) :: info
2255 integer ( kind = 4 ) lr
2256 integer ( kind = 4 ) maxfev
2257 integer ( kind = 4 ) mode
2258 integer ( kind = 4 ) nfev
2259 integer ( kind = 4 ) njev
2260 integer ( kind = 4 ) nprint
2261 real ( kind = 8 ) qtf(n)
2262 real ( kind = 8 ) r((n*(n+1))/2)
2263 real ( kind = 8 ), intent(in) :: tol
2264 real ( kind = 8 ), intent(inout) :: x(n)
2265 real ( kind = 8 ) xtol
2266
2267 info = 0
2268
2269 if ( n <= 0 ) then
2270 return
2271 else if ( ldfjac < n ) then
2272 return
2273 else if ( tol < 0.0d+00 ) then
2274 return
2275 end if
2276
2277 maxfev = 100 * ( n + 1 )
2278 xtol = tol
2279 mode = 2
2280 diag(1:n) = 1.0d+00
2281 factor = 100.0d+00
2282 nprint = 0
2283 lr = ( n * ( n + 1 ) ) / 2
2284
2285 call hybrj ( fcn, n, x, fvec, fjac, ldfjac, xtol, maxfev, diag, mode, &
2286 factor, nprint, info, nfev, njev, r, lr, qtf )
2287
2288 if ( info == 5 ) then
2289 info = 4
2290 end if
2291
2292 return
2293end
2294subroutine lmder ( fcn, m, n, x, fvec, fjac, ldfjac, ftol, xtol, gtol, maxfev, &
2295 diag, mode, factor, nprint, info, nfev, njev, ipvt, qtf )
2296
2297!*****************************************************************************80
2298!
2299!! LMDER minimizes M functions in N variables by the Levenberg-Marquardt method.
2300!
2301! Discussion:
2302!
2303! LMDER minimizes the sum of the squares of M nonlinear functions in
2304! N variables by a modification of the Levenberg-Marquardt algorithm.
2305! The user must provide a subroutine which calculates the functions
2306! and the jacobian.
2307!
2308! Licensing:
2309!
2310! This code is distributed under the GNU LGPL license.
2311!
2312! Modified:
2313!
2314! 06 April 2010
2315!
2316! Author:
2317!
2318! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
2319! FORTRAN90 version by John Burkardt.
2320!
2321! Reference:
2322!
2323! Jorge More, Burton Garbow, Kenneth Hillstrom,
2324! User Guide for MINPACK-1,
2325! Technical Report ANL-80-74,
2326! Argonne National Laboratory, 1980.
2327!
2328! Parameters:
2329!
2330! Input, external FCN, the name of the user-supplied subroutine which
2331! calculates the functions and the jacobian. FCN should have the form:
2332! subroutine fcn ( m, n, x, fvec, fjac, ldfjac, iflag )
2333! integer ( kind = 4 ) ldfjac
2334! integer ( kind = 4 ) n
2335! real ( kind = 8 ) fjac(ldfjac,n)
2336! real ( kind = 8 ) fvec(m)
2337! integer ( kind = 4 ) iflag
2338! real ( kind = 8 ) x(n)
2339!
2340! If IFLAG = 0 on input, then FCN is only being called to allow the user
2341! to print out the current iterate.
2342! If IFLAG = 1 on input, FCN should calculate the functions at X and
2343! return this vector in FVEC.
2344! If IFLAG = 2 on input, FCN should calculate the jacobian at X and
2345! return this matrix in FJAC.
2346! To terminate the algorithm, FCN may set IFLAG negative on return.
2347!
2348! Input, integer ( kind = 4 ) M, is the number of functions.
2349!
2350! Input, integer ( kind = 4 ) N, is the number of variables.
2351! N must not exceed M.
2352!
2353! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
2354! estimate of the solution vector. On output X contains the final
2355! estimate of the solution vector.
2356!
2357! Output, real ( kind = 8 ) FVEC(M), the functions evaluated at the output X.
2358!
2359! Output, real ( kind = 8 ) FJAC(LDFJAC,N), an M by N array. The upper
2360! N by N submatrix of FJAC contains an upper triangular matrix R with
2361! diagonal elements of nonincreasing magnitude such that
2362! P' * ( JAC' * JAC ) * P = R' * R,
2363! where P is a permutation matrix and JAC is the final calculated jacobian.
2364! Column J of P is column IPVT(J) of the identity matrix. The lower
2365! trapezoidal part of FJAC contains information generated during
2366! the computation of R.
2367!
2368! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of FJAC.
2369! LDFJAC must be at least M.
2370!
2371! Input, real ( kind = 8 ) FTOL. Termination occurs when both the actual
2372! and predicted relative reductions in the sum of squares are at most FTOL.
2373! Therefore, FTOL measures the relative error desired in the sum of
2374! squares. FTOL should be nonnegative.
2375!
2376! Input, real ( kind = 8 ) XTOL. Termination occurs when the relative error
2377! between two consecutive iterates is at most XTOL. XTOL should be
2378! nonnegative.
2379!
2380! Input, real ( kind = 8 ) GTOL. Termination occurs when the cosine of the
2381! angle between FVEC and any column of the jacobian is at most GTOL in
2382! absolute value. Therefore, GTOL measures the orthogonality desired
2383! between the function vector and the columns of the jacobian. GTOL should
2384! be nonnegative.
2385!
2386! Input, integer ( kind = 4 ) MAXFEV. Termination occurs when the number of
2387! calls to FCN with IFLAG = 1 is at least MAXFEV by the end of an iteration.
2388!
2389! Input/output, real ( kind = 8 ) DIAG(N). If MODE = 1, then DIAG is set
2390! internally. If MODE = 2, then DIAG must contain positive entries that
2391! serve as multiplicative scale factors for the variables.
2392!
2393! Input, integer ( kind = 4 ) MODE, scaling option.
2394! 1, variables will be scaled internally.
2395! 2, scaling is specified by the input DIAG vector.
2396!
2397! Input, real ( kind = 8 ) FACTOR, determines the initial step bound. This
2398! bound is set to the product of FACTOR and the euclidean norm of DIAG*X if
2399! nonzero, or else to FACTOR itself. In most cases, FACTOR should lie
2400! in the interval (0.1, 100) with 100 the recommended value.
2401!
2402! Input, integer ( kind = 4 ) NPRINT, enables controlled printing of iterates
2403! if it is positive. In this case, FCN is called with IFLAG = 0 at the
2404! beginning of the first iteration and every NPRINT iterations thereafter
2405! and immediately prior to return, with X and FVEC available
2406! for printing. If NPRINT is not positive, no special calls
2407! of FCN with IFLAG = 0 are made.
2408!
2409! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
2410! execution, INFO is set to the (negative) value of IFLAG. See description
2411! of FCN. Otherwise, INFO is set as follows:
2412! 0, improper input parameters.
2413! 1, both actual and predicted relative reductions in the sum of
2414! squares are at most FTOL.
2415! 2, relative error between two consecutive iterates is at most XTOL.
2416! 3, conditions for INFO = 1 and INFO = 2 both hold.
2417! 4, the cosine of the angle between FVEC and any column of the jacobian
2418! is at most GTOL in absolute value.
2419! 5, number of calls to FCN with IFLAG = 1 has reached MAXFEV.
2420! 6, FTOL is too small. No further reduction in the sum of squares
2421! is possible.
2422! 7, XTOL is too small. No further improvement in the approximate
2423! solution X is possible.
2424! 8, GTOL is too small. FVEC is orthogonal to the columns of the
2425! jacobian to machine precision.
2426!
2427! Output, integer ( kind = 4 ) NFEV, the number of calls to FCN with
2428! IFLAG = 1.
2429!
2430! Output, integer ( kind = 4 ) NJEV, the number of calls to FCN with
2431! IFLAG = 2.
2432!
2433! Output, integer ( kind = 4 ) IPVT(N), defines a permutation matrix P
2434! such that JAC*P = Q*R, where JAC is the final calculated jacobian, Q is
2435! orthogonal (not stored), and R is upper triangular with diagonal
2436! elements of nonincreasing magnitude. Column J of P is column
2437! IPVT(J) of the identity matrix.
2438!
2439! Output, real ( kind = 8 ) QTF(N), contains the first N elements of Q'*FVEC.
2440!
2441 implicit none
2442
2443 integer ( kind = 4 ), intent(in) :: ldfjac
2444 integer ( kind = 4 ), intent(in) :: m
2445 integer ( kind = 4 ), intent(in) :: n
2446
2447 real ( kind = 8 ) actred
2448 real ( kind = 8 ) delta
2449 real ( kind = 8 ), intent(inout) :: diag(n)
2450 real ( kind = 8 ) dirder
2451 real ( kind = 8 ) enorm
2452 real ( kind = 8 ) epsmch
2453 real ( kind = 8 ), intent(in) :: factor
2454 external fcn
2455 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
2456 real ( kind = 8 ) fnorm
2457 real ( kind = 8 ) fnorm1
2458 real ( kind = 8 ), intent(in) :: ftol
2459 real ( kind = 8 ), intent(out) :: fvec(m)
2460 real ( kind = 8 ) gnorm
2461 real ( kind = 8 ), intent(in) :: gtol
2462 integer ( kind = 4 ) iflag
2463 integer ( kind = 4 ), intent(out) :: info
2464 integer ( kind = 4 ), intent(out) :: ipvt(n)
2465 integer ( kind = 4 ) iter
2466 integer ( kind = 4 ) j
2467 integer ( kind = 4 ) l
2468 integer ( kind = 4 ), intent(in) :: maxfev
2469 integer ( kind = 4 ), intent(in) :: mode
2470 integer ( kind = 4 ), intent(out) :: nfev
2471 integer ( kind = 4 ), intent(out) :: njev
2472 integer ( kind = 4 ), intent(in) :: nprint
2473 real ( kind = 8 ) par
2474 logical pivot
2475 real ( kind = 8 ) pnorm
2476 real ( kind = 8 ) prered
2477 real ( kind = 8 ), intent(out) :: qtf(n)
2478 real ( kind = 8 ) ratio
2479 real ( kind = 8 ) sum2
2480 real ( kind = 8 ) temp
2481 real ( kind = 8 ) temp1
2482 real ( kind = 8 ) temp2
2483 real ( kind = 8 ) wa1(n)
2484 real ( kind = 8 ) wa2(n)
2485 real ( kind = 8 ) wa3(n)
2486 real ( kind = 8 ) wa4(m)
2487 real ( kind = 8 ) xnorm
2488 real ( kind = 8 ), intent(inout) :: x(n)
2489 real ( kind = 8 ), intent(in) :: xtol
2490
2491 epsmch = epsilon( epsmch )
2492
2493 info = 0
2494 iflag = 0
2495 nfev = 0
2496 njev = 0
2497!
2498! Check the input parameters for errors.
2499!
2500 if ( n <= 0 ) then
2501 go to 300
2502 end if
2503
2504 if ( m < n ) then
2505 go to 300
2506 end if
2507
2508 if ( ldfjac < m &
2509 .or. ftol < 0.0d+00 .or. xtol < 0.0d+00 .or. gtol < 0.0d+00 &
2510 .or. maxfev <= 0 .or. factor <= 0.0d+00 ) then
2511 go to 300
2512 end if
2513
2514 if ( mode == 2 ) then
2515 do j = 1, n
2516 if ( diag(j) <= 0.0d+00 ) then
2517 go to 300
2518 end if
2519 end do
2520 end if
2521!
2522! Evaluate the function at the starting point and calculate its norm.
2523!
2524 iflag = 1
2525 call fcn ( m, n, x, fvec, fjac, ldfjac, iflag )
2526 nfev = 1
2527 if ( iflag < 0 ) then
2528 go to 300
2529 end if
2530
2531 fnorm = enorm( m, fvec )
2532!
2533! Initialize Levenberg-Marquardt parameter and iteration counter.
2534!
2535 par = 0.0d+00
2536 iter = 1
2537!
2538! Beginning of the outer loop.
2539!
254030 continue
2541!
2542! Calculate the jacobian matrix.
2543!
2544 iflag = 2
2545 call fcn ( m, n, x, fvec, fjac, ldfjac, iflag )
2546
2547 njev = njev + 1
2548
2549 if ( iflag < 0 ) then
2550 go to 300
2551 end if
2552!
2553! If requested, call FCN to enable printing of iterates.
2554!
2555 if ( 0 < nprint ) then
2556 iflag = 0
2557 if ( mod( iter - 1, nprint ) == 0 ) then
2558 call fcn ( m, n, x, fvec, fjac, ldfjac, iflag )
2559 end if
2560 if ( iflag < 0 ) then
2561 go to 300
2562 end if
2563 end if
2564!
2565! Compute the QR factorization of the jacobian.
2566!
2567 pivot = .true.
2568 call qrfac ( m, n, fjac, ldfjac, pivot, ipvt, n, wa1, wa2 )
2569!
2570! On the first iteration and if mode is 1, scale according
2571! to the norms of the columns of the initial jacobian.
2572!
2573 if ( iter == 1 ) then
2574
2575 if ( mode /= 2 ) then
2576 diag(1:n) = wa2(1:n)
2577 do j = 1, n
2578 if ( wa2(j) == 0.0d+00 ) then
2579 diag(j) = 1.0d+00
2580 end if
2581 end do
2582 end if
2583!
2584! On the first iteration, calculate the norm of the scaled X
2585! and initialize the step bound DELTA.
2586!
2587 wa3(1:n) = diag(1:n) * x(1:n)
2588
2589 xnorm = enorm( n, wa3 )
2590 delta = factor * xnorm
2591 if ( delta == 0.0d+00 ) then
2592 delta = factor
2593 end if
2594
2595 end if
2596!
2597! Form Q'*FVEC and store the first N components in QTF.
2598!
2599 wa4(1:m) = fvec(1:m)
2600
2601 do j = 1, n
2602
2603 if ( fjac(j,j) /= 0.0d+00 ) then
2604 sum2 = dot_product( wa4(j:m), fjac(j:m,j) )
2605 temp = - sum2 / fjac(j,j)
2606 wa4(j:m) = wa4(j:m) + fjac(j:m,j) * temp
2607 end if
2608
2609 fjac(j,j) = wa1(j)
2610 qtf(j) = wa4(j)
2611
2612 end do
2613!
2614! Compute the norm of the scaled gradient.
2615!
2616 gnorm = 0.0d+00
2617
2618 if ( fnorm /= 0.0d+00 ) then
2619
2620 do j = 1, n
2621 l = ipvt(j)
2622 if ( wa2(l) /= 0.0d+00 ) then
2623 sum2 = dot_product( qtf(1:j), fjac(1:j,j) ) / fnorm
2624 gnorm = max( gnorm, abs( sum2 / wa2(l) ) )
2625 end if
2626 end do
2627
2628 end if
2629!
2630! Test for convergence of the gradient norm.
2631!
2632 if ( gnorm <= gtol ) then
2633 info = 4
2634 go to 300
2635 end if
2636!
2637! Rescale if necessary.
2638!
2639 if ( mode /= 2 ) then
2640 do j = 1, n
2641 diag(j) = max( diag(j), wa2(j) )
2642 end do
2643 end if
2644!
2645! Beginning of the inner loop.
2646!
2647200 continue
2648!
2649! Determine the Levenberg-Marquardt parameter.
2650!
2651 call lmpar ( n, fjac, ldfjac, ipvt, diag, qtf, delta, par, wa1, wa2 )
2652!
2653! Store the direction p and x + p. calculate the norm of p.
2654!
2655 wa1(1:n) = - wa1(1:n)
2656 wa2(1:n) = x(1:n) + wa1(1:n)
2657 wa3(1:n) = diag(1:n) * wa1(1:n)
2658
2659 pnorm = enorm( n, wa3 )
2660!
2661! On the first iteration, adjust the initial step bound.
2662!
2663 if ( iter == 1 ) then
2664 delta = min( delta, pnorm )
2665 end if
2666!
2667! Evaluate the function at x + p and calculate its norm.
2668!
2669 iflag = 1
2670 call fcn ( m, n, wa2, wa4, fjac, ldfjac, iflag )
2671
2672 nfev = nfev + 1
2673
2674 if ( iflag < 0 ) then
2675 go to 300
2676 end if
2677
2678 fnorm1 = enorm( m, wa4 )
2679!
2680! Compute the scaled actual reduction.
2681!
2682 actred = -1.0d+00
2683 if ( 0.1d+00 * fnorm1 < fnorm ) then
2684 actred = 1.0d+00 - ( fnorm1 / fnorm ) ** 2
2685 end if
2686!
2687! Compute the scaled predicted reduction and
2688! the scaled directional derivative.
2689!
2690 do j = 1, n
2691 wa3(j) = 0.0d+00
2692 l = ipvt(j)
2693 temp = wa1(l)
2694 wa3(1:j) = wa3(1:j) + fjac(1:j,j) * temp
2695 end do
2696
2697 temp1 = enorm( n, wa3 ) / fnorm
2698 temp2 = ( sqrt( par ) * pnorm ) / fnorm
2699 prered = temp1 ** 2 + temp2 ** 2 / 0.5d+00
2700 dirder = - ( temp1 ** 2 + temp2 ** 2 )
2701!
2702! Compute the ratio of the actual to the predicted reduction.
2703!
2704 if ( prered /= 0.0d+00 ) then
2705 ratio = actred / prered
2706 else
2707 ratio = 0.0d+00
2708 end if
2709!
2710! Update the step bound.
2711!
2712 if ( ratio <= 0.25d+00 ) then
2713
2714 if ( 0.0d+00 <= actred ) then
2715 temp = 0.5d+00
2716 end if
2717
2718 if ( actred < 0.0d+00 ) then
2719 temp = 0.5d+00 * dirder / ( dirder + 0.5d+00 * actred )
2720 end if
2721
2722 if ( 0.1d+00 * fnorm1 >= fnorm .or. temp < 0.1d+00 ) then
2723 temp = 0.1d+00
2724 end if
2725
2726 delta = temp * min( delta, pnorm / 0.1d+00 )
2727 par = par / temp
2728
2729 else
2730
2731 if ( par == 0.0d+00 .or. ratio >= 0.75d+00 ) then
2732 delta = 2.0d+00 * pnorm
2733 par = 0.5d+00 * par
2734 end if
2735
2736 end if
2737!
2738! Successful iteration.
2739!
2740! Update X, FVEC, and their norms.
2741!
2742 if ( 0.0001d+00 <= ratio ) then
2743 x(1:n) = wa2(1:n)
2744 wa2(1:n) = diag(1:n) * x(1:n)
2745 fvec(1:m) = wa4(1:m)
2746 xnorm = enorm( n, wa2 )
2747 fnorm = fnorm1
2748 iter = iter + 1
2749 end if
2750!
2751! Tests for convergence.
2752!
2753 if ( abs( actred) <= ftol .and. &
2754 prered <= ftol .and. &
2755 0.5d+00 * ratio <= 1.0d+00 ) then
2756 info = 1
2757 end if
2758
2759 if ( delta <= xtol * xnorm ) then
2760 info = 2
2761 end if
2762
2763 if ( abs( actred) <= ftol .and. prered <= ftol &
2764 .and. 0.5d+00 * ratio <= 1.0d+00 .and. info == 2 ) then
2765 info = 3
2766 end if
2767
2768 if ( info /= 0 ) then
2769 go to 300
2770 end if
2771!
2772! Tests for termination and stringent tolerances.
2773!
2774 if ( nfev >= maxfev ) then
2775 info = 5
2776 end if
2777
2778 if ( abs( actred ) <= epsmch .and. prered <= epsmch &
2779 .and. 0.5d+00 * ratio <= 1.0d+00 ) then
2780 info = 6
2781 end if
2782
2783 if ( delta <= epsmch * xnorm ) then
2784 info = 7
2785 end if
2786
2787 if ( gnorm <= epsmch ) then
2788 info = 8
2789 end if
2790
2791 if ( info /= 0 ) then
2792 go to 300
2793 end if
2794!
2795! End of the inner loop. repeat if iteration unsuccessful.
2796!
2797 if ( ratio < 0.0001d+00 ) then
2798 go to 200
2799 end if
2800!
2801! End of the outer loop.
2802!
2803 go to 30
2804
2805 300 continue
2806!
2807! Termination, either normal or user imposed.
2808!
2809 if ( iflag < 0 ) then
2810 info = iflag
2811 end if
2812
2813 iflag = 0
2814
2815 if ( 0 < nprint ) then
2816 call fcn ( m, n, x, fvec, fjac, ldfjac, iflag )
2817 end if
2818
2819 return
2820end
2821subroutine lmder1 ( fcn, m, n, x, fvec, fjac, ldfjac, tol, info )
2822
2823!*****************************************************************************80
2824!
2825!! LMDER1 minimizes M functions in N variables by Levenberg-Marquardt method.
2826!
2827! Discussion:
2828!
2829! LMDER1 minimizes the sum of the squares of M nonlinear functions in
2830! N variables by a modification of the Levenberg-Marquardt algorithm.
2831! This is done by using the more general least-squares solver LMDER.
2832! The user must provide a subroutine which calculates the functions
2833! and the jacobian.
2834!
2835! Licensing:
2836!
2837! This code is distributed under the GNU LGPL license.
2838!
2839! Modified:
2840!
2841! 06 April 2010
2842!
2843! Author:
2844!
2845! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
2846! FORTRAN90 version by John Burkardt.
2847!
2848! Reference:
2849!
2850! Jorge More, Burton Garbow, Kenneth Hillstrom,
2851! User Guide for MINPACK-1,
2852! Technical Report ANL-80-74,
2853! Argonne National Laboratory, 1980.
2854!
2855! Parameters:
2856!
2857! Input, external FCN, the name of the user-supplied subroutine which
2858! calculates the functions and the jacobian. FCN should have the form:
2859! subroutine fcn ( m, n, x, fvec, fjac, ldfjac, iflag )
2860! integer ( kind = 4 ) ldfjac
2861! integer ( kind = 4 ) n
2862! real ( kind = 8 ) fjac(ldfjac,n)
2863! real ( kind = 8 ) fvec(m)
2864! integer ( kind = 4 ) iflag
2865! real ( kind = 8 ) x(n)
2866!
2867! If IFLAG = 0 on input, then FCN is only being called to allow the user
2868! to print out the current iterate.
2869! If IFLAG = 1 on input, FCN should calculate the functions at X and
2870! return this vector in FVEC.
2871! If IFLAG = 2 on input, FCN should calculate the jacobian at X and
2872! return this matrix in FJAC.
2873! To terminate the algorithm, FCN may set IFLAG negative on return.
2874!
2875! Input, integer ( kind = 4 ) M, the number of functions.
2876!
2877! Input, integer ( kind = 4 ) N, is the number of variables.
2878! N must not exceed M.
2879!
2880! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
2881! estimate of the solution vector. On output X contains the final
2882! estimate of the solution vector.
2883!
2884! Output, real ( kind = 8 ) FVEC(M), the functions evaluated at the output X.
2885!
2886! Output, real ( kind = 8 ) FJAC(LDFJAC,N), an M by N array. The upper
2887! N by N submatrix contains an upper triangular matrix R with
2888! diagonal elements of nonincreasing magnitude such that
2889! P' * ( JAC' * JAC ) * P = R' * R,
2890! where P is a permutation matrix and JAC is the final calculated
2891! jacobian. Column J of P is column IPVT(J) of the identity matrix.
2892! The lower trapezoidal part of FJAC contains information generated during
2893! the computation of R.
2894!
2895! Input, integer ( kind = 4 ) LDFJAC, is the leading dimension of FJAC,
2896! which must be no less than M.
2897!
2898! Input, real ( kind = 8 ) TOL. Termination occurs when the algorithm
2899! estimates either that the relative error in the sum of squares is at
2900! most TOL or that the relative error between X and the solution is at
2901! most TOL.
2902!
2903! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
2904! execution, INFO is set to the (negative) value of IFLAG. See description
2905! of FCN. Otherwise, INFO is set as follows:
2906! 0, improper input parameters.
2907! 1, algorithm estimates that the relative error in the sum of squares
2908! is at most TOL.
2909! 2, algorithm estimates that the relative error between X and the
2910! solution is at most TOL.
2911! 3, conditions for INFO = 1 and INFO = 2 both hold.
2912! 4, FVEC is orthogonal to the columns of the jacobian to machine precision.
2913! 5, number of calls to FCN with IFLAG = 1 has reached 100*(N+1).
2914! 6, TOL is too small. No further reduction in the sum of squares is
2915! possible.
2916! 7, TOL is too small. No further improvement in the approximate
2917! solution X is possible.
2918!
2919 implicit none
2920
2921 integer ( kind = 4 ), intent(in) :: ldfjac
2922 integer ( kind = 4 ), intent(in) :: m
2923 integer ( kind = 4 ), intent(in) :: n
2924
2925 real ( kind = 8 ) diag(n)
2926 real ( kind = 8 ) factor
2927 external fcn
2928 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
2929 real ( kind = 8 ) ftol
2930 real ( kind = 8 ), intent(out) :: fvec(m)
2931 real ( kind = 8 ) gtol
2932 integer ( kind = 4 ), intent(out) :: info
2933 integer ( kind = 4 ) ipvt(n)
2934 integer ( kind = 4 ) maxfev
2935 integer ( kind = 4 ) mode
2936 integer ( kind = 4 ) nfev
2937 integer ( kind = 4 ) njev
2938 integer ( kind = 4 ) nprint
2939 real ( kind = 8 ) qtf(n)
2940 real ( kind = 8 ), intent(in) :: tol
2941 real ( kind = 8 ), intent(inout) :: x(n)
2942 real ( kind = 8 ) xtol
2943
2944 info = 0
2945
2946 if ( n <= 0 ) then
2947 return
2948 else if ( m < n ) then
2949 return
2950 else if ( ldfjac < m ) then
2951 return
2952 else if ( tol < 0.0d+00 ) then
2953 return
2954 end if
2955
2956 factor = 100.0d+00
2957 maxfev = 100 * ( n + 1 )
2958 ftol = tol
2959 xtol = tol
2960 gtol = 0.0d+00
2961 mode = 1
2962 nprint = 0
2963
2964 call lmder ( fcn, m, n, x, fvec, fjac, ldfjac, ftol, xtol, gtol, maxfev, &
2965 diag, mode, factor, nprint, info, nfev, njev, ipvt, qtf )
2966
2967 if ( info == 8 ) then
2968 info = 4
2969 end if
2970
2971 return
2972end
2973subroutine lmdif ( fcn, m, n, x, fvec, ftol, xtol, gtol, maxfev, epsfcn, &
2974 diag, mode, factor, nprint, info, nfev, fjac, ldfjac, ipvt, qtf )
2975
2976!*****************************************************************************80
2977!
2978!! LMDIF minimizes M functions in N variables by the Levenberg-Marquardt method.
2979!
2980! Discussion:
2981!
2982! LMDIF minimizes the sum of the squares of M nonlinear functions in
2983! N variables by a modification of the Levenberg-Marquardt algorithm.
2984! The user must provide a subroutine which calculates the functions.
2985! The jacobian is then calculated by a forward-difference approximation.
2986!
2987! Licensing:
2988!
2989! This code is distributed under the GNU LGPL license.
2990!
2991! Modified:
2992!
2993! 06 April 2010
2994!
2995! Author:
2996!
2997! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
2998! FORTRAN90 version by John Burkardt.
2999!
3000! Reference:
3001!
3002! Jorge More, Burton Garbow, Kenneth Hillstrom,
3003! User Guide for MINPACK-1,
3004! Technical Report ANL-80-74,
3005! Argonne National Laboratory, 1980.
3006!
3007! Parameters:
3008!
3009! Input, external FCN, the name of the user-supplied subroutine which
3010! calculates the functions. The routine should have the form:
3011! subroutine fcn ( m, n, x, fvec, iflag )
3012! integer ( kind = 4 ) m
3013! integer ( kind = 4 ) n
3014! real ( kind = 8 ) fvec(m)
3015! integer ( kind = 4 ) iflag
3016! real ( kind = 8 ) x(n)
3017! If IFLAG = 0 on input, then FCN is only being called to allow the user
3018! to print out the current iterate.
3019! If IFLAG = 1 on input, FCN should calculate the functions at X and
3020! return this vector in FVEC.
3021! To terminate the algorithm, FCN may set IFLAG negative on return.
3022!
3023! Input, integer ( kind = 4 ) M, the number of functions.
3024!
3025! Input, integer ( kind = 4 ) N, the number of variables.
3026! N must not exceed M.
3027!
3028! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
3029! estimate of the solution vector. On output X contains the final
3030! estimate of the solution vector.
3031!
3032! Output, real ( kind = 8 ) FVEC(M), the functions evaluated at the output X.
3033!
3034! Input, real ( kind = 8 ) FTOL. Termination occurs when both the actual
3035! and predicted relative reductions in the sum of squares are at most FTOL.
3036! Therefore, FTOL measures the relative error desired in the sum of
3037! squares. FTOL should be nonnegative.
3038!
3039! Input, real ( kind = 8 ) XTOL. Termination occurs when the relative error
3040! between two consecutive iterates is at most XTOL. Therefore, XTOL
3041! measures the relative error desired in the approximate solution. XTOL
3042! should be nonnegative.
3043!
3044! Input, real ( kind = 8 ) GTOL. termination occurs when the cosine of the
3045! angle between FVEC and any column of the jacobian is at most GTOL in
3046! absolute value. Therefore, GTOL measures the orthogonality desired
3047! between the function vector and the columns of the jacobian. GTOL should
3048! be nonnegative.
3049!
3050! Input, integer ( kind = 4 ) MAXFEV. Termination occurs when the number of
3051! calls to FCN is at least MAXFEV by the end of an iteration.
3052!
3053! Input, real ( kind = 8 ) EPSFCN, is used in determining a suitable step
3054! length for the forward-difference approximation. This approximation
3055! assumes that the relative errors in the functions are of the order of
3056! EPSFCN. If EPSFCN is less than the machine precision, it is assumed that
3057! the relative errors in the functions are of the order of the machine
3058! precision.
3059!
3060! Input/output, real ( kind = 8 ) DIAG(N). If MODE = 1, then DIAG is set
3061! internally. If MODE = 2, then DIAG must contain positive entries that
3062! serve as multiplicative scale factors for the variables.
3063!
3064! Input, integer ( kind = 4 ) MODE, scaling option.
3065! 1, variables will be scaled internally.
3066! 2, scaling is specified by the input DIAG vector.
3067!
3068! Input, real ( kind = 8 ) FACTOR, determines the initial step bound.
3069! This bound is set to the product of FACTOR and the euclidean norm of
3070! DIAG*X if nonzero, or else to FACTOR itself. In most cases, FACTOR should
3071! lie in the interval (0.1, 100) with 100 the recommended value.
3072!
3073! Input, integer ( kind = 4 ) NPRINT, enables controlled printing of iterates
3074! if it is positive. In this case, FCN is called with IFLAG = 0 at the
3075! beginning of the first iteration and every NPRINT iterations thereafter
3076! and immediately prior to return, with X and FVEC available
3077! for printing. If NPRINT is not positive, no special calls
3078! of FCN with IFLAG = 0 are made.
3079!
3080! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
3081! execution, INFO is set to the (negative) value of IFLAG. See description
3082! of FCN. Otherwise, INFO is set as follows:
3083! 0, improper input parameters.
3084! 1, both actual and predicted relative reductions in the sum of squares
3085! are at most FTOL.
3086! 2, relative error between two consecutive iterates is at most XTOL.
3087! 3, conditions for INFO = 1 and INFO = 2 both hold.
3088! 4, the cosine of the angle between FVEC and any column of the jacobian
3089! is at most GTOL in absolute value.
3090! 5, number of calls to FCN has reached or exceeded MAXFEV.
3091! 6, FTOL is too small. No further reduction in the sum of squares
3092! is possible.
3093! 7, XTOL is too small. No further improvement in the approximate
3094! solution X is possible.
3095! 8, GTOL is too small. FVEC is orthogonal to the columns of the
3096! jacobian to machine precision.
3097!
3098! Output, integer ( kind = 4 ) NFEV, the number of calls to FCN.
3099!
3100! Output, real ( kind = 8 ) FJAC(LDFJAC,N), an M by N array. The upper
3101! N by N submatrix of FJAC contains an upper triangular matrix R with
3102! diagonal elements of nonincreasing magnitude such that
3103!
3104! P' * ( JAC' * JAC ) * P = R' * R,
3105!
3106! where P is a permutation matrix and JAC is the final calculated jacobian.
3107! Column J of P is column IPVT(J) of the identity matrix. The lower
3108! trapezoidal part of FJAC contains information generated during
3109! the computation of R.
3110!
3111! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of FJAC.
3112! LDFJAC must be at least M.
3113!
3114! Output, integer ( kind = 4 ) IPVT(N), defines a permutation matrix P such
3115! that JAC * P = Q * R, where JAC is the final calculated jacobian, Q is
3116! orthogonal (not stored), and R is upper triangular with diagonal
3117! elements of nonincreasing magnitude. Column J of P is column IPVT(J)
3118! of the identity matrix.
3119!
3120! Output, real ( kind = 8 ) QTF(N), the first N elements of Q'*FVEC.
3121!
3122 implicit none
3123
3124 integer ( kind = 4 ), intent(in) :: ldfjac
3125 integer ( kind = 4 ), intent(in) :: m
3126 integer ( kind = 4 ), intent(in) :: n
3127
3128 real ( kind = 8 ) actred
3129 real ( kind = 8 ) delta
3130 real ( kind = 8 ), intent(inout) :: diag(n)
3131 real ( kind = 8 ) dirder
3132 real ( kind = 8 ) enorm
3133 real ( kind = 8 ), intent(in) :: epsfcn
3134 real ( kind = 8 ) epsmch
3135 real ( kind = 8 ), intent(in) :: factor
3136 external fcn
3137 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
3138 real ( kind = 8 ) fnorm
3139 real ( kind = 8 ) fnorm1
3140 real ( kind = 8 ), intent(in) :: ftol
3141 real ( kind = 8 ), intent(out) :: fvec(m)
3142 real ( kind = 8 ) gnorm
3143 real ( kind = 8 ), intent(in) :: gtol
3144 integer ( kind = 4 ) i
3145 integer ( kind = 4 ) iflag
3146 integer ( kind = 4 ) iter
3147 integer ( kind = 4 ), intent(out) :: info
3148 integer ( kind = 4 ), intent(out) :: ipvt(n)
3149 integer ( kind = 4 ) j
3150 integer ( kind = 4 ) l
3151 integer ( kind = 4 ), intent(in) :: maxfev
3152 integer ( kind = 4 ), intent(in) :: mode
3153 integer ( kind = 4 ), intent(out) :: nfev
3154 integer ( kind = 4 ), intent(in) :: nprint
3155 real ( kind = 8 ) par
3156 logical pivot
3157 real ( kind = 8 ) pnorm
3158 real ( kind = 8 ) prered
3159 real ( kind = 8 ), intent(out) :: qtf(n)
3160 real ( kind = 8 ) ratio
3161 real ( kind = 8 ) sum2
3162 real ( kind = 8 ) temp
3163 real ( kind = 8 ) temp1
3164 real ( kind = 8 ) temp2
3165 real ( kind = 8 ) wa1(n)
3166 real ( kind = 8 ) wa2(n)
3167 real ( kind = 8 ) wa3(n)
3168 real ( kind = 8 ) wa4(m)
3169 real ( kind = 8 ), intent(inout) :: x(n)
3170 real ( kind = 8 ) xnorm
3171 real ( kind = 8 ), intent(in) :: xtol
3172
3173 epsmch = epsilon( epsmch )
3174
3175 info = 0
3176 iflag = 0
3177 nfev = 0
3178
3179 if ( n <= 0 ) then
3180 go to 300
3181 else if ( m < n ) then
3182 go to 300
3183 else if ( ldfjac < m ) then
3184 go to 300
3185 else if ( ftol < 0.0d+00 ) then
3186 go to 300
3187 else if ( xtol < 0.0d+00 ) then
3188 go to 300
3189 else if ( gtol < 0.0d+00 ) then
3190 go to 300
3191 else if ( maxfev <= 0 ) then
3192 go to 300
3193 else if ( factor <= 0.0d+00 ) then
3194 go to 300
3195 end if
3196
3197 if ( mode == 2 ) then
3198 do j = 1, n
3199 if ( diag(j) <= 0.0d+00 ) then
3200 go to 300
3201 end if
3202 end do
3203 end if
3204!
3205! Evaluate the function at the starting point and calculate its norm.
3206!
3207 iflag = 1
3208 call fcn ( m, n, x, fvec, iflag )
3209 nfev = 1
3210
3211 if ( iflag < 0 ) then
3212 go to 300
3213 end if
3214
3215 fnorm = enorm( m, fvec )
3216!
3217! Initialize Levenberg-Marquardt parameter and iteration counter.
3218!
3219 par = 0.0d+00
3220 iter = 1
3221!
3222! Beginning of the outer loop.
3223!
322430 continue
3225!
3226! Calculate the jacobian matrix.
3227!
3228 iflag = 2
3229 call fdjac2 ( fcn, m, n, x, fvec, fjac, ldfjac, iflag, epsfcn )
3230 nfev = nfev + n
3231
3232 if ( iflag < 0 ) then
3233 go to 300
3234 end if
3235!
3236! If requested, call FCN to enable printing of iterates.
3237!
3238 if ( 0 < nprint ) then
3239 iflag = 0
3240 if ( mod( iter - 1, nprint ) == 0 ) then
3241 call fcn ( m, n, x, fvec, iflag )
3242 end if
3243 if ( iflag < 0 ) then
3244 go to 300
3245 end if
3246 end if
3247!
3248! Compute the QR factorization of the jacobian.
3249!
3250 pivot = .true.
3251 call qrfac ( m, n, fjac, ldfjac, pivot, ipvt, n, wa1, wa2 )
3252!
3253! On the first iteration and if MODE is 1, scale according
3254! to the norms of the columns of the initial jacobian.
3255!
3256 if ( iter == 1 ) then
3257
3258 if ( mode /= 2 ) then
3259 diag(1:n) = wa2(1:n)
3260 do j = 1, n
3261 if ( wa2(j) == 0.0d+00 ) then
3262 diag(j) = 1.0d+00
3263 end if
3264 end do
3265 end if
3266!
3267! On the first iteration, calculate the norm of the scaled X
3268! and initialize the step bound DELTA.
3269!
3270 wa3(1:n) = diag(1:n) * x(1:n)
3271 xnorm = enorm( n, wa3 )
3272 delta = factor * xnorm
3273 if ( delta == 0.0d+00 ) then
3274 delta = factor
3275 end if
3276 end if
3277!
3278! Form Q' * FVEC and store the first N components in QTF.
3279!
3280 wa4(1:m) = fvec(1:m)
3281
3282 do j = 1, n
3283
3284 if ( fjac(j,j) /= 0.0d+00 ) then
3285 sum2 = dot_product( wa4(j:m), fjac(j:m,j) )
3286 temp = - sum2 / fjac(j,j)
3287 wa4(j:m) = wa4(j:m) + fjac(j:m,j) * temp
3288 end if
3289
3290 fjac(j,j) = wa1(j)
3291 qtf(j) = wa4(j)
3292
3293 end do
3294!
3295! Compute the norm of the scaled gradient.
3296!
3297 gnorm = 0.0d+00
3298
3299 if ( fnorm /= 0.0d+00 ) then
3300
3301 do j = 1, n
3302
3303 l = ipvt(j)
3304
3305 if ( wa2(l) /= 0.0d+00 ) then
3306 sum2 = 0.0d+00
3307 do i = 1, j
3308 sum2 = sum2 + fjac(i,j) * ( qtf(i) / fnorm )
3309 end do
3310 gnorm = max( gnorm, abs( sum2 / wa2(l) ) )
3311 end if
3312
3313 end do
3314
3315 end if
3316!
3317! Test for convergence of the gradient norm.
3318!
3319 if ( gnorm <= gtol ) then
3320 info = 4
3321 go to 300
3322 end if
3323!
3324! Rescale if necessary.
3325!
3326 if ( mode /= 2 ) then
3327 do j = 1, n
3328 diag(j) = max( diag(j), wa2(j) )
3329 end do
3330 end if
3331!
3332! Beginning of the inner loop.
3333!
3334200 continue
3335!
3336! Determine the Levenberg-Marquardt parameter.
3337!
3338 call lmpar ( n, fjac, ldfjac, ipvt, diag, qtf, delta, par, wa1, wa2 )
3339!
3340! Store the direction P and X + P.
3341! Calculate the norm of P.
3342!
3343 wa1(1:n) = -wa1(1:n)
3344 wa2(1:n) = x(1:n) + wa1(1:n)
3345 wa3(1:n) = diag(1:n) * wa1(1:n)
3346
3347 pnorm = enorm( n, wa3 )
3348!
3349! On the first iteration, adjust the initial step bound.
3350!
3351 if ( iter == 1 ) then
3352 delta = min( delta, pnorm )
3353 end if
3354!
3355! Evaluate the function at X + P and calculate its norm.
3356!
3357 iflag = 1
3358 call fcn ( m, n, wa2, wa4, iflag )
3359 nfev = nfev + 1
3360 if ( iflag < 0 ) then
3361 go to 300
3362 end if
3363 fnorm1 = enorm( m, wa4 )
3364!
3365! Compute the scaled actual reduction.
3366!
3367 if ( 0.1d+00 * fnorm1 < fnorm ) then
3368 actred = 1.0d+00 - ( fnorm1 / fnorm ) ** 2
3369 else
3370 actred = -1.0d+00
3371 end if
3372!
3373! Compute the scaled predicted reduction and the scaled directional derivative.
3374!
3375 do j = 1, n
3376 wa3(j) = 0.0d+00
3377 l = ipvt(j)
3378 temp = wa1(l)
3379 wa3(1:j) = wa3(1:j) + fjac(1:j,j) * temp
3380 end do
3381
3382 temp1 = enorm( n, wa3 ) / fnorm
3383 temp2 = ( sqrt( par ) * pnorm ) / fnorm
3384 prered = temp1 ** 2 + temp2 ** 2 / 0.5d+00
3385 dirder = - ( temp1 ** 2 + temp2 ** 2 )
3386!
3387! Compute the ratio of the actual to the predicted reduction.
3388!
3389 ratio = 0.0d+00
3390 if ( prered /= 0.0d+00 ) then
3391 ratio = actred / prered
3392 end if
3393!
3394! Update the step bound.
3395!
3396 if ( ratio <= 0.25d+00 ) then
3397
3398 if ( actred >= 0.0d+00 ) then
3399 temp = 0.5d+00
3400 endif
3401
3402 if ( actred < 0.0d+00 ) then
3403 temp = 0.5d+00 * dirder / ( dirder + 0.5d+00 * actred )
3404 end if
3405
3406 if ( 0.1d+00 * fnorm1 >= fnorm .or. temp < 0.1d+00 ) then
3407 temp = 0.1d+00
3408 end if
3409
3410 delta = temp * min( delta, pnorm / 0.1d+00 )
3411 par = par / temp
3412
3413 else
3414
3415 if ( par == 0.0d+00 .or. ratio >= 0.75d+00 ) then
3416 delta = 2.0d+00 * pnorm
3417 par = 0.5d+00 * par
3418 end if
3419
3420 end if
3421!
3422! Test for successful iteration.
3423!
3424
3425!
3426! Successful iteration. update X, FVEC, and their norms.
3427!
3428 if ( 0.0001d+00 <= ratio ) then
3429 x(1:n) = wa2(1:n)
3430 wa2(1:n) = diag(1:n) * x(1:n)
3431 fvec(1:m) = wa4(1:m)
3432 xnorm = enorm( n, wa2 )
3433 fnorm = fnorm1
3434 iter = iter + 1
3435 end if
3436!
3437! Tests for convergence.
3438!
3439 if ( abs( actred) <= ftol .and. prered <= ftol &
3440 .and. 0.5d+00 * ratio <= 1.0d+00 ) then
3441 info = 1
3442 end if
3443
3444 if ( delta <= xtol * xnorm ) then
3445 info = 2
3446 end if
3447
3448 if ( abs( actred) <= ftol .and. prered <= ftol &
3449 .and. 0.5d+00 * ratio <= 1.0d+00 .and. info == 2 ) info = 3
3450
3451 if ( info /= 0 ) then
3452 go to 300
3453 end if
3454!
3455! Tests for termination and stringent tolerances.
3456!
3457 if ( maxfev <= nfev ) then
3458 info = 5
3459 end if
3460
3461 if ( abs( actred) <= epsmch .and. prered <= epsmch &
3462 .and. 0.5d+00 * ratio <= 1.0d+00 ) then
3463 info = 6
3464 end if
3465
3466 if ( delta <= epsmch * xnorm ) then
3467 info = 7
3468 end if
3469
3470 if ( gnorm <= epsmch ) then
3471 info = 8
3472 end if
3473
3474 if ( info /= 0 ) then
3475 go to 300
3476 end if
3477!
3478! End of the inner loop. Repeat if iteration unsuccessful.
3479!
3480 if ( ratio < 0.0001d+00 ) then
3481 go to 200
3482 end if
3483!
3484! End of the outer loop.
3485!
3486 go to 30
3487
3488300 continue
3489!
3490! Termination, either normal or user imposed.
3491!
3492 if ( iflag < 0 ) then
3493 info = iflag
3494 end if
3495
3496 iflag = 0
3497
3498 if ( 0 < nprint ) then
3499 call fcn ( m, n, x, fvec, iflag )
3500 end if
3501
3502 return
3503end
3504subroutine lmdif1 ( fcn, m, n, x, fvec, tol, info )
3505
3506!*****************************************************************************80
3507!
3508!! LMDIF1 minimizes M functions in N variables using Levenberg-Marquardt method.
3509!
3510! Discussion:
3511!
3512! LMDIF1 minimizes the sum of the squares of M nonlinear functions in
3513! N variables by a modification of the Levenberg-Marquardt algorithm.
3514! This is done by using the more general least-squares solver LMDIF.
3515! The user must provide a subroutine which calculates the functions.
3516! The jacobian is then calculated by a forward-difference approximation.
3517!
3518! Licensing:
3519!
3520! This code is distributed under the GNU LGPL license.
3521!
3522! Modified:
3523!
3524! 06 April 2010
3525!
3526! Author:
3527!
3528! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
3529! FORTRAN90 version by John Burkardt.
3530!
3531! Reference:
3532!
3533! Jorge More, Burton Garbow, Kenneth Hillstrom,
3534! User Guide for MINPACK-1,
3535! Technical Report ANL-80-74,
3536! Argonne National Laboratory, 1980.
3537!
3538! Parameters:
3539!
3540! Input, external FCN, the name of the user-supplied subroutine which
3541! calculates the functions. The routine should have the form:
3542! subroutine fcn ( m, n, x, fvec, iflag )
3543! integer ( kind = 4 ) n
3544! real ( kind = 8 ) fvec(m)
3545! integer ( kind = 4 ) iflag
3546! real ( kind = 8 ) x(n)
3547!
3548! If IFLAG = 0 on input, then FCN is only being called to allow the user
3549! to print out the current iterate.
3550! If IFLAG = 1 on input, FCN should calculate the functions at X and
3551! return this vector in FVEC.
3552! To terminate the algorithm, FCN may set IFLAG negative on return.
3553!
3554! Input, integer ( kind = 4 ) M, the number of functions.
3555!
3556! Input, integer ( kind = 4 ) N, the number of variables.
3557! N must not exceed M.
3558!
3559! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
3560! estimate of the solution vector. On output X contains the final
3561! estimate of the solution vector.
3562!
3563! Output, real ( kind = 8 ) FVEC(M), the functions evaluated at the output X.
3564!
3565! Input, real ( kind = 8 ) TOL. Termination occurs when the algorithm
3566! estimates either that the relative error in the sum of squares is at
3567! most TOL or that the relative error between X and the solution is at
3568! most TOL. TOL should be nonnegative.
3569!
3570! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
3571! execution, INFO is set to the (negative) value of IFLAG. See description
3572! of FCN. Otherwise, INFO is set as follows:
3573! 0, improper input parameters.
3574! 1, algorithm estimates that the relative error in the sum of squares
3575! is at most TOL.
3576! 2, algorithm estimates that the relative error between X and the
3577! solution is at most TOL.
3578! 3, conditions for INFO = 1 and INFO = 2 both hold.
3579! 4, FVEC is orthogonal to the columns of the jacobian to machine precision.
3580! 5, number of calls to FCN has reached or exceeded 200*(N+1).
3581! 6, TOL is too small. No further reduction in the sum of squares
3582! is possible.
3583! 7, TOL is too small. No further improvement in the approximate
3584! solution X is possible.
3585!
3586 implicit none
3587
3588 integer ( kind = 4 ), intent(in) :: m
3589 integer ( kind = 4 ), intent(in) :: n
3590
3591 real ( kind = 8 ) diag(n)
3592 real ( kind = 8 ) epsfcn
3593 real ( kind = 8 ) factor
3594 external fcn
3595 real ( kind = 8 ) fjac(m,n)
3596 real ( kind = 8 ) ftol
3597 real ( kind = 8 ), intent(out) :: fvec(m)
3598 real ( kind = 8 ) gtol
3599 integer ( kind = 4 ), intent(out) :: info
3600 integer ( kind = 4 ) ipvt(n)
3601 integer ( kind = 4 ) ldfjac
3602 integer ( kind = 4 ) maxfev
3603 integer ( kind = 4 ) mode
3604 integer ( kind = 4 ) nfev
3605 integer ( kind = 4 ) nprint
3606 real ( kind = 8 ) qtf(n)
3607 real ( kind = 8 ), intent(in) :: tol
3608 real ( kind = 8 ), intent(inout) :: x(n)
3609 real ( kind = 8 ) xtol
3610
3611 info = 0
3612
3613 if ( n <= 0 ) then
3614 return
3615 else if ( m < n ) then
3616 return
3617 else if ( tol < 0.0d+00 ) then
3618 return
3619 end if
3620
3621 ! *** BVIE BEGIN ***
3622 !factor = 100.0D+00
3623 factor = 0.1d+00
3624 ! *** BVIE END ***
3625 maxfev = 200 * ( n + 1 )
3626 ftol = tol
3627 xtol = tol
3628 gtol = 0.0d+00
3629 epsfcn = 0.0d+00
3630 mode = 1
3631 nprint = 0
3632 ldfjac = m
3633
3634 call lmdif ( fcn, m, n, x, fvec, ftol, xtol, gtol, maxfev, epsfcn, &
3635 diag, mode, factor, nprint, info, nfev, fjac, ldfjac, ipvt, qtf )
3636
3637 if ( info == 8 ) then
3638 info = 4
3639 end if
3640
3641 return
3642end
3643subroutine lmpar ( n, r, ldr, ipvt, diag, qtb, delta, par, x, sdiag )
3644
3645!*****************************************************************************80
3646!
3647!! LMPAR computes a parameter for the Levenberg-Marquardt method.
3648!
3649! Discussion:
3650!
3651! Given an M by N matrix A, an N by N nonsingular diagonal
3652! matrix D, an M-vector B, and a positive number DELTA,
3653! the problem is to determine a value for the parameter
3654! PAR such that if X solves the system
3655!
3656! A*X = B,
3657! sqrt ( PAR ) * D * X = 0,
3658!
3659! in the least squares sense, and DXNORM is the euclidean
3660! norm of D*X, then either PAR is zero and
3661!
3662! ( DXNORM - DELTA ) <= 0.1 * DELTA,
3663!
3664! or PAR is positive and
3665!
3666! abs ( DXNORM - DELTA) <= 0.1 * DELTA.
3667!
3668! This subroutine completes the solution of the problem
3669! if it is provided with the necessary information from the
3670! QR factorization, with column pivoting, of A. That is, if
3671! A*P = Q*R, where P is a permutation matrix, Q has orthogonal
3672! columns, and R is an upper triangular matrix with diagonal
3673! elements of nonincreasing magnitude, then LMPAR expects
3674! the full upper triangle of R, the permutation matrix P,
3675! and the first N components of Q'*B. On output
3676! LMPAR also provides an upper triangular matrix S such that
3677!
3678! P' * ( A' * A + PAR * D * D ) * P = S'* S.
3679!
3680! S is employed within LMPAR and may be of separate interest.
3681!
3682! Only a few iterations are generally needed for convergence
3683! of the algorithm. If, however, the limit of 10 iterations
3684! is reached, then the output PAR will contain the best
3685! value obtained so far.
3686!
3687! Licensing:
3688!
3689! This code is distributed under the GNU LGPL license.
3690!
3691! Modified:
3692!
3693! 24 January 2014
3694!
3695! Author:
3696!
3697! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
3698! FORTRAN90 version by John Burkardt.
3699!
3700! Reference:
3701!
3702! Jorge More, Burton Garbow, Kenneth Hillstrom,
3703! User Guide for MINPACK-1,
3704! Technical Report ANL-80-74,
3705! Argonne National Laboratory, 1980.
3706!
3707! Parameters:
3708!
3709! Input, integer ( kind = 4 ) N, the order of R.
3710!
3711! Input/output, real ( kind = 8 ) R(LDR,N),the N by N matrix. The full
3712! upper triangle must contain the full upper triangle of the matrix R.
3713! On output the full upper triangle is unaltered, and the strict lower
3714! triangle contains the strict upper triangle (transposed) of the upper
3715! triangular matrix S.
3716!
3717! Input, integer ( kind = 4 ) LDR, the leading dimension of R. LDR must be
3718! no less than N.
3719!
3720! Input, integer ( kind = 4 ) IPVT(N), defines the permutation matrix P
3721! such that A*P = Q*R. Column J of P is column IPVT(J) of the
3722! identity matrix.
3723!
3724! Input, real ( kind = 8 ) DIAG(N), the diagonal elements of the matrix D.
3725!
3726! Input, real ( kind = 8 ) QTB(N), the first N elements of the vector Q'*B.
3727!
3728! Input, real ( kind = 8 ) DELTA, an upper bound on the euclidean norm
3729! of D*X. DELTA should be positive.
3730!
3731! Input/output, real ( kind = 8 ) PAR. On input an initial estimate of the
3732! Levenberg-Marquardt parameter. On output the final estimate.
3733! PAR should be nonnegative.
3734!
3735! Output, real ( kind = 8 ) X(N), the least squares solution of the system
3736! A*X = B, sqrt(PAR)*D*X = 0, for the output value of PAR.
3737!
3738! Output, real ( kind = 8 ) SDIAG(N), the diagonal elements of the upper
3739! triangular matrix S.
3740!
3741 implicit none
3742
3743 integer ( kind = 4 ), intent(in) :: ldr
3744 integer ( kind = 4 ), intent(in) :: n
3745
3746 real ( kind = 8 ), intent(in) :: delta
3747 real ( kind = 8 ), intent(in) :: diag(n)
3748 real ( kind = 8 ) dwarf
3749 real ( kind = 8 ) dxnorm
3750 real ( kind = 8 ) enorm
3751 real ( kind = 8 ) gnorm
3752 real ( kind = 8 ) fp
3753 integer ( kind = 4 ), intent(in) :: ipvt(n)
3754 integer ( kind = 4 ) iter
3755 integer ( kind = 4 ) j
3756 integer ( kind = 4 ) k
3757 integer ( kind = 4 ) l
3758 integer ( kind = 4 ) nsing
3759 real ( kind = 8 ), intent(inout) :: par
3760 real ( kind = 8 ) parc
3761 real ( kind = 8 ) parl
3762 real ( kind = 8 ) paru
3763 real ( kind = 8 ), intent(in) :: qtb(n)
3764 real ( kind = 8 ), intent(inout) :: r(ldr,n)
3765 real ( kind = 8 ), intent(out) :: sdiag(n)
3766 real ( kind = 8 ) sum2
3767 real ( kind = 8 ) temp
3768 real ( kind = 8 ) wa1(n)
3769 real ( kind = 8 ) wa2(n)
3770 real ( kind = 8 ), intent(out) :: x(n)
3771!
3772! DWARF is the smallest positive magnitude.
3773!
3774 dwarf = tiny( dwarf )
3775!
3776! Compute and store in X the Gauss-Newton direction.
3777!
3778! If the jacobian is rank-deficient, obtain a least squares solution.
3779!
3780 nsing = n
3781
3782 do j = 1, n
3783 wa1(j) = qtb(j)
3784 if ( r(j,j) == 0.0d+00 .and. nsing == n ) then
3785 nsing = j - 1
3786 end if
3787 if ( nsing < n ) then
3788 wa1(j) = 0.0d+00
3789 end if
3790 end do
3791
3792 do k = 1, nsing
3793 j = nsing - k + 1
3794 wa1(j) = wa1(j) / r(j,j)
3795 temp = wa1(j)
3796 wa1(1:j-1) = wa1(1:j-1) - r(1:j-1,j) * temp
3797 end do
3798
3799 do j = 1, n
3800 l = ipvt(j)
3801 x(l) = wa1(j)
3802 end do
3803!
3804! Initialize the iteration counter.
3805! Evaluate the function at the origin, and test
3806! for acceptance of the Gauss-Newton direction.
3807!
3808 iter = 0
3809 wa2(1:n) = diag(1:n) * x(1:n)
3810 dxnorm = enorm( n, wa2 )
3811 fp = dxnorm - delta
3812
3813 if ( fp <= 0.1d+00 * delta ) then
3814 if ( iter == 0 ) then
3815 par = 0.0d+00
3816 end if
3817 return
3818 end if
3819!
3820! If the jacobian is not rank deficient, the Newton
3821! step provides a lower bound, PARL, for the zero of
3822! the function.
3823!
3824! Otherwise set this bound to zero.
3825!
3826 parl = 0.0d+00
3827
3828 if ( n <= nsing ) then
3829
3830 do j = 1, n
3831 l = ipvt(j)
3832 wa1(j) = diag(l) * ( wa2(l) / dxnorm )
3833 end do
3834
3835 do j = 1, n
3836 sum2 = dot_product( wa1(1:j-1), r(1:j-1,j) )
3837 wa1(j) = ( wa1(j) - sum2 ) / r(j,j)
3838 end do
3839
3840 temp = enorm( n, wa1 )
3841 parl = ( ( fp / delta ) / temp ) / temp
3842
3843 end if
3844!
3845! Calculate an upper bound, PARU, for the zero of the function.
3846!
3847 do j = 1, n
3848 sum2 = dot_product( qtb(1:j), r(1:j,j) )
3849 l = ipvt(j)
3850 wa1(j) = sum2 / diag(l)
3851 end do
3852
3853 gnorm = enorm( n, wa1 )
3854 paru = gnorm / delta
3855
3856 if ( paru == 0.0d+00 ) then
3857 paru = dwarf / min( delta, 0.1d+00 )
3858 end if
3859!
3860! If the input PAR lies outside of the interval (PARL, PARU),
3861! set PAR to the closer endpoint.
3862!
3863 par = max( par, parl )
3864 par = min( par, paru )
3865 if ( par == 0.0d+00 ) then
3866 par = gnorm / dxnorm
3867 end if
3868!
3869! Beginning of an iteration.
3870!
3871 do
3872
3873 iter = iter + 1
3874!
3875! Evaluate the function at the current value of PAR.
3876!
3877 if ( par == 0.0d+00 ) then
3878 par = max( dwarf, 0.001d+00 * paru )
3879 end if
3880
3881 wa1(1:n) = sqrt( par ) * diag(1:n)
3882
3883 call qrsolv ( n, r, ldr, ipvt, wa1, qtb, x, sdiag )
3884
3885 wa2(1:n) = diag(1:n) * x(1:n)
3886 dxnorm = enorm( n, wa2 )
3887 temp = fp
3888 fp = dxnorm - delta
3889!
3890! If the function is small enough, accept the current value of PAR.
3891!
3892 if ( abs( fp ) <= 0.1d+00 * delta ) then
3893 exit
3894 end if
3895!
3896! Test for the exceptional cases where PARL
3897! is zero or the number of iterations has reached 10.
3898!
3899 if ( parl == 0.0d+00 .and. fp <= temp .and. temp < 0.0d+00 ) then
3900 exit
3901 else if ( iter == 10 ) then
3902 exit
3903 end if
3904!
3905! Compute the Newton correction.
3906!
3907 do j = 1, n
3908 l = ipvt(j)
3909 wa1(j) = diag(l) * ( wa2(l) / dxnorm )
3910 end do
3911
3912 do j = 1, n
3913 wa1(j) = wa1(j) / sdiag(j)
3914 temp = wa1(j)
3915 wa1(j+1:n) = wa1(j+1:n) - r(j+1:n,j) * temp
3916 end do
3917
3918 temp = enorm( n, wa1 )
3919 parc = ( ( fp / delta ) / temp ) / temp
3920!
3921! Depending on the sign of the function, update PARL or PARU.
3922!
3923 if ( 0.0d+00 < fp ) then
3924 parl = max( parl, par )
3925 else if ( fp < 0.0d+00 ) then
3926 paru = min( paru, par )
3927 end if
3928!
3929! Compute an improved estimate for PAR.
3930!
3931 par = max( parl, par + parc )
3932!
3933! End of an iteration.
3934!
3935 end do
3936!
3937! Termination.
3938!
3939 if ( iter == 0 ) then
3940 par = 0.0d+00
3941 end if
3942
3943 return
3944end
3945subroutine lmstr ( fcn, m, n, x, fvec, fjac, ldfjac, ftol, xtol, gtol, maxfev, &
3946 diag, mode, factor, nprint, info, nfev, njev, ipvt, qtf )
3947
3948!*****************************************************************************80
3949!
3950!! LMSTR minimizes M functions in N variables using Levenberg-Marquardt method.
3951!
3952! Discussion:
3953!
3954! LMSTR minimizes the sum of the squares of M nonlinear functions in
3955! N variables by a modification of the Levenberg-Marquardt algorithm
3956! which uses minimal storage.
3957!
3958! The user must provide a subroutine which calculates the functions and
3959! the rows of the jacobian.
3960!
3961! Licensing:
3962!
3963! This code is distributed under the GNU LGPL license.
3964!
3965! Modified:
3966!
3967! 06 April 2010
3968!
3969! Author:
3970!
3971! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
3972! FORTRAN90 version by John Burkardt.
3973!
3974! Reference:
3975!
3976! Jorge More, Burton Garbow, Kenneth Hillstrom,
3977! User Guide for MINPACK-1,
3978! Technical Report ANL-80-74,
3979! Argonne National Laboratory, 1980.
3980!
3981! Parameters:
3982!
3983! Input, external FCN, the name of the user-supplied subroutine which
3984! calculates the functions and the rows of the jacobian.
3985! FCN should have the form:
3986! subroutine fcn ( m, n, x, fvec, fjrow, iflag )
3987! integer ( kind = 4 ) m
3988! integer ( kind = 4 ) n
3989! real ( kind = 8 ) fjrow(n)
3990! real ( kind = 8 ) fvec(m)
3991! integer ( kind = 4 ) iflag
3992! real ( kind = 8 ) x(n)
3993! If IFLAG = 0 on input, then FCN is only being called to allow the user
3994! to print out the current iterate.
3995! If IFLAG = 1 on input, FCN should calculate the functions at X and
3996! return this vector in FVEC.
3997! If the input value of IFLAG is I > 1, calculate the (I-1)-st row of
3998! the jacobian at X, and return this vector in FJROW.
3999! To terminate the algorithm, set the output value of IFLAG negative.
4000!
4001! Input, integer ( kind = 4 ) M, the number of functions.
4002!
4003! Input, integer ( kind = 4 ) N, the number of variables.
4004! N must not exceed M.
4005!
4006! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
4007! estimate of the solution vector. On output X contains the final
4008! estimate of the solution vector.
4009!
4010! Output, real ( kind = 8 ) FVEC(M), the functions evaluated at the output X.
4011!
4012! Output, real ( kind = 8 ) FJAC(LDFJAC,N), an N by N array. The upper
4013! triangle of FJAC contains an upper triangular matrix R such that
4014!
4015! P' * ( JAC' * JAC ) * P = R' * R,
4016!
4017! where P is a permutation matrix and JAC is the final calculated jacobian.
4018! Column J of P is column IPVT(J) of the identity matrix. The lower
4019! triangular part of FJAC contains information generated during
4020! the computation of R.
4021!
4022! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of FJAC.
4023! LDFJAC must be at least N.
4024!
4025! Input, real ( kind = 8 ) FTOL. Termination occurs when both the actual and
4026! predicted relative reductions in the sum of squares are at most FTOL.
4027! Therefore, FTOL measures the relative error desired in the sum of
4028! squares. FTOL should be nonnegative.
4029!
4030! Input, real ( kind = 8 ) XTOL. Termination occurs when the relative error
4031! between two consecutive iterates is at most XTOL. XTOL should be
4032! nonnegative.
4033!
4034! Input, real ( kind = 8 ) GTOL. termination occurs when the cosine of the
4035! angle between FVEC and any column of the jacobian is at most GTOL in
4036! absolute value. Therefore, GTOL measures the orthogonality desired
4037! between the function vector and the columns of the jacobian. GTOL should
4038! be nonnegative.
4039!
4040! Input, integer ( kind = 4 ) MAXFEV. Termination occurs when the number
4041! of calls to FCN with IFLAG = 1 is at least MAXFEV by the end of
4042! an iteration.
4043!
4044! Input/output, real ( kind = 8 ) DIAG(N). If MODE = 1, then DIAG is set
4045! internally. If MODE = 2, then DIAG must contain positive entries that
4046! serve as multiplicative scale factors for the variables.
4047!
4048! Input, integer ( kind = 4 ) MODE, scaling option.
4049! 1, variables will be scaled internally.
4050! 2, scaling is specified by the input DIAG vector.
4051!
4052! Input, real ( kind = 8 ) FACTOR, determines the initial step bound. This
4053! bound is set to the product of FACTOR and the euclidean norm of DIAG*X if
4054! nonzero, or else to FACTOR itself. In most cases, FACTOR should lie
4055! in the interval (0.1, 100) with 100 the recommended value.
4056!
4057! Input, integer ( kind = 4 ) NPRINT, enables controlled printing of iterates
4058! if it is positive. In this case, FCN is called with IFLAG = 0 at the
4059! beginning of the first iteration and every NPRINT iterations thereafter
4060! and immediately prior to return, with X and FVEC available
4061! for printing. If NPRINT is not positive, no special calls
4062! of FCN with IFLAG = 0 are made.
4063!
4064! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
4065! execution, INFO is set to the (negative) value of IFLAG. See the
4066! description of FCN. Otherwise, INFO is set as follows:
4067! 0, improper input parameters.
4068! 1, both actual and predicted relative reductions in the sum of squares
4069! are at most FTOL.
4070! 2, relative error between two consecutive iterates is at most XTOL.
4071! 3, conditions for INFO = 1 and INFO = 2 both hold.
4072! 4, the cosine of the angle between FVEC and any column of the jacobian
4073! is at most GTOL in absolute value.
4074! 5, number of calls to FCN with IFLAG = 1 has reached MAXFEV.
4075! 6, FTOL is too small. No further reduction in the sum of squares is
4076! possible.
4077! 7, XTOL is too small. No further improvement in the approximate
4078! solution X is possible.
4079! 8, GTOL is too small. FVEC is orthogonal to the columns of the
4080! jacobian to machine precision.
4081!
4082! Output, integer ( kind = 4 ) NFEV, the number of calls to FCN
4083! with IFLAG = 1.
4084!
4085! Output, integer ( kind = 4 ) NJEV, the number of calls to FCN
4086! with IFLAG = 2.
4087!
4088! Output, integer ( kind = 4 ) IPVT(N), defines a permutation matrix P such
4089! that JAC * P = Q * R, where JAC is the final calculated jacobian, Q is
4090! orthogonal (not stored), and R is upper triangular.
4091! Column J of P is column IPVT(J) of the identity matrix.
4092!
4093! Output, real ( kind = 8 ) QTF(N), contains the first N elements of Q'*FVEC.
4094!
4095 implicit none
4096
4097 integer ( kind = 4 ), intent(in) :: ldfjac
4098 integer ( kind = 4 ), intent(in) :: m
4099 integer ( kind = 4 ), intent(in) :: n
4100
4101 real ( kind = 8 ) actred
4102 real ( kind = 8 ) delta
4103 real ( kind = 8 ), intent(inout) :: diag(n)
4104 real ( kind = 8 ) dirder
4105 real ( kind = 8 ) enorm
4106 real ( kind = 8 ) epsmch
4107 real ( kind = 8 ), intent(in) :: factor
4108 external fcn
4109 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
4110 real ( kind = 8 ) fnorm
4111 real ( kind = 8 ) fnorm1
4112 real ( kind = 8 ), intent(in) :: ftol
4113 real ( kind = 8 ), intent(out) :: fvec(m)
4114 real ( kind = 8 ) gnorm
4115 real ( kind = 8 ), intent(in) :: gtol
4116 integer ( kind = 4 ) i
4117 integer ( kind = 4 ) iflag
4118 integer ( kind = 4 ), intent(out) :: info
4119 integer ( kind = 4 ), intent(out) :: ipvt(n)
4120 integer ( kind = 4 ) iter
4121 integer ( kind = 4 ) j
4122 integer ( kind = 4 ) l
4123 integer ( kind = 4 ), intent(in) :: maxfev
4124 integer ( kind = 4 ), intent(in) :: mode
4125 integer ( kind = 4 ), intent(out) :: nfev
4126 integer ( kind = 4 ), intent(out) :: njev
4127 integer ( kind = 4 ), intent(in) :: nprint
4128 real ( kind = 8 ) par
4129 logical pivot
4130 real ( kind = 8 ) pnorm
4131 real ( kind = 8 ) prered
4132 real ( kind = 8 ), intent(out) :: qtf(n)
4133 real ( kind = 8 ) ratio
4134 logical sing
4135 real ( kind = 8 ) sum2
4136 real ( kind = 8 ) temp
4137 real ( kind = 8 ) temp1
4138 real ( kind = 8 ) temp2
4139 real ( kind = 8 ) wa1(n)
4140 real ( kind = 8 ) wa2(n)
4141 real ( kind = 8 ) wa3(n)
4142 real ( kind = 8 ) wa4(m)
4143 real ( kind = 8 ), intent(inout) :: x(n)
4144 real ( kind = 8 ) xnorm
4145 real ( kind = 8 ), intent(in) :: xtol
4146
4147 epsmch = epsilon( epsmch )
4148
4149 info = 0
4150 iflag = 0
4151 nfev = 0
4152 njev = 0
4153!
4154! Check the input parameters for errors.
4155!
4156 if ( n <= 0 ) then
4157 go to 340
4158 else if ( m < n ) then
4159 go to 340
4160 else if ( ldfjac < n ) then
4161 go to 340
4162 else if ( ftol < 0.0d+00 ) then
4163 go to 340
4164 else if ( xtol < 0.0d+00 ) then
4165 go to 340
4166 else if ( gtol < 0.0d+00 ) then
4167 go to 340
4168 else if ( maxfev <= 0 ) then
4169 go to 340
4170 else if ( factor <= 0.0d+00 ) then
4171 go to 340
4172 end if
4173
4174 if ( mode == 2 ) then
4175 do j = 1, n
4176 if ( diag(j) <= 0.0d+00 ) then
4177 go to 340
4178 end if
4179 end do
4180 end if
4181!
4182! Evaluate the function at the starting point and calculate its norm.
4183!
4184 iflag = 1
4185 call fcn ( m, n, x, fvec, wa3, iflag )
4186 nfev = 1
4187
4188 if ( iflag < 0 ) then
4189 go to 340
4190 end if
4191
4192 fnorm = enorm( m, fvec )
4193!
4194! Initialize Levenberg-Marquardt parameter and iteration counter.
4195!
4196 par = 0.0d+00
4197 iter = 1
4198!
4199! Beginning of the outer loop.
4200!
4201 30 continue
4202!
4203! If requested, call FCN to enable printing of iterates.
4204!
4205 if ( 0 < nprint ) then
4206 iflag = 0
4207 if ( mod( iter-1, nprint ) == 0 ) then
4208 call fcn ( m, n, x, fvec, wa3, iflag )
4209 end if
4210 if ( iflag < 0 ) then
4211 go to 340
4212 end if
4213 end if
4214!
4215! Compute the QR factorization of the jacobian matrix calculated one row
4216! at a time, while simultaneously forming Q'* FVEC and storing
4217! the first N components in QTF.
4218!
4219 qtf(1:n) = 0.0d+00
4220 fjac(1:n,1:n) = 0.0d+00
4221 iflag = 2
4222
4223 do i = 1, m
4224 call fcn ( m, n, x, fvec, wa3, iflag )
4225 if ( iflag < 0 ) then
4226 go to 340
4227 end if
4228 temp = fvec(i)
4229 call rwupdt ( n, fjac, ldfjac, wa3, qtf, temp, wa1, wa2 )
4230 iflag = iflag + 1
4231 end do
4232
4233 njev = njev + 1
4234!
4235! If the jacobian is rank deficient, call QRFAC to
4236! reorder its columns and update the components of QTF.
4237!
4238 sing = .false.
4239 do j = 1, n
4240 if ( fjac(j,j) == 0.0d+00 ) then
4241 sing = .true.
4242 end if
4243 ipvt(j) = j
4244 wa2(j) = enorm( j, fjac(1,j) )
4245 end do
4246
4247 if ( sing ) then
4248
4249 pivot = .true.
4250 call qrfac ( n, n, fjac, ldfjac, pivot, ipvt, n, wa1, wa2 )
4251
4252 do j = 1, n
4253
4254 if ( fjac(j,j) /= 0.0d+00 ) then
4255
4256 sum2 = dot_product( qtf(j:n), fjac(j:n,j) )
4257 temp = - sum2 / fjac(j,j)
4258 qtf(j:n) = qtf(j:n) + fjac(j:n,j) * temp
4259
4260 end if
4261
4262 fjac(j,j) = wa1(j)
4263
4264 end do
4265
4266 end if
4267!
4268! On the first iteration
4269! if mode is 1,
4270! scale according to the norms of the columns of the initial jacobian.
4271! calculate the norm of the scaled X,
4272! initialize the step bound delta.
4273!
4274 if ( iter == 1 ) then
4275
4276 if ( mode /= 2 ) then
4277
4278 diag(1:n) = wa2(1:n)
4279 do j = 1, n
4280 if ( wa2(j) == 0.0d+00 ) then
4281 diag(j) = 1.0d+00
4282 end if
4283 end do
4284
4285 end if
4286
4287 wa3(1:n) = diag(1:n) * x(1:n)
4288 xnorm = enorm( n, wa3 )
4289 delta = factor * xnorm
4290 if ( delta == 0.0d+00 ) then
4291 delta = factor
4292 end if
4293
4294 end if
4295!
4296! Compute the norm of the scaled gradient.
4297!
4298 gnorm = 0.0d+00
4299
4300 if ( fnorm /= 0.0d+00 ) then
4301
4302 do j = 1, n
4303 l = ipvt(j)
4304 if ( wa2(l) /= 0.0d+00 ) then
4305 sum2 = dot_product( qtf(1:j), fjac(1:j,j) ) / fnorm
4306 gnorm = max( gnorm, abs( sum2 / wa2(l) ) )
4307 end if
4308 end do
4309
4310 end if
4311!
4312! Test for convergence of the gradient norm.
4313!
4314 if ( gnorm <= gtol ) then
4315 info = 4
4316 go to 340
4317 end if
4318!
4319! Rescale if necessary.
4320!
4321 if ( mode /= 2 ) then
4322 do j = 1, n
4323 diag(j) = max( diag(j), wa2(j) )
4324 end do
4325 end if
4326!
4327! Beginning of the inner loop.
4328!
4329240 continue
4330!
4331! Determine the Levenberg-Marquardt parameter.
4332!
4333 call lmpar ( n, fjac, ldfjac, ipvt, diag, qtf, delta, par, wa1, wa2 )
4334!
4335! Store the direction P and X + P.
4336! Calculate the norm of P.
4337!
4338 wa1(1:n) = -wa1(1:n)
4339 wa2(1:n) = x(1:n) + wa1(1:n)
4340 wa3(1:n) = diag(1:n) * wa1(1:n)
4341 pnorm = enorm( n, wa3 )
4342!
4343! On the first iteration, adjust the initial step bound.
4344!
4345 if ( iter == 1 ) then
4346 delta = min( delta, pnorm )
4347 end if
4348!
4349! Evaluate the function at X + P and calculate its norm.
4350!
4351 iflag = 1
4352 call fcn ( m, n, wa2, wa4, wa3, iflag )
4353 nfev = nfev + 1
4354 if ( iflag < 0 ) then
4355 go to 340
4356 end if
4357 fnorm1 = enorm( m, wa4 )
4358!
4359! Compute the scaled actual reduction.
4360!
4361 if ( 0.1d+00 * fnorm1 < fnorm ) then
4362 actred = 1.0d+00 - ( fnorm1 / fnorm ) ** 2
4363 else
4364 actred = -1.0d+00
4365 end if
4366!
4367! Compute the scaled predicted reduction and
4368! the scaled directional derivative.
4369!
4370 do j = 1, n
4371 wa3(j) = 0.0d+00
4372 l = ipvt(j)
4373 temp = wa1(l)
4374 wa3(1:j) = wa3(1:j) + fjac(1:j,j) * temp
4375 end do
4376
4377 temp1 = enorm( n, wa3 ) / fnorm
4378 temp2 = ( sqrt(par) * pnorm ) / fnorm
4379 prered = temp1 ** 2 + temp2 ** 2 / 0.5d+00
4380 dirder = - ( temp1 ** 2 + temp2 ** 2 )
4381!
4382! Compute the ratio of the actual to the predicted reduction.
4383!
4384 if ( prered /= 0.0d+00 ) then
4385 ratio = actred / prered
4386 else
4387 ratio = 0.0d+00
4388 end if
4389!
4390! Update the step bound.
4391!
4392 if ( ratio <= 0.25d+00 ) then
4393
4394 if ( actred >= 0.0d+00 ) then
4395 temp = 0.5d+00
4396 else
4397 temp = 0.5d+00 * dirder / ( dirder + 0.5d+00 * actred )
4398 end if
4399
4400 if ( 0.1d+00 * fnorm1 >= fnorm .or. temp < 0.1d+00 ) then
4401 temp = 0.1d+00
4402 end if
4403
4404 delta = temp * min( delta, pnorm / 0.1d+00 )
4405 par = par / temp
4406
4407 else
4408
4409 if ( par == 0.0d+00 .or. ratio >= 0.75d+00 ) then
4410 delta = pnorm / 0.5d+00
4411 par = 0.5d+00 * par
4412 end if
4413
4414 end if
4415!
4416! Test for successful iteration.
4417!
4418 if ( ratio >= 0.0001d+00 ) then
4419 x(1:n) = wa2(1:n)
4420 wa2(1:n) = diag(1:n) * x(1:n)
4421 fvec(1:m) = wa4(1:m)
4422 xnorm = enorm( n, wa2 )
4423 fnorm = fnorm1
4424 iter = iter + 1
4425 end if
4426!
4427! Tests for convergence, termination and stringent tolerances.
4428!
4429 if ( abs( actred ) <= ftol .and. prered <= ftol &
4430 .and. 0.5d+00 * ratio <= 1.0d+00 ) then
4431 info = 1
4432 end if
4433
4434 if ( delta <= xtol * xnorm ) then
4435 info = 2
4436 end if
4437
4438 if ( abs( actred ) <= ftol .and. prered <= ftol &
4439 .and. 0.5d+00 * ratio <= 1.0d+00 .and. info == 2 ) then
4440 info = 3
4441 end if
4442
4443 if ( info /= 0 ) then
4444 go to 340
4445 end if
4446
4447 if ( nfev >= maxfev) then
4448 info = 5
4449 end if
4450
4451 if ( abs( actred ) <= epsmch .and. prered <= epsmch &
4452 .and. 0.5d+00 * ratio <= 1.0d+00 ) then
4453 info = 6
4454 end if
4455
4456 if ( delta <= epsmch * xnorm ) then
4457 info = 7
4458 end if
4459
4460 if ( gnorm <= epsmch ) then
4461 info = 8
4462 end if
4463
4464 if ( info /= 0 ) then
4465 go to 340
4466 end if
4467!
4468! End of the inner loop. Repeat if iteration unsuccessful.
4469!
4470 if ( ratio < 0.0001d+00 ) then
4471 go to 240
4472 end if
4473!
4474! End of the outer loop.
4475!
4476 go to 30
4477
4478 340 continue
4479!
4480! Termination, either normal or user imposed.
4481!
4482 if ( iflag < 0 ) then
4483 info = iflag
4484 end if
4485
4486 iflag = 0
4487
4488 if ( 0 < nprint ) then
4489 call fcn ( m, n, x, fvec, wa3, iflag )
4490 end if
4491
4492 return
4493end
4494subroutine lmstr1 ( fcn, m, n, x, fvec, fjac, ldfjac, tol, info )
4495
4496!*****************************************************************************80
4497!
4498!! LMSTR1 minimizes M functions in N variables using Levenberg-Marquardt method.
4499!
4500! Discussion:
4501!
4502! LMSTR1 minimizes the sum of the squares of M nonlinear functions in
4503! N variables by a modification of the Levenberg-Marquardt algorithm
4504! which uses minimal storage.
4505!
4506! This is done by using the more general least-squares solver
4507! LMSTR. The user must provide a subroutine which calculates
4508! the functions and the rows of the jacobian.
4509!
4510! Licensing:
4511!
4512! This code is distributed under the GNU LGPL license.
4513!
4514! Modified:
4515!
4516! 19 August 2016
4517!
4518! Author:
4519!
4520! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
4521! FORTRAN90 version by John Burkardt.
4522!
4523! Reference:
4524!
4525! Jorge More, Burton Garbow, Kenneth Hillstrom,
4526! User Guide for MINPACK-1,
4527! Technical Report ANL-80-74,
4528! Argonne National Laboratory, 1980.
4529!
4530! Parameters:
4531!
4532! Input, external FCN, the name of the user-supplied subroutine which
4533! calculates the functions and the rows of the jacobian.
4534! FCN should have the form:
4535! subroutine fcn ( m, n, x, fvec, fjrow, iflag )
4536! integer ( kind = 4 ) m
4537! integer ( kind = 4 ) n
4538! real ( kind = 8 ) fjrow(n)
4539! real ( kind = 8 ) fvec(m)
4540! integer ( kind = 4 ) iflag
4541! real ( kind = 8 ) x(n)
4542! If IFLAG = 0 on input, then FCN is only being called to allow the user
4543! to print out the current iterate.
4544! If IFLAG = 1 on input, FCN should calculate the functions at X and
4545! return this vector in FVEC.
4546! If the input value of IFLAG is I > 1, calculate the (I-1)-st row of
4547! the jacobian at X, and return this vector in FJROW.
4548! To terminate the algorithm, set the output value of IFLAG negative.
4549!
4550! Input, integer ( kind = 4 ) M, the number of functions.
4551!
4552! Input, integer ( kind = 4 ) N, the number of variables.
4553! N must not exceed M.
4554!
4555! Input/output, real ( kind = 8 ) X(N). On input, X must contain an initial
4556! estimate of the solution vector. On output X contains the final
4557! estimate of the solution vector.
4558!
4559! Output, real ( kind = 8 ) FVEC(M), the functions evaluated at the output X.
4560!
4561! Output, real ( kind = 8 ) FJAC(LDFJAC,N), an N by N array. The upper
4562! triangle contains an upper triangular matrix R such that
4563!
4564! P' * ( JAC' * JAC ) * P = R' * R,
4565!
4566! where P is a permutation matrix and JAC is the final calculated
4567! jacobian. Column J of P is column IPVT(J) of the identity matrix.
4568! The lower triangular part of FJAC contains information generated
4569! during the computation of R.
4570!
4571! Input, integer ( kind = 4 ) LDFJAC, the leading dimension of FJAC.
4572! LDFJAC must be at least N.
4573!
4574! Input, real ( kind = 8 ) TOL. Termination occurs when the algorithm
4575! estimates either that the relative error in the sum of squares is at
4576! most TOL or that the relative error between X and the solution is at
4577! most TOL. TOL should be nonnegative.
4578!
4579! Output, integer ( kind = 4 ) INFO, error flag. If the user has terminated
4580! execution, INFO is set to the (negative) value of IFLAG. See description
4581! of FCN. Otherwise, INFO is set as follows:
4582! 0, improper input parameters.
4583! 1, algorithm estimates that the relative error in the sum of squares
4584! is at most TOL.
4585! 2, algorithm estimates that the relative error between X and the
4586! solution is at most TOL.
4587! 3, conditions for INFO = 1 and INFO = 2 both hold.
4588! 4, FVEC is orthogonal to the columns of the jacobian to machine precision.
4589! 5, number of calls to FCN with IFLAG = 1 has reached 100*(N+1).
4590! 6, TOL is too small. No further reduction in the sum of squares
4591! is possible.
4592! 7, TOL is too small. No further improvement in the approximate
4593! solution X is possible.
4594!
4595 implicit none
4596
4597 integer ( kind = 4 ), intent(in) :: ldfjac
4598 integer ( kind = 4 ), intent(in) :: m
4599 integer ( kind = 4 ), intent(in) :: n
4600
4601 real ( kind = 8 ) diag(n)
4602 real ( kind = 8 ) factor
4603 external fcn
4604 real ( kind = 8 ), intent(out) :: fjac(ldfjac,n)
4605 real ( kind = 8 ) ftol
4606 real ( kind = 8 ), intent(out) :: fvec(m)
4607 real ( kind = 8 ) gtol
4608 integer ( kind = 4 ), intent(out) :: info
4609 integer ( kind = 4 ) ipvt(n)
4610 integer ( kind = 4 ) maxfev
4611 integer ( kind = 4 ) mode
4612 integer ( kind = 4 ) nfev
4613 integer ( kind = 4 ) njev
4614 integer ( kind = 4 ) nprint
4615 real ( kind = 8 ) qtf(n)
4616 real ( kind = 8 ), intent(in) :: tol
4617 real ( kind = 8 ), intent(inout) :: x(n)
4618 real ( kind = 8 ) xtol
4619
4620 if ( n <= 0 ) then
4621 info = 0
4622 return
4623 end if
4624
4625 if ( m < n ) then
4626 info = 0
4627 return
4628 end if
4629
4630 if ( ldfjac < n ) then
4631 info = 0
4632 return
4633 end if
4634
4635 if ( tol < 0.0d+00 ) then
4636 info = 0
4637 return
4638 end if
4639
4640 fvec(1:n) = 0.0d+00
4641 fjac(1:ldfjac,1:n) = 0.0d+00
4642 ftol = tol
4643 xtol = tol
4644 gtol = 0.0d+00
4645 maxfev = 100 * ( n + 1 )
4646 diag(1:n) = 0.0d+00
4647 mode = 1
4648 factor = 100.0d+00
4649 nprint = 0
4650 info = 0
4651 nfev = 0
4652 njev = 0
4653 ipvt(1:n) = 0
4654 qtf(1:n) = 0.0d+00
4655
4656 call lmstr ( fcn, m, n, x, fvec, fjac, ldfjac, ftol, xtol, gtol, maxfev, &
4657 diag, mode, factor, nprint, info, nfev, njev, ipvt, qtf )
4658
4659 if ( info == 8 ) then
4660 info = 4
4661 end if
4662
4663 return
4664end
4665subroutine qform ( m, n, q, ldq )
4666
4667!*****************************************************************************80
4668!
4669!! QFORM produces the explicit QR factorization of a matrix.
4670!
4671! Discussion:
4672!
4673! The QR factorization of a matrix is usually accumulated in implicit
4674! form, that is, as a series of orthogonal transformations of the
4675! original matrix. This routine carries out those transformations,
4676! to explicitly exhibit the factorization constructed by QRFAC.
4677!
4678! Licensing:
4679!
4680! This code is distributed under the GNU LGPL license.
4681!
4682! Modified:
4683!
4684! 06 April 2010
4685!
4686! Author:
4687!
4688! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
4689! FORTRAN90 version by John Burkardt.
4690!
4691! Reference:
4692!
4693! Jorge More, Burton Garbow, Kenneth Hillstrom,
4694! User Guide for MINPACK-1,
4695! Technical Report ANL-80-74,
4696! Argonne National Laboratory, 1980.
4697!
4698! Parameters:
4699!
4700! Input, integer ( kind = 4 ) M, is a positive integer input variable set
4701! to the number of rows of A and the order of Q.
4702!
4703! Input, integer ( kind = 4 ) N, is a positive integer input variable set
4704! to the number of columns of A.
4705!
4706! Input/output, real ( kind = 8 ) Q(LDQ,M). Q is an M by M array.
4707! On input the full lower trapezoid in the first min(M,N) columns of Q
4708! contains the factored form.
4709! On output, Q has been accumulated into a square matrix.
4710!
4711! Input, integer ( kind = 4 ) LDQ, is a positive integer input variable
4712! not less than M which specifies the leading dimension of the array Q.
4713!
4714 implicit none
4715
4716 integer ( kind = 4 ), intent(in) :: ldq
4717 integer ( kind = 4 ), intent(in) :: m
4718 integer ( kind = 4 ), intent(in) :: n
4719
4720 integer ( kind = 4 ) j
4721 integer ( kind = 4 ) k
4722 integer ( kind = 4 ) l
4723 integer ( kind = 4 ) minmn
4724 real ( kind = 8 ), intent(inout) :: q(ldq,m)
4725 real ( kind = 8 ) temp
4726 real ( kind = 8 ) wa(m)
4727
4728 minmn = min( m, n )
4729
4730 do j = 2, minmn
4731 q(1:j-1,j) = 0.0d+00
4732 end do
4733!
4734! Initialize remaining columns to those of the identity matrix.
4735!
4736 q(1:m,n+1:m) = 0.0d+00
4737
4738 do j = n+1, m
4739 q(j,j) = 1.0d+00
4740 end do
4741!
4742! Accumulate Q from its factored form.
4743!
4744 do l = 1, minmn
4745
4746 k = minmn - l + 1
4747
4748 wa(k:m) = q(k:m,k)
4749
4750 q(k:m,k) = 0.0d+00
4751 q(k,k) = 1.0d+00
4752
4753 if ( wa(k) /= 0.0d+00 ) then
4754
4755 do j = k, m
4756 temp = dot_product( wa(k:m), q(k:m,j) ) / wa(k)
4757 q(k:m,j) = q(k:m,j) - temp * wa(k:m)
4758 end do
4759
4760 end if
4761
4762 end do
4763
4764 return
4765end
4766subroutine qrfac ( m, n, a, lda, pivot, ipvt, lipvt, rdiag, acnorm )
4767
4768!*****************************************************************************80
4769!
4770!! QRFAC computes a QR factorization using Householder transformations.
4771!
4772! Discussion:
4773!
4774! This subroutine uses Householder transformations with column
4775! pivoting (optional) to compute a QR factorization of the
4776! M by N matrix A. That is, QRFAC determines an orthogonal
4777! matrix Q, a permutation matrix P, and an upper trapezoidal
4778! matrix R with diagonal elements of nonincreasing magnitude,
4779! such that A*P = Q*R. The Householder transformation for
4780! column K, K = 1,2,...,min(M,N), is of the form
4781!
4782! I - ( 1 / U(K) ) * U * U'
4783!
4784! where U has zeros in the first K-1 positions. The form of
4785! this transformation and the method of pivoting first
4786! appeared in the corresponding LINPACK subroutine.
4787!
4788! Licensing:
4789!
4790! This code is distributed under the GNU LGPL license.
4791!
4792! Modified:
4793!
4794! 06 April 2010
4795!
4796! Author:
4797!
4798! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
4799! FORTRAN90 version by John Burkardt.
4800!
4801! Reference:
4802!
4803! Jorge More, Burton Garbow, Kenneth Hillstrom,
4804! User Guide for MINPACK-1,
4805! Technical Report ANL-80-74,
4806! Argonne National Laboratory, 1980.
4807!
4808! Parameters:
4809!
4810! Input, integer ( kind = 4 ) M, the number of rows of A.
4811!
4812! Input, integer ( kind = 4 ) N, the number of columns of A.
4813!
4814! Input/output, real ( kind = 8 ) A(LDA,N), the M by N array.
4815! On input, A contains the matrix for which the QR factorization is to
4816! be computed. On output, the strict upper trapezoidal part of A contains
4817! the strict upper trapezoidal part of R, and the lower trapezoidal
4818! part of A contains a factored form of Q (the non-trivial elements of
4819! the U vectors described above).
4820!
4821! Input, integer ( kind = 4 ) LDA, the leading dimension of A, which must
4822! be no less than M.
4823!
4824! Input, logical PIVOT, is TRUE if column pivoting is to be carried out.
4825!
4826! Output, integer ( kind = 4 ) IPVT(LIPVT), defines the permutation matrix P
4827! such that A*P = Q*R. Column J of P is column IPVT(J) of the identity
4828! matrix. If PIVOT is false, IPVT is not referenced.
4829!
4830! Input, integer ( kind = 4 ) LIPVT, the dimension of IPVT, which should
4831! be N if pivoting is used.
4832!
4833! Output, real ( kind = 8 ) RDIAG(N), contains the diagonal elements of R.
4834!
4835! Output, real ( kind = 8 ) ACNORM(N), the norms of the corresponding
4836! columns of the input matrix A. If this information is not needed,
4837! then ACNORM can coincide with RDIAG.
4838!
4839 implicit none
4840
4841 integer ( kind = 4 ), intent(in) :: lda
4842 integer ( kind = 4 ), intent(in) :: lipvt
4843 integer ( kind = 4 ), intent(in) :: m
4844 integer ( kind = 4 ), intent(in) :: n
4845
4846 real ( kind = 8 ), intent(inout) :: a(lda,n)
4847 real ( kind = 8 ), intent(out) :: acnorm(n)
4848 real ( kind = 8 ) ajnorm
4849 real ( kind = 8 ) enorm
4850 real ( kind = 8 ) epsmch
4851 integer ( kind = 4 ) i4_temp
4852 integer ( kind = 4 ), intent(out) :: ipvt(lipvt)
4853 integer ( kind = 4 ) j
4854 integer ( kind = 4 ) k
4855 integer ( kind = 4 ) kmax
4856 integer ( kind = 4 ) minmn
4857 logical, intent(in) :: pivot
4858 real ( kind = 8 ) r8_temp(m)
4859 real ( kind = 8 ), intent(out) :: rdiag(n)
4860 real ( kind = 8 ) temp
4861 real ( kind = 8 ) wa(n)
4862
4863 epsmch = epsilon( epsmch )
4864!
4865! Compute the initial column norms and initialize several arrays.
4866!
4867 do j = 1, n
4868 acnorm(j) = enorm( m, a(1:m,j) )
4869 end do
4870
4871 rdiag(1:n) = acnorm(1:n)
4872 wa(1:n) = acnorm(1:n)
4873
4874 if ( pivot ) then
4875 do j = 1, n
4876 ipvt(j) = j
4877 end do
4878 end if
4879!
4880! Reduce A to R with Householder transformations.
4881!
4882 minmn = min( m, n )
4883
4884 do j = 1, minmn
4885!
4886! Bring the column of largest norm into the pivot position.
4887!
4888 if ( pivot ) then
4889
4890 kmax = j
4891
4892 do k = j, n
4893 if ( rdiag(kmax) < rdiag(k) ) then
4894 kmax = k
4895 end if
4896 end do
4897
4898 if ( kmax /= j ) then
4899
4900 r8_temp(1:m) = a(1:m,j)
4901 a(1:m,j) = a(1:m,kmax)
4902 a(1:m,kmax) = r8_temp(1:m)
4903
4904 rdiag(kmax) = rdiag(j)
4905 wa(kmax) = wa(j)
4906
4907 i4_temp = ipvt(j)
4908 ipvt(j) = ipvt(kmax)
4909 ipvt(kmax) = i4_temp
4910
4911 end if
4912
4913 end if
4914!
4915! Compute the Householder transformation to reduce the
4916! J-th column of A to a multiple of the J-th unit vector.
4917!
4918 ajnorm = enorm( m-j+1, a(j,j) )
4919
4920 if ( ajnorm /= 0.0d+00 ) then
4921
4922 if ( a(j,j) < 0.0d+00 ) then
4923 ajnorm = -ajnorm
4924 end if
4925
4926 a(j:m,j) = a(j:m,j) / ajnorm
4927 a(j,j) = a(j,j) + 1.0d+00
4928!
4929! Apply the transformation to the remaining columns and update the norms.
4930!
4931 do k = j + 1, n
4932
4933 temp = dot_product( a(j:m,j), a(j:m,k) ) / a(j,j)
4934
4935 a(j:m,k) = a(j:m,k) - temp * a(j:m,j)
4936
4937 if ( pivot .and. rdiag(k) /= 0.0d+00 ) then
4938
4939 temp = a(j,k) / rdiag(k)
4940 rdiag(k) = rdiag(k) * sqrt( max( 0.0d+00, 1.0d+00-temp ** 2 ) )
4941
4942 if ( 0.05d+00 * ( rdiag(k) / wa(k) ) ** 2 <= epsmch ) then
4943 rdiag(k) = enorm( m-j, a(j+1,k) )
4944 wa(k) = rdiag(k)
4945 end if
4946
4947 end if
4948
4949 end do
4950
4951 end if
4952
4953 rdiag(j) = - ajnorm
4954
4955 end do
4956
4957 return
4958end
4959subroutine qrsolv ( n, r, ldr, ipvt, diag, qtb, x, sdiag )
4960
4961!*****************************************************************************80
4962!
4963!! QRSOLV solves a rectangular linear system A*x=b in the least squares sense.
4964!
4965! Discussion:
4966!
4967! Given an M by N matrix A, an N by N diagonal matrix D,
4968! and an M-vector B, the problem is to determine an X which
4969! solves the system
4970!
4971! A*X = B
4972! D*X = 0
4973!
4974! in the least squares sense.
4975!
4976! This subroutine completes the solution of the problem
4977! if it is provided with the necessary information from the
4978! QR factorization, with column pivoting, of A. That is, if
4979! Q*P = Q*R, where P is a permutation matrix, Q has orthogonal
4980! columns, and R is an upper triangular matrix with diagonal
4981! elements of nonincreasing magnitude, then QRSOLV expects
4982! the full upper triangle of R, the permutation matrix p,
4983! and the first N components of Q'*B.
4984!
4985! The system is then equivalent to
4986!
4987! R*Z = Q'*B
4988! P'*D*P*Z = 0
4989!
4990! where X = P*Z. If this system does not have full rank,
4991! then a least squares solution is obtained. On output QRSOLV
4992! also provides an upper triangular matrix S such that
4993!
4994! P'*(A'*A + D*D)*P = S'*S.
4995!
4996! S is computed within QRSOLV and may be of separate interest.
4997!
4998! Licensing:
4999!
5000! This code is distributed under the GNU LGPL license.
5001!
5002! Modified:
5003!
5004! 06 April 2010
5005!
5006! Author:
5007!
5008! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
5009! FORTRAN90 version by John Burkardt.
5010!
5011! Reference:
5012!
5013! Jorge More, Burton Garbow, Kenneth Hillstrom,
5014! User Guide for MINPACK-1,
5015! Technical Report ANL-80-74,
5016! Argonne National Laboratory, 1980.
5017!
5018! Parameters:
5019!
5020! Input, integer ( kind = 4 ) N, the order of R.
5021!
5022! Input/output, real ( kind = 8 ) R(LDR,N), the N by N matrix.
5023! On input the full upper triangle must contain the full upper triangle
5024! of the matrix R. On output the full upper triangle is unaltered, and
5025! the strict lower triangle contains the strict upper triangle
5026! (transposed) of the upper triangular matrix S.
5027!
5028! Input, integer ( kind = 4 ) LDR, the leading dimension of R, which must be
5029! at least N.
5030!
5031! Input, integer ( kind = 4 ) IPVT(N), defines the permutation matrix P such
5032! that A*P = Q*R. Column J of P is column IPVT(J) of the identity matrix.
5033!
5034! Input, real ( kind = 8 ) DIAG(N), the diagonal elements of the matrix D.
5035!
5036! Input, real ( kind = 8 ) QTB(N), the first N elements of the vector Q'*B.
5037!
5038! Output, real ( kind = 8 ) X(N), the least squares solution.
5039!
5040! Output, real ( kind = 8 ) SDIAG(N), the diagonal elements of the upper
5041! triangular matrix S.
5042!
5043 implicit none
5044
5045 integer ( kind = 4 ), intent(in) :: ldr
5046 integer ( kind = 4 ), intent(in) :: n
5047
5048 real ( kind = 8 ) c
5049 real ( kind = 8 ) cotan
5050 real ( kind = 8 ), intent(in) :: diag(n)
5051 integer ( kind = 4 ) i
5052 integer ( kind = 4 ), intent(in) :: ipvt(n)
5053 integer ( kind = 4 ) j
5054 integer ( kind = 4 ) k
5055 integer ( kind = 4 ) l
5056 integer ( kind = 4 ) nsing
5057 real ( kind = 8 ), intent(in) :: qtb(n)
5058 real ( kind = 8 ) qtbpj
5059 real ( kind = 8 ), intent(inout) :: r(ldr,n)
5060 real ( kind = 8 ) s
5061 real ( kind = 8 ), intent(out) :: sdiag(n)
5062 real ( kind = 8 ) sum2
5063 real ( kind = 8 ) t
5064 real ( kind = 8 ) temp
5065 real ( kind = 8 ) wa(n)
5066 real ( kind = 8 ), intent(out) :: x(n)
5067!
5068! Copy R and Q'*B to preserve input and initialize S.
5069!
5070! In particular, save the diagonal elements of R in X.
5071!
5072 do j = 1, n
5073 r(j:n,j) = r(j,j:n)
5074 x(j) = r(j,j)
5075 end do
5076
5077 wa(1:n) = qtb(1:n)
5078!
5079! Eliminate the diagonal matrix D using a Givens rotation.
5080!
5081 do j = 1, n
5082!
5083! Prepare the row of D to be eliminated, locating the
5084! diagonal element using P from the QR factorization.
5085!
5086 l = ipvt(j)
5087
5088 if ( diag(l) /= 0.0d+00 ) then
5089
5090 sdiag(j:n) = 0.0d+00
5091 sdiag(j) = diag(l)
5092!
5093! The transformations to eliminate the row of D
5094! modify only a single element of Q'*B
5095! beyond the first N, which is initially zero.
5096!
5097 qtbpj = 0.0d+00
5098
5099 do k = j, n
5100!
5101! Determine a Givens rotation which eliminates the
5102! appropriate element in the current row of D.
5103!
5104 if ( sdiag(k) /= 0.0d+00 ) then
5105
5106 if ( abs( r(k,k) ) < abs( sdiag(k) ) ) then
5107 cotan = r(k,k) / sdiag(k)
5108 s = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * cotan ** 2 )
5109 c = s * cotan
5110 else
5111 t = sdiag(k) / r(k,k)
5112 c = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * t ** 2 )
5113 s = c * t
5114 end if
5115!
5116! Compute the modified diagonal element of R and
5117! the modified element of (Q'*B,0).
5118!
5119 r(k,k) = c * r(k,k) + s * sdiag(k)
5120 temp = c * wa(k) + s * qtbpj
5121 qtbpj = - s * wa(k) + c * qtbpj
5122 wa(k) = temp
5123!
5124! Accumulate the tranformation in the row of S.
5125!
5126 do i = k+1, n
5127 temp = c * r(i,k) + s * sdiag(i)
5128 sdiag(i) = - s * r(i,k) + c * sdiag(i)
5129 r(i,k) = temp
5130 end do
5131
5132 end if
5133
5134 end do
5135
5136 end if
5137!
5138! Store the diagonal element of S and restore
5139! the corresponding diagonal element of R.
5140!
5141 sdiag(j) = r(j,j)
5142 r(j,j) = x(j)
5143
5144 end do
5145!
5146! Solve the triangular system for Z. If the system is
5147! singular, then obtain a least squares solution.
5148!
5149 nsing = n
5150
5151 do j = 1, n
5152
5153 if ( sdiag(j) == 0.0d+00 .and. nsing == n ) then
5154 nsing = j - 1
5155 end if
5156
5157 if ( nsing < n ) then
5158 wa(j) = 0.0d+00
5159 end if
5160
5161 end do
5162
5163 do j = nsing, 1, -1
5164 sum2 = dot_product( wa(j+1:nsing), r(j+1:nsing,j) )
5165 wa(j) = ( wa(j) - sum2 ) / sdiag(j)
5166 end do
5167!
5168! Permute the components of Z back to components of X.
5169!
5170 do j = 1, n
5171 l = ipvt(j)
5172 x(l) = wa(j)
5173 end do
5174
5175 return
5176end
5177subroutine r1mpyq ( m, n, a, lda, v, w )
5178
5179!*****************************************************************************80
5180!
5181!! R1MPYQ computes A*Q, where Q is the product of Householder transformations.
5182!
5183! Discussion:
5184!
5185! Given an M by N matrix A, this subroutine computes A*Q where
5186! Q is the product of 2*(N - 1) transformations
5187!
5188! GV(N-1)*...*GV(1)*GW(1)*...*GW(N-1)
5189!
5190! and GV(I), GW(I) are Givens rotations in the (I,N) plane which
5191! eliminate elements in the I-th and N-th planes, respectively.
5192! Q itself is not given, rather the information to recover the
5193! GV, GW rotations is supplied.
5194!
5195! Licensing:
5196!
5197! This code is distributed under the GNU LGPL license.
5198!
5199! Modified:
5200!
5201! 06 April 2010
5202!
5203! Author:
5204!
5205! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
5206! FORTRAN90 version by John Burkardt.
5207!
5208! Reference:
5209!
5210! Jorge More, Burton Garbow, Kenneth Hillstrom,
5211! User Guide for MINPACK-1,
5212! Technical Report ANL-80-74,
5213! Argonne National Laboratory, 1980.
5214!
5215! Parameters:
5216!
5217! Input, integer ( kind = 4 ) M, the number of rows of A.
5218!
5219! Input, integer ( kind = 4 ) N, the number of columns of A.
5220!
5221! Input/output, real ( kind = 8 ) A(LDA,N), the M by N array.
5222! On input, the matrix A to be postmultiplied by the orthogonal matrix Q.
5223! On output, the value of A*Q.
5224!
5225! Input, integer ( kind = 4 ) LDA, the leading dimension of A, which must not
5226! be less than M.
5227!
5228! Input, real ( kind = 8 ) V(N), W(N), contain the information necessary
5229! to recover the Givens rotations GV and GW.
5230!
5231 implicit none
5232
5233 integer ( kind = 4 ), intent(in) :: lda
5234 integer ( kind = 4 ), intent(in) :: m
5235 integer ( kind = 4 ), intent(in) :: n
5236
5237 real ( kind = 8 ), intent(inout) :: a(lda,n)
5238 real ( kind = 8 ) c
5239 integer ( kind = 4 ) i
5240 integer ( kind = 4 ) j
5241 real ( kind = 8 ) s
5242 real ( kind = 8 ) temp
5243 real ( kind = 8 ), intent(in) :: v(n)
5244 real ( kind = 8 ), intent(in) :: w(n)
5245!
5246! Apply the first set of Givens rotations to A.
5247!
5248 do j = n - 1, 1, -1
5249
5250 if ( 1.0d+00 < abs( v(j) ) ) then
5251 c = 1.0d+00 / v(j)
5252 s = sqrt( 1.0d+00 - c ** 2 )
5253 else
5254 s = v(j)
5255 c = sqrt( 1.0d+00 - s ** 2 )
5256 end if
5257
5258 do i = 1, m
5259 temp = c * a(i,j) - s * a(i,n)
5260 a(i,n) = s * a(i,j) + c * a(i,n)
5261 a(i,j) = temp
5262 end do
5263
5264 end do
5265!
5266! Apply the second set of Givens rotations to A.
5267!
5268 do j = 1, n - 1
5269
5270 if ( abs( w(j) ) > 1.0d+00 ) then
5271 c = 1.0d+00 / w(j)
5272 s = sqrt( 1.0d+00 - c ** 2 )
5273 else
5274 s = w(j)
5275 c = sqrt( 1.0d+00 - s ** 2 )
5276 end if
5277
5278 do i = 1, m
5279 temp = c * a(i,j) + s * a(i,n)
5280 a(i,n) = - s * a(i,j) + c * a(i,n)
5281 a(i,j) = temp
5282 end do
5283
5284 end do
5285
5286 return
5287end
5288subroutine r1updt ( m, n, s, ls, u, v, w, sing )
5289
5290!*****************************************************************************80
5291!
5292!! R1UPDT re-triangularizes a matrix after a rank one update.
5293!
5294! Discussion:
5295!
5296! Given an M by N lower trapezoidal matrix S, an M-vector U, and an
5297! N-vector V, the problem is to determine an orthogonal matrix Q such that
5298!
5299! (S + U * V' ) * Q
5300!
5301! is again lower trapezoidal.
5302!
5303! This subroutine determines Q as the product of 2 * (N - 1)
5304! transformations
5305!
5306! GV(N-1)*...*GV(1)*GW(1)*...*GW(N-1)
5307!
5308! where GV(I), GW(I) are Givens rotations in the (I,N) plane
5309! which eliminate elements in the I-th and N-th planes,
5310! respectively. Q itself is not accumulated, rather the
5311! information to recover the GV and GW rotations is returned.
5312!
5313! Licensing:
5314!
5315! This code is distributed under the GNU LGPL license.
5316!
5317! Modified:
5318!
5319! 06 April 2010
5320!
5321! Author:
5322!
5323! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
5324! FORTRAN90 version by John Burkardt.
5325!
5326! Reference:
5327!
5328! Jorge More, Burton Garbow, Kenneth Hillstrom,
5329! User Guide for MINPACK-1,
5330! Technical Report ANL-80-74,
5331! Argonne National Laboratory, 1980.
5332!
5333! Parameters:
5334!
5335! Input, integer ( kind = 4 ) M, the number of rows of S.
5336!
5337! Input, integer ( kind = 4 ) N, the number of columns of S.
5338! N must not exceed M.
5339!
5340! Input/output, real ( kind = 8 ) S(LS). On input, the lower trapezoidal
5341! matrix S stored by columns. On output S contains the lower trapezoidal
5342! matrix produced as described above.
5343!
5344! Input, integer ( kind = 4 ) LS, the length of the S array. LS must be at
5345! least (N*(2*M-N+1))/2.
5346!
5347! Input, real ( kind = 8 ) U(M), the U vector.
5348!
5349! Input/output, real ( kind = 8 ) V(N). On input, V must contain the
5350! vector V. On output V contains the information necessary to recover the
5351! Givens rotations GV described above.
5352!
5353! Output, real ( kind = 8 ) W(M), contains information necessary to
5354! recover the Givens rotations GW described above.
5355!
5356! Output, logical SING, is set to TRUE if any of the diagonal elements
5357! of the output S are zero. Otherwise SING is set FALSE.
5358!
5359 implicit none
5360
5361 integer ( kind = 4 ), intent(in) :: ls
5362 integer ( kind = 4 ), intent(in) :: m
5363 integer ( kind = 4 ), intent(in) :: n
5364
5365 real ( kind = 8 ) cos
5366 real ( kind = 8 ) cotan
5367 real ( kind = 8 ) giant
5368 integer ( kind = 4 ) i
5369 integer ( kind = 4 ) j
5370 integer ( kind = 4 ) jj
5371 integer ( kind = 4 ) l
5372 real ( kind = 8 ), intent(inout) :: s(ls)
5373 real ( kind = 8 ) sin
5374 logical, intent(out) :: sing
5375 real ( kind = 8 ) tan
5376 real ( kind = 8 ) tau
5377 real ( kind = 8 ) temp
5378 real ( kind = 8 ), intent(in) :: u(m)
5379 real ( kind = 8 ), intent(inout) :: v(n)
5380 real ( kind = 8 ), intent(out) :: w(m)
5381!
5382! GIANT is the largest magnitude.
5383!
5384 giant = huge( giant )
5385!
5386! Initialize the diagonal element pointer.
5387!
5388 jj = ( n * ( 2 * m - n + 1 ) ) / 2 - ( m - n )
5389!
5390! Move the nontrivial part of the last column of S into W.
5391!
5392 l = jj
5393 do i = n, m
5394 w(i) = s(l)
5395 l = l + 1
5396 end do
5397!
5398! Rotate the vector V into a multiple of the N-th unit vector
5399! in such a way that a spike is introduced into W.
5400!
5401 do j = n - 1, 1, -1
5402
5403 jj = jj - ( m - j + 1 )
5404 w(j) = 0.0d+00
5405
5406 if ( v(j) /= 0.0d+00 ) then
5407!
5408! Determine a Givens rotation which eliminates the
5409! J-th element of V.
5410!
5411 if ( abs( v(n) ) < abs( v(j) ) ) then
5412 cotan = v(n) / v(j)
5413 sin = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * cotan ** 2 )
5414 cos = sin * cotan
5415 tau = 1.0d+00
5416 if ( abs( cos ) * giant > 1.0d+00 ) then
5417 tau = 1.0d+00 / cos
5418 end if
5419 else
5420 tan = v(j) / v(n)
5421 cos = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * tan ** 2 )
5422 sin = cos * tan
5423 tau = sin
5424 end if
5425!
5426! Apply the transformation to V and store the information
5427! necessary to recover the Givens rotation.
5428!
5429 v(n) = sin * v(j) + cos * v(n)
5430 v(j) = tau
5431!
5432! Apply the transformation to S and extend the spike in W.
5433!
5434 l = jj
5435 do i = j, m
5436 temp = cos * s(l) - sin * w(i)
5437 w(i) = sin * s(l) + cos * w(i)
5438 s(l) = temp
5439 l = l + 1
5440 end do
5441
5442 end if
5443
5444 end do
5445!
5446! Add the spike from the rank 1 update to W.
5447!
5448 w(1:m) = w(1:m) + v(n) * u(1:m)
5449!
5450! Eliminate the spike.
5451!
5452 sing = .false.
5453
5454 do j = 1, n-1
5455
5456 if ( w(j) /= 0.0d+00 ) then
5457!
5458! Determine a Givens rotation which eliminates the
5459! J-th element of the spike.
5460!
5461 if ( abs( s(jj) ) < abs( w(j) ) ) then
5462
5463 cotan = s(jj) / w(j)
5464 sin = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * cotan ** 2 )
5465 cos = sin * cotan
5466
5467 if ( 1.0d+00 < abs( cos ) * giant ) then
5468 tau = 1.0d+00 / cos
5469 else
5470 tau = 1.0d+00
5471 end if
5472
5473 else
5474
5475 tan = w(j) / s(jj)
5476 cos = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * tan ** 2 )
5477 sin = cos * tan
5478 tau = sin
5479
5480 end if
5481!
5482! Apply the transformation to S and reduce the spike in W.
5483!
5484 l = jj
5485 do i = j, m
5486 temp = cos * s(l) + sin * w(i)
5487 w(i) = - sin * s(l) + cos * w(i)
5488 s(l) = temp
5489 l = l + 1
5490 end do
5491!
5492! Store the information necessary to recover the Givens rotation.
5493!
5494 w(j) = tau
5495
5496 end if
5497!
5498! Test for zero diagonal elements in the output S.
5499!
5500 if ( s(jj) == 0.0d+00 ) then
5501 sing = .true.
5502 end if
5503
5504 jj = jj + ( m - j + 1 )
5505
5506 end do
5507!
5508! Move W back into the last column of the output S.
5509!
5510 l = jj
5511 do i = n, m
5512 s(l) = w(i)
5513 l = l + 1
5514 end do
5515
5516 if ( s(jj) == 0.0d+00 ) then
5517 sing = .true.
5518 end if
5519
5520 return
5521end
5522subroutine r8vec_print ( n, a, title )
5523
5524!*****************************************************************************80
5525!
5526!! R8VEC_PRINT prints an R8VEC.
5527!
5528! Discussion:
5529!
5530! An R8VEC is a vector of R8's.
5531!
5532! Licensing:
5533!
5534! This code is distributed under the GNU LGPL license.
5535!
5536! Modified:
5537!
5538! 22 August 2000
5539!
5540! Author:
5541!
5542! John Burkardt
5543!
5544! Parameters:
5545!
5546! Input, integer ( kind = 4 ) N, the number of components of the vector.
5547!
5548! Input, real ( kind = 8 ) A(N), the vector to be printed.
5549!
5550! Input, character ( len = * ) TITLE, a title.
5551!
5552 implicit none
5553
5554 integer ( kind = 4 ), intent(in) :: n
5555
5556 real ( kind = 8 ), intent(in) :: a(n)
5557 integer ( kind = 4 ) i
5558 character ( len = * ), intent(in) :: title
5559
5560 write ( *, '(a)' ) ' '
5561 write ( *, '(a)' ) trim( title )
5562 write ( *, '(a)' ) ' '
5563 do i = 1, n
5564 write ( *, '(2x,i8,2x,g16.8)' ) i, a(i)
5565 end do
5566
5567 return
5568end
5569subroutine rwupdt ( n, r, ldr, w, b, alpha, c, s )
5570
5571!*****************************************************************************80
5572!
5573!! RWUPDT computes the decomposition of triangular matrix augmented by one row.
5574!
5575! Discussion:
5576!
5577! Given an N by N upper triangular matrix R, this subroutine
5578! computes the QR decomposition of the matrix formed when a row
5579! is added to R. If the row is specified by the vector W, then
5580! RWUPDT determines an orthogonal matrix Q such that when the
5581! N+1 by N matrix composed of R augmented by W is premultiplied
5582! by Q', the resulting matrix is upper trapezoidal.
5583! The matrix Q' is the product of N transformations
5584!
5585! G(N)*G(N-1)* ... *G(1)
5586!
5587! where G(I) is a Givens rotation in the (I,N+1) plane which eliminates
5588! elements in the (N+1)-st plane. RWUPDT also computes the product
5589! Q'*C where C is the (N+1)-vector (B,ALPHA). Q itself is not
5590! accumulated, rather the information to recover the G rotations is
5591! supplied.
5592!
5593! Licensing:
5594!
5595! This code is distributed under the GNU LGPL license.
5596!
5597! Modified:
5598!
5599! 06 April 2010
5600!
5601! Author:
5602!
5603! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
5604! FORTRAN90 version by John Burkardt.
5605!
5606! Reference:
5607!
5608! Jorge More, Burton Garbow, Kenneth Hillstrom,
5609! User Guide for MINPACK-1,
5610! Technical Report ANL-80-74,
5611! Argonne National Laboratory, 1980.
5612!
5613! Parameters:
5614!
5615! Input, integer ( kind = 4 ) N, the order of R.
5616!
5617! Input/output, real ( kind = 8 ) R(LDR,N). On input the upper triangular
5618! part of R must contain the matrix to be updated. On output R contains the
5619! updated triangular matrix.
5620!
5621! Input, integer ( kind = 4 ) LDR, the leading dimension of the array R.
5622! LDR must not be less than N.
5623!
5624! Input, real ( kind = 8 ) W(N), the row vector to be added to R.
5625!
5626! Input/output, real ( kind = 8 ) B(N). On input, the first N elements
5627! of the vector C. On output the first N elements of the vector Q'*C.
5628!
5629! Input/output, real ( kind = 8 ) ALPHA. On input, the (N+1)-st element
5630! of the vector C. On output the (N+1)-st element of the vector Q'*C.
5631!
5632! Output, real ( kind = 8 ) C(N), S(N), the cosines and sines of the
5633! transforming Givens rotations.
5634!
5635 implicit none
5636
5637 integer ( kind = 4 ), intent(in) :: ldr
5638 integer ( kind = 4 ), intent(in) :: n
5639
5640 real ( kind = 8 ), intent(inout) :: alpha
5641 real ( kind = 8 ), intent(inout) :: b(n)
5642 real ( kind = 8 ), intent(out) :: c(n)
5643 real ( kind = 8 ) cotan
5644 integer ( kind = 4 ) i
5645 integer ( kind = 4 ) j
5646 real ( kind = 8 ), intent(inout) :: r(ldr,n)
5647 real ( kind = 8 ) rowj
5648 real ( kind = 8 ), intent(out) :: s(n)
5649 real ( kind = 8 ) tan
5650 real ( kind = 8 ) temp
5651 real ( kind = 8 ), intent(in) :: w(n)
5652
5653 do j = 1, n
5654
5655 rowj = w(j)
5656!
5657! Apply the previous transformations to R(I,J), I=1,2,...,J-1, and to W(J).
5658!
5659 do i = 1, j - 1
5660 temp = c(i) * r(i,j) + s(i) * rowj
5661 rowj = - s(i) * r(i,j) + c(i) * rowj
5662 r(i,j) = temp
5663 end do
5664!
5665! Determine a Givens rotation which eliminates W(J).
5666!
5667 c(j) = 1.0d+00
5668 s(j) = 0.0d+00
5669
5670 if ( rowj /= 0.0d+00 ) then
5671
5672 if ( abs( r(j,j) ) < abs( rowj ) ) then
5673 cotan = r(j,j) / rowj
5674 s(j) = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * cotan ** 2 )
5675 c(j) = s(j) * cotan
5676 else
5677 tan = rowj / r(j,j)
5678 c(j) = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * tan ** 2 )
5679 s(j) = c(j) * tan
5680 end if
5681!
5682! Apply the current transformation to R(J,J), B(J), and ALPHA.
5683!
5684 r(j,j) = c(j) * r(j,j) + s(j) * rowj
5685 temp = c(j) * b(j) + s(j) * alpha
5686 alpha = - s(j) * b(j) + c(j) * alpha
5687 b(j) = temp
5688
5689 end if
5690
5691 end do
5692
5693 return
5694end
5695subroutine timestamp ( )
5696
5697!*****************************************************************************80
5698!
5699!! TIMESTAMP prints the current YMDHMS date as a time stamp.
5700!
5701! Example:
5702!
5703! 31 May 2001 9:45:54.872 AM
5704!
5705! Licensing:
5706!
5707! This code is distributed under the GNU LGPL license.
5708!
5709! Modified:
5710!
5711! 18 May 2013
5712!
5713! Author:
5714!
5715! John Burkardt
5716!
5717! Parameters:
5718!
5719! None
5720!
5721 implicit none
5722
5723 character ( len = 8 ) ampm
5724 integer ( kind = 4 ) d
5725 integer ( kind = 4 ) h
5726 integer ( kind = 4 ) m
5727 integer ( kind = 4 ) mm
5728 character ( len = 9 ), parameter, dimension(12) :: month = (/ &
5729 'January ', 'February ', 'March ', 'April ', &
5730 'May ', 'June ', 'July ', 'August ', &
5731 'September', 'October ', 'November ', 'December ' /)
5732 integer ( kind = 4 ) n
5733 integer ( kind = 4 ) s
5734 integer ( kind = 4 ) values(8)
5735 integer ( kind = 4 ) y
5736
5737 call date_and_time ( values = values )
5738
5739 y = values(1)
5740 m = values(2)
5741 d = values(3)
5742 h = values(5)
5743 n = values(6)
5744 s = values(7)
5745 mm = values(8)
5746
5747 if ( h < 12 ) then
5748 ampm = 'AM'
5749 else if ( h == 12 ) then
5750 if ( n == 0 .and. s == 0 ) then
5751 ampm = 'Noon'
5752 else
5753 ampm = 'PM'
5754 end if
5755 else
5756 h = h - 12
5757 if ( h < 12 ) then
5758 ampm = 'PM'
5759 else if ( h == 12 ) then
5760 if ( n == 0 .and. s == 0 ) then
5761 ampm = 'Midnight'
5762 else
5763 ampm = 'AM'
5764 end if
5765 end if
5766 end if
5767
5768 write ( *, '(i2.2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) &
5769 d, trim( month(m) ), y, h, ':', n, ':', s, '.', mm, trim( ampm )
5770
5771 return
5772end
subroutine chkder(m, n, x, fvec, fjac, ldfjac, xp, fvecp, mode, err)
Definition minpack.F90:54
subroutine qrsolv(n, r, ldr, ipvt, diag, qtb, x, sdiag)
Definition minpack.F90:4960
subroutine qform(m, n, q, ldq)
Definition minpack.F90:4666
subroutine lmstr(fcn, m, n, x, fvec, fjac, ldfjac, ftol, xtol, gtol, maxfev, diag, mode, factor, nprint, info, nfev, njev, ipvt, qtf)
Definition minpack.F90:3947
subroutine lmdif1(fcn, m, n, x, fvec, tol, info)
Definition minpack.F90:3505
subroutine lmpar(n, r, ldr, ipvt, diag, qtb, delta, par, x, sdiag)
Definition minpack.F90:3644
subroutine hybrd(fcn, n, x, fvec, xtol, maxfev, ml, mu, epsfcn, diag, mode, factor, nprint, info, nfev, fjac, ldfjac, r, lr, qtf)
Definition minpack.F90:911
subroutine lmder(fcn, m, n, x, fvec, fjac, ldfjac, ftol, xtol, gtol, maxfev, diag, mode, factor, nprint, info, nfev, njev, ipvt, qtf)
Definition minpack.F90:2296
subroutine hybrd1(fcn, n, x, fvec, tol, info)
Definition minpack.F90:1458
subroutine fdjac1(fcn, n, x, fvec, fjac, ldfjac, iflag, ml, mu, epsfcn)
Definition minpack.F90:613
subroutine timestamp()
Definition minpack.F90:5696
subroutine hybrj(fcn, n, x, fvec, fjac, ldfjac, xtol, maxfev, diag, mode, factor, nprint, info, nfev, njev, r, lr, qtf)
Definition minpack.F90:1593
subroutine qrfac(m, n, a, lda, pivot, ipvt, lipvt, rdiag, acnorm)
Definition minpack.F90:4767
real(kind=8) function enorm(n, x)
Definition minpack.F90:434
real(kind=8) function enorm2(n, x)
Definition minpack.F90:483
subroutine r1updt(m, n, s, ls, u, v, w, sing)
Definition minpack.F90:5289
subroutine fdjac2(fcn, m, n, x, fvec, fjac, ldfjac, iflag, epsfcn)
Definition minpack.F90:790
subroutine lmdif(fcn, m, n, x, fvec, ftol, xtol, gtol, maxfev, epsfcn, diag, mode, factor, nprint, info, nfev, fjac, ldfjac, ipvt, qtf)
Definition minpack.F90:2975
subroutine lmder1(fcn, m, n, x, fvec, fjac, ldfjac, tol, info)
Definition minpack.F90:2822
subroutine dogleg(n, r, lr, diag, qtb, delta, x)
Definition minpack.F90:224
subroutine r1mpyq(m, n, a, lda, v, w)
Definition minpack.F90:5178
subroutine lmstr1(fcn, m, n, x, fvec, fjac, ldfjac, tol, info)
Definition minpack.F90:4495
subroutine rwupdt(n, r, ldr, w, b, alpha, c, s)
Definition minpack.F90:5570
subroutine r8vec_print(n, a, title)
Definition minpack.F90:5523
subroutine hybrj1(fcn, n, x, fvec, fjac, ldfjac, tol, info)
Definition minpack.F90:2161