-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlm.f90
2019 lines (1427 loc) · 63.9 KB
/
lm.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module lm
use precision, only : fdp
! Levenberg_Marquardt MINPACK routines which are used by both LMDIF & LMDER
! under BSD-like license
!
implicit none
integer, parameter :: lmdp = fdp
private
public :: lmdp, lmdif1, lmdif, lmder1, lmder, enorm
public :: decompactify, compactify
contains
function decompactify(p,pmin,pmax,ndim)
implicit none
integer, intent(in) :: ndim
real(fdp), dimension(ndim) :: decompactify
real(fdp), dimension(ndim), intent(in) :: p,pmin,pmax
real(fdp), dimension(ndim) :: t
integer :: i
if (any(p.gt.pmax).or.any(p.lt.pmin)) then
stop 'decompactify: parameter out of bounds!'
endif
t = (p - 0.5_fdp*(pmin+pmax)) / (0.5_fdp*(pmax-pmin))
if (any(abs(t).ge.1._fdp)) stop 'decompactify: pb!'
do i=1,ndim
if (t(i).eq.1._fdp) t(i)=1._fdp-epsilon(1._fdp)
if (t(i).eq.-1._fdp) t(i) = -1._fdp + epsilon(1._fdp)
enddo
decompactify = t/(1._fdp-abs(t))
end function decompactify
function compactify(pinf,pmin,pmax,ndim)
implicit none
integer, intent(in) :: ndim
real(fdp), dimension(ndim) :: compactify
real(fdp), dimension(ndim),intent(in) :: pinf,pmin,pmax
real(fdp), dimension(ndim) :: t
t = pinf/(1._fdp+abs(pinf))
if (any(abs(t).ge.1._fdp)) stop 'compactify pb!'
compactify = 0.5_fdp*(pmin+pmax) + 0.5_fdp*(pmax-pmin)*t
end function compactify
SUBROUTINE lmdif1(fcn1, m, n, x, fvec, tol, info, iwa)
INTEGER, INTENT(IN) :: m
INTEGER, INTENT(IN) :: n
REAL (lmdp), INTENT(IN OUT) :: x(:)
REAL (lmdp), INTENT(OUT) :: fvec(:)
REAL (lmdp), INTENT(IN) :: tol
INTEGER, INTENT(OUT) :: info
INTEGER, INTENT(OUT) :: iwa(:)
include 'lm.h'
! **********
! subroutine lmdif1
! The purpose of lmdif1 is to minimize the sum of the squares of m nonlinear
! functions in n variables by a modification of the Levenberg-Marquardt
! algorithm. This is done by using the more general least-squares
! solver lmdif. The user must provide a subroutine which calculates the
! functions. The jacobian is then calculated by a forward-difference
! approximation.
! the subroutine statement is
! subroutine lmdif1(fcn, m, n, x, fvec, tol, info, iwa)
! where
! fcn is the name of the user-supplied subroutine which calculates
! the functions. fcn must be declared in an external statement in the
! user calling program, and should be written as follows.
! subroutine fcn(m, n, x, fvec, iflag)
! integer m, n, iflag
! REAL (lmdp) x(n), fvec(m)
! ----------
! calculate the functions at x and return this vector in fvec.
! ----------
! return
! end
! the value of iflag should not be changed by fcn unless
! the user wants to terminate execution of lmdif1.
! In this case set iflag to a negative integer.
! m is a positive integer input variable set to the number of functions.
! n is a positive integer input variable set to the number of variables.
! n must not exceed m.
! x is an array of length n. On input x must contain an initial estimate
! of the solution vector. On output x contains the final estimate of
! the solution vector.
! fvec is an output array of length m which contains
! the functions evaluated at the output x.
! tol is a nonnegative input variable. Termination occurs when the
! algorithm estimates either that the relative error in the sum of
! squares is at most tol or that the relative error between x and the
! solution is at most tol.
! info is an integer output variable. If the user has terminated execution,
! info is set to the (negative) value of iflag. See description of fcn.
! Otherwise, info is set as follows.
! info = 0 improper input parameters.
! info = 1 algorithm estimates that the relative error
! in the sum of squares is at most tol.
! info = 2 algorithm estimates that the relative error
! between x and the solution is at most tol.
! info = 3 conditions for info = 1 and info = 2 both hold.
! info = 4 fvec is orthogonal to the columns of the
! jacobian to machine precision.
! info = 5 number of calls to fcn has reached or exceeded 200*(n+1).
! info = 6 tol is too small. no further reduction in
! the sum of squares is possible.
! info = 7 tol is too small. No further improvement in
! the approximate solution x is possible.
! iwa is an integer work array of length n.
! wa is a work array of length lwa.
! lwa is a positive integer input variable not less than m*n+5*n+m.
! subprograms called
! user-supplied ...... fcn
! minpack-supplied ... lmdif
! argonne national laboratory. minpack project. march 1980.
! burton s. garbow, kenneth e. hillstrom, jorge j. more
! **********
INTEGER :: maxfev, mode, nfev, nprint
REAL (lmdp) :: epsfcn, ftol, gtol, xtol, fjac(m,n)
REAL (lmdp), PARAMETER :: factor = 100._lmdp, zero = 0.0_lmdp
info = 0
! check the input parameters for errors.
IF (n <= 0 .OR. m < n .OR. tol < zero) GO TO 10
! call lmdif.
maxfev = 300*(n + 1)
ftol = tol
xtol = tol
gtol = zero
epsfcn = zero
mode = 1
nprint = 0
CALL lmdif(fcn1, m, n, x, fvec, ftol, xtol, gtol, maxfev, epsfcn, &
mode, factor, nprint, info, nfev, fjac, iwa)
IF (info == 8) info = 4
10 RETURN
! last card of subroutine lmdif1.
END SUBROUTINE lmdif1
SUBROUTINE lmdif(fcn1, m, n, x, fvec, ftol, xtol, gtol, maxfev, epsfcn, &
mode, factor, nprint, info, nfev, fjac, ipvt)
INTEGER, INTENT(IN) :: m
INTEGER, INTENT(IN) :: n
REAL (lmdp), INTENT(IN OUT) :: x(:)
REAL (lmdp), INTENT(OUT) :: fvec(:)
REAL (lmdp), INTENT(IN) :: ftol
REAL (lmdp), INTENT(IN) :: xtol
REAL (lmdp), INTENT(IN OUT) :: gtol
INTEGER, INTENT(IN OUT) :: maxfev
REAL (lmdp), INTENT(IN OUT) :: epsfcn
INTEGER, INTENT(IN) :: mode
REAL (lmdp), INTENT(IN) :: factor
INTEGER, INTENT(IN) :: nprint
INTEGER, INTENT(OUT) :: info
INTEGER, INTENT(OUT) :: nfev
REAL (lmdp), INTENT(OUT) :: fjac(:,:) ! fjac(ldfjac,n)
INTEGER, INTENT(OUT) :: ipvt(:)
include 'lm.h'
! **********
! subroutine lmdif
! The purpose of lmdif is to minimize the sum of the squares of m nonlinear
! functions in n variables by a modification of the Levenberg-Marquardt
! algorithm. The user must provide a subroutine which calculates the
! functions. The jacobian is then calculated by a forward-difference
! approximation.
! the subroutine statement is
! subroutine lmdif(fcn, m, n, x, fvec, ftol, xtol, gtol, maxfev, epsfcn,
! diag, mode, factor, nprint, info, nfev, fjac,
! ldfjac, ipvt, qtf, wa1, wa2, wa3, wa4)
! N.B. 7 of these arguments have been removed in this version.
! where
! fcn is the name of the user-supplied subroutine which calculates the
! functions. fcn must be declared in an external statement in the user
! calling program, and should be written as follows.
! subroutine fcn(m, n, x, fvec, iflag)
! integer m, n, iflag
! REAL (lmdp) x(:), fvec(m)
! ----------
! calculate the functions at x and return this vector in fvec.
! ----------
! return
! end
! the value of iflag should not be changed by fcn unless
! the user wants to terminate execution of lmdif.
! in this case set iflag to a negative integer.
! m is a positive integer input variable set to the number of functions.
! n is a positive integer input variable set to the number of variables.
! n must not exceed m.
! x is an array of length n. On input x must contain an initial estimate
! of the solution vector. On output x contains the final estimate of the
! solution vector.
! fvec is an output array of length m which contains
! the functions evaluated at the output x.
! ftol is a nonnegative input variable. Termination occurs when both the
! actual and predicted relative reductions in the sum of squares are at
! most ftol. Therefore, ftol measures the relative error desired
! in the sum of squares.
! xtol is a nonnegative input variable. Termination occurs when the
! relative error between two consecutive iterates is at most xtol.
! Therefore, xtol measures the relative error desired in the approximate
! solution.
! gtol is a nonnegative input variable. Termination occurs when the cosine
! of the angle between fvec and any column of the jacobian is at most
! gtol in absolute value. Therefore, gtol measures the orthogonality
! desired between the function vector and the columns of the jacobian.
! maxfev is a positive integer input variable. Termination occurs when the
! number of calls to fcn is at least maxfev by the end of an iteration.
! epsfcn is an input variable used in determining a suitable step length
! for the forward-difference approximation. This approximation assumes
! that the relative errors in the functions are of the order of epsfcn.
! If epsfcn is less than the machine precision, it is assumed that the
! relative errors in the functions are of the order of the machine
! precision.
! diag is an array of length n. If mode = 1 (see below), diag is
! internally set. If mode = 2, diag must contain positive entries that
! serve as multiplicative scale factors for the variables.
! mode is an integer input variable. If mode = 1, the variables will be
! scaled internally. If mode = 2, the scaling is specified by the input
! diag. other values of mode are equivalent to mode = 1.
! factor is a positive input variable used in determining the initial step
! bound. This bound is set to the product of factor and the euclidean
! norm of diag*x if nonzero, or else to factor itself. In most cases
! factor should lie in the interval (.1,100.). 100. is a generally
! recommended value.
! nprint is an integer input variable that enables controlled printing of
! iterates if it is positive. In this case, fcn is called with iflag = 0
! at the beginning of the first iteration and every nprint iterations
! thereafter and immediately prior to return, with x and fvec available
! for printing. If nprint is not positive, no special calls
! of fcn with iflag = 0 are made.
! info is an integer output variable. If the user has terminated
! execution, info is set to the (negative) value of iflag.
! See description of fcn. Otherwise, info is set as follows.
! info = 0 improper input parameters.
! info = 1 both actual and predicted relative reductions
! in the sum of squares are at most ftol.
! info = 2 relative error between two consecutive iterates <= xtol.
! info = 3 conditions for info = 1 and info = 2 both hold.
! info = 4 the cosine of the angle between fvec and any column of
! the Jacobian is at most gtol in absolute value.
! info = 5 number of calls to fcn has reached or exceeded maxfev.
! info = 6 ftol is too small. no further reduction in
! the sum of squares is possible.
! info = 7 xtol is too small. no further improvement in
! the approximate solution x is possible.
! info = 8 gtol is too small. fvec is orthogonal to the
! columns of the jacobian to machine precision.
! nfev is an integer output variable set to the number of calls to fcn.
! fjac is an output m by n array. the upper n by n submatrix
! of fjac contains an upper triangular matrix r with
! diagonal elements of nonincreasing magnitude such that
! t t t
! p *(jac *jac)*p = r *r,
! where p is a permutation matrix and jac is the final calculated
! Jacobian. Column j of p is column ipvt(j) (see below) of the
! identity matrix. the lower trapezoidal part of fjac contains
! information generated during the computation of r.
! ldfjac is a positive integer input variable not less than m
! which specifies the leading dimension of the array fjac.
! ipvt is an integer output array of length n. ipvt defines a permutation
! matrix p such that jac*p = q*r, where jac is the final calculated
! jacobian, q is orthogonal (not stored), and r is upper triangular
! with diagonal elements of nonincreasing magnitude.
! Column j of p is column ipvt(j) of the identity matrix.
! qtf is an output array of length n which contains
! the first n elements of the vector (q transpose)*fvec.
! wa1, wa2, and wa3 are work arrays of length n.
! wa4 is a work array of length m.
! subprograms called
! user-supplied ...... fcn
! minpack-supplied ... dpmpar,enorm,fdjac2,lmpar,qrfac
! fortran-supplied ... dabs,dmax1,dmin1,dsqrt,mod
! argonne national laboratory. minpack project. march 1980.
! burton s. garbow, kenneth e. hillstrom, jorge j. more
! **********
INTEGER :: i, iflag, iter, j, l
REAL (lmdp) :: actred, delta, dirder, epsmch, fnorm, fnorm1, gnorm, &
par, pnorm, prered, ratio, sum, temp, temp1, temp2, xnorm
REAL (lmdp) :: diag(n), qtf(n), wa1(n), wa2(n), wa3(n), wa4(m)
REAL (lmdp), PARAMETER :: one = 1.0_lmdp, p1 = 0.1_lmdp, p5 = 0.5_lmdp, &
p25 = 0.25_lmdp, p75 = 0.75_lmdp, p0001 = 0.0001_lmdp, &
zero = 0.0_lmdp
! epsmch is the machine precision.
epsmch = EPSILON(zero)
info = 0
iflag = 0
nfev = 0
! check the input parameters for errors.
IF (n <= 0 .OR. m < n .OR. ftol < zero .OR. xtol < zero .OR. gtol < zero &
.OR. maxfev <= 0 .OR. factor <= zero) GO TO 300
IF (mode /= 2) GO TO 20
DO j = 1, n
IF (diag(j) <= zero) GO TO 300
END DO
! evaluate the function at the starting point and calculate its norm.
20 iflag = 1
CALL fcn1(m, n, x, fvec, iflag)
nfev = 1
IF (iflag < 0) GO TO 300
fnorm = enorm(m, fvec)
! initialize levenberg-marquardt parameter and iteration counter.
par = zero
iter = 1
! beginning of the outer loop.
! calculate the jacobian matrix.
30 iflag = 2
CALL fdjac2(fcn1, m, n, x, fvec, fjac, iflag, epsfcn)
nfev = nfev + n
IF (iflag < 0) GO TO 300
! If requested, call fcn to enable printing of iterates.
IF (nprint <= 0) GO TO 40
iflag = 0
IF (MOD(iter-1,nprint) == 0) CALL fcn1(m, n, x, fvec, iflag)
IF (iflag < 0) GO TO 300
! Compute the qr factorization of the jacobian.
40 CALL qrfac(m, n, fjac, .true., ipvt, wa1, wa2)
! On the first iteration and if mode is 1, scale according
! to the norms of the columns of the initial jacobian.
IF (iter /= 1) GO TO 80
IF (mode == 2) GO TO 60
DO j = 1, n
diag(j) = wa2(j)
IF (wa2(j) == zero) diag(j) = one
END DO
! On the first iteration, calculate the norm of the scaled x
! and initialize the step bound delta.
60 wa3(1:n) = diag(1:n)*x(1:n)
xnorm = enorm(n, wa3)
delta = factor*xnorm
IF (delta == zero) delta = factor
! Form (q transpose)*fvec and store the first n components in qtf.
80 wa4(1:m) = fvec(1:m)
DO j = 1, n
IF (fjac(j,j) == zero) GO TO 120
sum = DOT_PRODUCT( fjac(j:m,j), wa4(j:m) )
temp = -sum/fjac(j,j)
DO i = j, m
wa4(i) = wa4(i) + fjac(i,j)*temp
END DO
120 fjac(j,j) = wa1(j)
qtf(j) = wa4(j)
END DO
! compute the norm of the scaled gradient.
gnorm = zero
IF (fnorm == zero) GO TO 170
DO j = 1, n
l = ipvt(j)
IF (wa2(l) == zero) CYCLE
sum = zero
DO i = 1, j
sum = sum + fjac(i,j)*(qtf(i)/fnorm)
END DO
gnorm = MAX(gnorm, ABS(sum/wa2(l)))
END DO
! test for convergence of the gradient norm.
170 IF (gnorm <= gtol) info = 4
IF (info /= 0) GO TO 300
! rescale if necessary.
IF (mode == 2) GO TO 200
DO j = 1, n
diag(j) = MAX(diag(j), wa2(j))
END DO
! beginning of the inner loop.
! determine the Levenberg-Marquardt parameter.
200 CALL lmpar(n, fjac, ipvt, diag, qtf, delta, par, wa1, wa2)
! store the direction p and x + p. calculate the norm of p.
DO j = 1, n
wa1(j) = -wa1(j)
wa2(j) = x(j) + wa1(j)
wa3(j) = diag(j)*wa1(j)
END DO
pnorm = enorm(n, wa3)
! on the first iteration, adjust the initial step bound.
IF (iter == 1) delta = MIN(delta, pnorm)
! evaluate the function at x + p and calculate its norm.
iflag = 1
CALL fcn1(m, n, wa2, wa4, iflag)
nfev = nfev + 1
IF (iflag < 0) GO TO 300
fnorm1 = enorm(m, wa4)
! compute the scaled actual reduction.
actred = -one
IF (p1*fnorm1 < fnorm) actred = one - (fnorm1/fnorm)**2
! Compute the scaled predicted reduction and
! the scaled directional derivative.
DO j = 1, n
wa3(j) = zero
l = ipvt(j)
temp = wa1(l)
DO i = 1, j
wa3(i) = wa3(i) + fjac(i,j)*temp
END DO
END DO
temp1 = enorm(n,wa3)/fnorm
temp2 = (SQRT(par)*pnorm)/fnorm
prered = temp1**2 + temp2**2/p5
dirder = -(temp1**2 + temp2**2)
! compute the ratio of the actual to the predicted reduction.
ratio = zero
IF (prered /= zero) ratio = actred/prered
! update the step bound.
IF (ratio <= p25) THEN
IF (actred >= zero) temp = p5
IF (actred < zero) temp = p5*dirder/(dirder + p5*actred)
IF (p1*fnorm1 >= fnorm .OR. temp < p1) temp = p1
delta = temp*MIN(delta,pnorm/p1)
par = par/temp
ELSE
IF (par /= zero .AND. ratio < p75) GO TO 260
delta = pnorm/p5
par = p5*par
END IF
! test for successful iteration.
260 IF (ratio < p0001) GO TO 290
! successful iteration. update x, fvec, and their norms.
DO j = 1, n
x(j) = wa2(j)
wa2(j) = diag(j)*x(j)
END DO
fvec(1:m) = wa4(1:m)
xnorm = enorm(n, wa2)
fnorm = fnorm1
iter = iter + 1
! tests for convergence.
290 IF (ABS(actred) <= ftol .AND. prered <= ftol .AND. p5*ratio <= one) info = 1
IF (delta <= xtol*xnorm) info = 2
IF (ABS(actred) <= ftol .AND. prered <= ftol &
.AND. p5*ratio <= one .AND. info == 2) info = 3
IF (info /= 0) GO TO 300
! tests for termination and stringent tolerances.
IF (nfev >= maxfev) info = 5
IF (ABS(actred) <= epsmch .AND. prered <= epsmch &
.AND. p5*ratio <= one) info = 6
IF (delta <= epsmch*xnorm) info = 7
IF (gnorm <= epsmch) info = 8
IF (info /= 0) GO TO 300
! end of the inner loop. repeat if iteration unsuccessful.
IF (ratio < p0001) GO TO 200
! end of the outer loop.
GO TO 30
! termination, either normal or user imposed.
300 IF (iflag < 0) info = iflag
iflag = 0
IF (nprint > 0) CALL fcn1(m, n, x, fvec, iflag)
RETURN
! last card of subroutine lmdif.
END SUBROUTINE lmdif
SUBROUTINE lmder1(fcn2, m, n, x, fvec, fjac, tol, info, ipvt)
INTEGER, INTENT(IN) :: m
INTEGER, INTENT(IN) :: n
REAL (lmdp), INTENT(IN OUT) :: x(:)
REAL (lmdp), INTENT(OUT) :: fvec(:)
REAL (lmdp), INTENT(IN OUT) :: fjac(:,:) ! fjac(ldfjac,n)
REAL (lmdp), INTENT(IN) :: tol
INTEGER, INTENT(OUT) :: info
INTEGER, INTENT(IN OUT) :: ipvt(:)
include 'lm.h'
! **********
! subroutine lmder1
! The purpose of lmder1 is to minimize the sum of the squares of
! m nonlinear functions in n variables by a modification of the
! levenberg-marquardt algorithm. This is done by using the more
! general least-squares solver lmder. The user must provide a
! subroutine which calculates the functions and the jacobian.
! the subroutine statement is
! subroutine lmder1(fcn, m, n, x, fvec, fjac, tol, info, ipvt)
! where
! fcn is the name of the user-supplied subroutine which
! calculates the functions and the jacobian. fcn must
! be declared in an interface statement in the user
! calling program, and should be written as follows.
! subroutine fcn(m, n, x, fvec, fjac, iflag)
! integer :: m, n, ldfjac, iflag
! REAL (lmdp) :: x(:), fvec(:), fjac(:,:)
! ----------
! if iflag = 1 calculate the functions at x and
! return this vector in fvec. do not alter fjac.
! if iflag = 2 calculate the jacobian at x and
! return this matrix in fjac. do not alter fvec.
! ----------
! return
! end
! the value of iflag should not be changed by fcn unless
! the user wants to terminate execution of lmder1.
! in this case set iflag to a negative integer.
! m is a positive integer input variable set to the number of functions.
! n is a positive integer input variable set to the number
! of variables. n must not exceed m.
! x is an array of length n. on input x must contain
! an initial estimate of the solution vector. on output x
! contains the final estimate of the solution vector.
! fvec is an output array of length m which contains
! the functions evaluated at the output x.
! fjac is an output m by n array. the upper n by n submatrix
! of fjac contains an upper triangular matrix r with
! diagonal elements of nonincreasing magnitude such that
! t t t
! p *(jac *jac)*p = r *r,
! where p is a permutation matrix and jac is the final calculated
! Jacobian. Column j of p is column ipvt(j) (see below) of the
! identity matrix. The lower trapezoidal part of fjac contains
! information generated during the computation of r.
! ldfjac is a positive integer input variable not less than m
! which specifies the leading dimension of the array fjac.
! tol is a nonnegative input variable. termination occurs
! when the algorithm estimates either that the relative
! error in the sum of squares is at most tol or that
! the relative error between x and the solution is at most tol.
! info is an integer output variable. If the user has terminated
! execution, info is set to the (negative) value of iflag.
! See description of fcn. Otherwise, info is set as follows.
! info = 0 improper input parameters.
! info = 1 algorithm estimates that the relative error
! in the sum of squares is at most tol.
! info = 2 algorithm estimates that the relative error
! between x and the solution is at most tol.
! info = 3 conditions for info = 1 and info = 2 both hold.
! info = 4 fvec is orthogonal to the columns of the
! jacobian to machine precision.
! info = 5 number of calls to fcn with iflag = 1 has reached 100*(n+1).
! info = 6 tol is too small. No further reduction in
! the sum of squares is possible.
! info = 7 tol is too small. No further improvement in
! the approximate solution x is possible.
! ipvt is an integer output array of length n. ipvt
! defines a permutation matrix p such that jac*p = q*r,
! where jac is the final calculated jacobian, q is
! orthogonal (not stored), and r is upper triangular
! with diagonal elements of nonincreasing magnitude.
! column j of p is column ipvt(j) of the identity matrix.
! wa is a work array of length lwa.
! lwa is a positive integer input variable not less than 5*n+m.
! subprograms called
! user-supplied ...... fcn
! minpack-supplied ... lmder
! argonne national laboratory. minpack project. march 1980.
! burton s. garbow, kenneth e. hillstrom, jorge j. more
! **********
INTEGER :: maxfev, mode, nfev, njev, nprint
REAL (lmdp) :: ftol, gtol, xtol
REAL (lmdp), PARAMETER :: factor = 100._lmdp, zero = 0.0_lmdp
info = 0
! check the input parameters for errors.
IF ( n <= 0 .OR. m < n .OR. tol < zero ) GO TO 10
! call lmder.
maxfev = 100*(n + 1)
ftol = tol
xtol = tol
gtol = zero
mode = 1
nprint = 0
CALL lmder(fcn2, m, n, x, fvec, fjac, ftol, xtol, gtol, maxfev, &
mode, factor, nprint, info, nfev, njev, ipvt)
IF (info == 8) info = 4
10 RETURN
! last card of subroutine lmder1.
END SUBROUTINE lmder1
SUBROUTINE lmder(fcn2, m, n, x, fvec, fjac, ftol, xtol, gtol, maxfev, &
mode, factor, nprint, info, nfev, njev, ipvt)
INTEGER, INTENT(IN) :: m
INTEGER, INTENT(IN) :: n
REAL (lmdp), INTENT(IN OUT) :: x(:)
REAL (lmdp), INTENT(OUT) :: fvec(m)
REAL (lmdp), INTENT(OUT) :: fjac(:,:) ! fjac(ldfjac,n)
REAL (lmdp), INTENT(IN) :: ftol
REAL (lmdp), INTENT(IN) :: xtol
REAL (lmdp), INTENT(IN OUT) :: gtol
INTEGER, INTENT(IN OUT) :: maxfev
INTEGER, INTENT(IN) :: mode
REAL (lmdp), INTENT(IN) :: factor
INTEGER, INTENT(IN) :: nprint
INTEGER, INTENT(OUT) :: info
INTEGER, INTENT(OUT) :: nfev
INTEGER, INTENT(OUT) :: njev
INTEGER, INTENT(OUT) :: ipvt(:)
include 'lm.h'
! **********
! subroutine lmder
! the purpose of lmder is to minimize the sum of the squares of
! m nonlinear functions in n variables by a modification of
! the levenberg-marquardt algorithm. the user must provide a
! subroutine which calculates the functions and the jacobian.
! the subroutine statement is
! subroutine lmder(fcn,m,n,x,fvec,fjac,ldfjac,ftol,xtol,gtol,
! maxfev,diag,mode,factor,nprint,info,nfev,
! njev,ipvt,qtf,wa1,wa2,wa3,wa4)
! where
! fcn is the name of the user-supplied subroutine which
! calculates the functions and the jacobian. fcn must
! be declared in an external statement in the user
! calling program, and should be written as follows.
! subroutine fcn(m,n,x,fvec,fjac,ldfjac,iflag)
! integer m,n,ldfjac,iflag
! REAL (lmdp) x(:),fvec(m),fjac(ldfjac,n)
! ----------
! if iflag = 1 calculate the functions at x and
! return this vector in fvec. do not alter fjac.
! if iflag = 2 calculate the jacobian at x and
! return this matrix in fjac. Do not alter fvec.
! ----------
! return
! end
! the value of iflag should not be changed by fcn unless
! the user wants to terminate execution of lmder.
! in this case set iflag to a negative integer.
! m is a positive integer input variable set to the number
! of functions.
! n is a positive integer input variable set to the number
! of variables. n must not exceed m.
! x is an array of length n. on input x must contain
! an initial estimate of the solution vector. on output x
! contains the final estimate of the solution vector.
! fvec is an output array of length m which contains
! the functions evaluated at the output x.
! fjac is an output m by n array. the upper n by n submatrix
! of fjac contains an upper triangular matrix r with
! diagonal elements of nonincreasing magnitude such that
! t t t
! p *(jac *jac)*p = r *r
! where p is a permutation matrix and jac is the final calculated
! jacobian. Column j of p is column ipvt(j) (see below) of the
! identity matrix. The lower trapezoidal part of fjac contains
! information generated during the computation of r.
! ldfjac is a positive integer input variable not less than m
! which specifies the leading dimension of the array fjac.
! ftol is a nonnegative input variable. Termination occurs when both
! the actual and predicted relative reductions in the sum of squares
! are at most ftol. Therefore, ftol measures the relative error
! desired in the sum of squares.
! xtol is a nonnegative input variable. termination
! occurs when the relative error between two consecutive
! iterates is at most xtol. therefore, xtol measures the
! relative error desired in the approximate solution.
! gtol is a nonnegative input variable. Termination occurs when the
! cosine of the angle between fvec and any column of the jacobian is
! at most gtol in absolute value. Therefore, gtol measures the
! orthogonality desired between the function vector and the columns
! of the jacobian.
! maxfev is a positive integer input variable. Termination occurs when
! the number of calls to fcn with iflag = 1 has reached maxfev.
! diag is an array of length n. If mode = 1 (see below), diag is
! internally set. If mode = 2, diag must contain positive entries
! that serve as multiplicative scale factors for the variables.
! mode is an integer input variable. if mode = 1, the
! variables will be scaled internally. if mode = 2,
! the scaling is specified by the input diag. other
! values of mode are equivalent to mode = 1.
! factor is a positive input variable used in determining the
! initial step bound. this bound is set to the product of
! factor and the euclidean norm of diag*x if nonzero, or else
! to factor itself. in most cases factor should lie in the
! interval (.1,100.).100. is a generally recommended value.
! nprint is an integer input variable that enables controlled printing
! of iterates if it is positive. In this case, fcn is called with
! iflag = 0 at the beginning of the first iteration and every nprint
! iterations thereafter and immediately prior to return, with x, fvec,
! and fjac available for printing. fvec and fjac should not be
! altered. If nprint is not positive, no special calls of fcn with
! iflag = 0 are made.
! info is an integer output variable. If the user has terminated
! execution, info is set to the (negative) value of iflag.
! See description of fcn. Otherwise, info is set as follows.
! info = 0 improper input parameters.
! info = 1 both actual and predicted relative reductions
! in the sum of squares are at most ftol.
! info = 2 relative error between two consecutive iterates
! is at most xtol.
! info = 3 conditions for info = 1 and info = 2 both hold.
! info = 4 the cosine of the angle between fvec and any column of
! the jacobian is at most gtol in absolute value.
! info = 5 number of calls to fcn with iflag = 1 has reached maxfev.
! info = 6 ftol is too small. No further reduction in
! the sum of squares is possible.
! info = 7 xtol is too small. No further improvement in
! the approximate solution x is possible.
! info = 8 gtol is too small. fvec is orthogonal to the
! columns of the jacobian to machine precision.
! nfev is an integer output variable set to the number of
! calls to fcn with iflag = 1.
! njev is an integer output variable set to the number of
! calls to fcn with iflag = 2.
! ipvt is an integer output array of length n. ipvt
! defines a permutation matrix p such that jac*p = q*r,
! where jac is the final calculated jacobian, q is
! orthogonal (not stored), and r is upper triangular
! with diagonal elements of nonincreasing magnitude.
! column j of p is column ipvt(j) of the identity matrix.
! qtf is an output array of length n which contains
! the first n elements of the vector (q transpose)*fvec.
! wa1, wa2, and wa3 are work arrays of length n.
! wa4 is a work array of length m.
! subprograms called
! user-supplied ...... fcn
! minpack-supplied ... dpmpar,enorm,lmpar,qrfac
! fortran-supplied ... ABS,MAX,MIN,SQRT,mod
! argonne national laboratory. minpack project. march 1980.
! burton s. garbow, kenneth e. hillstrom, jorge j. more
! **********
INTEGER :: i, iflag, iter, j, l
REAL (lmdp) :: actred, delta, dirder, epsmch, fnorm, fnorm1, gnorm, &
par, pnorm, prered, ratio, sum, temp, temp1, temp2, xnorm
REAL (lmdp) :: diag(n), qtf(n), wa1(n), wa2(n), wa3(n), wa4(m)
REAL (lmdp), PARAMETER :: one = 1.0_lmdp, p1 = 0.1_lmdp, p5 = 0.5_lmdp, &
p25 = 0.25_lmdp, p75 = 0.75_lmdp, p0001 = 0.0001_lmdp, &
zero = 0.0_lmdp
! epsmch is the machine precision.
epsmch = EPSILON(zero)
info = 0
iflag = 0
nfev = 0
njev = 0
! check the input parameters for errors.