-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminpack.cpp
2917 lines (2802 loc) · 73.2 KB
/
minpack.cpp
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
//
// Created by Администратор on 21.01.2018.
//
#include "minpack.h"
# include <cmath>
# include <cstdlib>
# include <ctime>
# include <iomanip>
# include <iostream>
using namespace std;
//****************************************************************************80
void chkder ( int m, int n, double x[], double fvec[], double fjac[],
int ldfjac, double xp[], double fvecp[], int mode, double err[] )
//****************************************************************************80
//
// Purpose:
//
// CHKDER checks the gradients of M functions in N variables.
//
// Discussion:
//
// This function checks the gradients of M nonlinear functions
// in N variables, evaluated at a point x, for consistency with
// the functions themselves. The user must call chkder twice,
// first with mode = 1 and then with mode = 2.
//
// mode = 1: on input, x must contain the point of evaluation.
// on output, xp is set to a neighboring point.
//
// mode = 2. on input, fvec must contain the functions and the
// rows of fjac must contain the gradients
// of the respective functions each evaluated
// at x, and fvecp must contain the functions
// evaluated at xp.
// on output, err contains measures of correctness of
// the respective gradients.
//
// The function does not perform reliably if cancellation or
// rounding errors cause a severe loss of significance in the
// evaluation of a function. therefore, none of the components
// of x should be unusually small (in particular, zero) or any
// other value which may cause loss of significance.
//
// Licensing:
//
// This code may freely be copied, modified, and used for any purpose.
//
// Modified:
//
// 05 April 2010
//
// Author:
//
// Original FORTRAN77 version by Jorge More, Burt Garbow, Ken Hillstrom.
// C++ version by John Burkardt.
//
// Reference:
//
// Jorge More, Burton Garbow, Kenneth Hillstrom,
// User Guide for MINPACK-1,
// Technical Report ANL-80-74,
// Argonne National Laboratory, 1980.
//
// Parameters:
//
// Input, int M, the number of functions.
//
// Input, int N, the number of variables.
//
// Input, double X[N], the point at which the jacobian is to be checked.
//
// fvec is an array of length m. on input when mode = 2,
// fvec must contain the functions evaluated at x.
//
// fjac is an m by n array. on input when mode = 2,
// the rows of fjac must contain the gradients of
// the respective functions evaluated at x.
//
// ldfjac is a positive integer input parameter not less than m
// which specifies the leading dimension of the array fjac.
//
// xp is an array of length n. on output when mode = 1,
// xp is set to a neighboring point of x.
//
// fvecp is an array of length m. on input when mode = 2,
// fvecp must contain the functions evaluated at xp.
//
// mode is an integer input variable set to 1 on the first call
// and 2 on the second. other values of mode are equivalent
// to mode = 1.
//
// err is an array of length m. on output when mode = 2,
// err contains measures of correctness of the respective
// gradients. if there is no severe loss of significance,
// then if err(i) is 1.0 the i-th gradient is correct,
// while if err(i) is 0.0 the i-th gradient is incorrect.
// for values of err between 0.0 and 1.0, the categorization
// is less certain. in general, a value of err(i) greater
// than 0.5 indicates that the i-th gradient is probably
// correct, while a value of err(i) less than 0.5 indicates
// that the i-th gradient is probably incorrect.
//
{
double eps;
double epsf;
double epslog;
double epsmch;
const double factor = 100.0;
int i;
int j;
double temp;
//
// EPSMCH is the machine precision.
//
epsmch = r8_epsilon ( );
//
eps = sqrt ( epsmch );
//
// MODE = 1.
//
if ( mode == 1 )
{
for ( j = 0; j < n; j++ )
{
if ( x[j] == 0.0 )
{
temp = eps;
}
else
{
temp = eps * fabs ( x[j] );
}
xp[j] = x[j] + temp;
}
}
//
// MODE = 2.
//
else
{
epsf = factor * epsmch;
epslog = log10 ( eps );
for ( i = 0; i < m; i++ )
{
err[i] = 0.0;
}
for ( j = 0; j < n; j++ )
{
if ( x[j] == 0.0 )
{
temp = 1.0;
}
else
{
temp = fabs ( x[j] );
}
for ( i = 0; i < m; i++ )
{
err[i] = err[i] + temp * fjac[i+j*ldfjac];
}
}
for ( i = 0; i < m; i++ )
{
temp = 1.0;
if ( fvec[i] != 0.0 &&
fvecp[i] != 0.0 &&
epsf * fabs ( fvec[i] ) <= fabs ( fvecp[i] - fvec[i] ) )
{
temp = eps * fabs ( ( fvecp[i] - fvec[i] ) / eps - err[i] )
/ ( fabs ( fvec[i] ) + fabs ( fvecp[i] ) );
if ( temp <= epsmch )
{
err[i] = 1.0;
}
else if ( temp < eps )
{
err[i] = ( log10 ( temp ) - epslog ) / epslog;
}
else
{
err[i] = 0.0;
}
}
}
}
return;
}
//****************************************************************************80
void dogleg ( int n, double r[], int lr, double diag[], double qtb[],
double delta, double x[], double wa1[], double wa2[] )
//****************************************************************************80
//
// Purpose:
//
// DOGLEG combines Gauss-Newton and gradient for a minimizing step.
//
// Discussion:
//
// Given an M by N matrix A, an n by n nonsingular diagonal
// matrix d, an m-vector b, and a positive number delta, the
// problem is to determine the convex combination x of the
// gauss-newton and scaled gradient directions that minimizes
// (a*x - b) in the least squares sense, subject to the
// restriction that the euclidean norm of d*x be at most delta.
//
// This function completes the solution of the problem
// if it is provided with the necessary information from the
// qr factorization of a.
//
// That is, if a = q*r, where q has orthogonal columns and r is an upper
// triangular matrix, then dogleg expects the full upper triangle of r and
// the first n components of Q'*b.
//
// Licensing:
//
// This code may freely be copied, modified, and used for any purpose.
//
// Modified:
//
// 05 April 2010
//
// Author:
//
// Original FORTRAN77 version by Jorge More, Burt Garbow, Ken Hillstrom.
// C++ version by John Burkardt.
//
// Reference:
//
// Jorge More, Burton Garbow, Kenneth Hillstrom,
// User Guide for MINPACK-1,
// Technical Report ANL-80-74,
// Argonne National Laboratory, 1980.
//
// Parameters:
//
// Input, int N, the order of R.
//
// Input, double R[LR], the upper triangular matrix R stored by rows.
//
// Input, int LR, the size of the storage for R, which should be at
// least (n*(n+1))/2.
//
// Input, double DIAG[N], the diagonal elements of the matrix D.
//
// qtb is an input array of length n which must contain the first
// n elements of the vector (q transpose)*b.
//
// delta is a positive input variable which specifies an upper
// bound on the euclidean norm of d*x.
//
// x is an output array of length n which contains the desired
// convex combination of the gauss-newton direction and the
// scaled gradient direction.
//
// Workspace, WA1[N].
//
// Workspace, WA2[N].
//
{
double alpha;
double bnorm;
double epsmch;
double gnorm;
int i;
int j;
int jj;
int jp1;
int k;
int l;
double qnorm;
double sgnorm;
double sum;
double temp;
//
// EPSMCH is the machine precision.
//
epsmch = r8_epsilon ( );
//
// Calculate the Gauss-Newton direction.
//
jj = ( n * ( n + 1 ) ) / 2 + 1;
for ( k = 1; k <= n; k++ )
{
j = n - k + 1;
jp1 = j + 1;
jj = jj - k;
l = jj + 1;
sum = 0.0;
for ( i = jp1; i <= n; i++ )
{
sum = sum + r[l-1] * x[i-1];
l = l + 1;
}
temp = r[jj-1];
if ( temp == 0.0 )
{
l = j;
for ( i = 1; i <= j; i++ )
{
temp = r8_max ( temp, fabs ( r[l-1] ) );
l = l + n - i;
}
temp = epsmch * temp;
if ( temp == 0.0 )
{
temp = epsmch;
}
}
x[j-1] = ( qtb[j-1] - sum ) / temp;
}
//
// Test whether the Gauss-Newton direction is acceptable.
//
for ( j = 0; j < n; j++ )
{
wa1[j] = 0.0;
wa2[j] = diag[j] * x[j];
}
qnorm = enorm ( n, wa2 );
if ( qnorm <= delta )
{
return;
}
//
// The Gauss-Newton direction is not acceptable.
// Calculate the scaled gradient direction.
//
l = 0;
for ( j = 0; j < n; j++ )
{
temp = qtb[j];
for ( i = j; i < n; i++ )
{
wa1[i-1] = wa1[i-1] + r[l-1] * temp;
l = l + 1;
}
wa1[j] = wa1[j] / diag[j];
}
//
// Calculate the norm of the scaled gradient and test for
// the special case in which the scaled gradient is zero.
//
gnorm = enorm ( n, wa1 );
sgnorm = 0.0;
alpha = delta / qnorm;
//
// Calculate the point along the scaled gradient
// at which the quadratic is minimized.
//
if ( gnorm != 0.0 )
{
for ( j = 0; j < n; j++ )
{
wa1[j] = ( wa1[j] / gnorm ) / diag[j];
}
l = 0;
for ( j = 0; j < n; j++ )
{
sum = 0.0;
for ( i = j; i < n; i++ )
{
sum = sum + r[l] * wa1[i];
l = l + 1;
}
wa2[j] = sum;
}
temp = enorm ( n, wa2 );
sgnorm = ( gnorm / temp ) / temp;
alpha = 0.0;
//
// If the scaled gradient direction is not acceptable,
// calculate the point along the dogleg at which the quadratic is minimized.
//
if ( sgnorm < delta)
{
bnorm = enorm ( n, qtb );
temp = ( bnorm / gnorm ) * ( bnorm / qnorm ) * ( sgnorm / delta );
temp = temp - ( delta / qnorm ) * ( sgnorm / delta ) * ( sgnorm / delta )
+ sqrt ( pow ( temp - ( delta / qnorm ), 2 )
+ ( 1.0 - ( delta / qnorm ) * ( delta / qnorm ) )
* ( 1.0 - ( sgnorm / delta ) * ( sgnorm / delta ) ) );
alpha = ( ( delta / qnorm )
* ( 1.0 - ( sgnorm / delta ) * ( sgnorm / delta ) ) ) / temp;
}
}
//
// Form appropriate convex combination of the Gauss-Newton
// direction and the scaled gradient direction.
//
temp = ( 1.0 - alpha ) * r8_min ( sgnorm, delta );
for ( j = 0; j < n; j++ )
{
x[j] = temp * wa1[j] + alpha * x[j];
}
return;
}
//****************************************************************************80
double enorm ( int n, double x[] )
//****************************************************************************80
//
// Purpose:
//
// ENORM returns the Euclidean norm of a vector.
//
// Licensing:
//
// This code may freely be copied, modified, and used for any purpose.
//
// Modified:
//
// 05 April 2010
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of entries in A.
//
// Input, double X[N], the vector whose norm is desired.
//
// Output, double ENORM, the norm of X.
//
{
int i;
double value;
value = 0.0;
for ( i = 0; i < n; i++ )
{
value = value + x[i] * x[i];
}
value = sqrt ( value );
return value;
}
//****************************************************************************80
void fdjac1 ( void fcn ( int n, double x[], double f[], int &iflag ),
int n, double x[], double fvec[], double fjac[], int ldfjac, int &iflag,
int ml, int mu, double epsfcn, double wa1[], double wa2[] )
//****************************************************************************80
//
// Purpose:
//
// FDJAC1 estimates an N by N Jacobian matrix using forward differences.
//
// Discussion:
//
// This function computes a forward-difference approximation
// to the N by N jacobian matrix associated with a specified
// problem of N functions in N variables.
//
// If the jacobian has a banded form, then function evaluations are saved
// by only approximating the nonzero terms.
//
// Licensing:
//
// This code may freely be copied, modified, and used for any purpose.
//
// Modified:
//
// 05 April 2010
//
// Author:
//
// Original FORTRAN77 version by Jorge More, Burt Garbow, Ken Hillstrom.
// C++ version by John Burkardt.
//
// Reference:
//
// Jorge More, Burton Garbow, Kenneth Hillstrom,
// User Guide for MINPACK-1,
// Technical Report ANL-80-74,
// Argonne National Laboratory, 1980.
//
// Parameters:
//
// 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(n,x,fvec,iflag)
// integer n,iflag
// double precision x(n),fvec(n)
// ----------
// 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 fdjac1.
// in this case set iflag to a negative integer.
//
// Input, int N, the number of functions and variables.
//
// Input, double X[N], the evaluation point.
//
// Input, double FVEC[N], the functions evaluated at X.
//
// Output, double FJAC[N*N], the approximate jacobian matrix at X.
//
// ldfjac is a positive integer input variable not less than n
// which specifies the leading dimension of the array fjac.
//
// iflag is an integer variable which can be used to terminate
// the execution of fdjac1. see description of fcn.
//
// ml is a nonnegative integer input variable which specifies
// the number of subdiagonals within the band of the
// jacobian matrix. if the jacobian is not banded, set
// ml to at least n - 1.
//
// 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.
//
// mu is a nonnegative integer input variable which specifies
// the number of superdiagonals within the band of the
// jacobian matrix. if the jacobian is not banded, set
// mu to at least n - 1.
//
// wa1 and wa2 are work arrays of length n. if ml + mu + 1 is at
// least n, then the jacobian is considered dense, and wa2 is
// not referenced.
{
double eps;
double epsmch;
double h;
int i;
int j;
int k;
int msum;
double temp;
//
// EPSMCH is the machine precision.
//
epsmch = r8_epsilon ( );
eps = sqrt ( r8_max ( epsfcn, epsmch ) );
msum = ml + mu + 1;
//
// Computation of dense approximate jacobian.
//
if ( n <= msum )
{
for ( j = 0; j < n; j++ )
{
temp = x[j];
h = eps * fabs ( temp );
if ( h == 0.0 )
{
h = eps;
}
x[j] = temp + h;
fcn ( n, x, wa1, iflag );
if ( iflag < 0 )
{
break;
}
x[j] = temp;
for ( i = 0; i < n; i++ )
{
fjac[i+j*ldfjac] = ( wa1[i] - fvec[i] ) / h;
}
}
}
//
// Computation of a banded approximate jacobian.
//
else
{
for ( k = 0; k < msum; k++ )
{
for ( j = k; j < n; j = j + msum )
{
wa2[j] = x[j];
h = eps * fabs ( wa2[j] );
if ( h == 0.0 )
{
h = eps;
}
x[j] = wa2[j] + h;
}
fcn ( n, x, wa1, iflag );
if ( iflag < 0 )
{
break;
}
for ( j = k; j < n; j = j + msum )
{
x[j] = wa2[j];
h = eps * fabs ( wa2[j] );
if ( h == 0.0 )
{
h = eps;
}
for ( i = 0; i < n; i++ )
{
if ( j - mu <= i && i <= j + ml )
{
fjac[i+j*ldfjac] = ( wa1[i] - fvec[i] ) / h;
}
else
{
fjac[i+j*ldfjac] = 0.0;
}
}
}
}
}
return;
}
//****************************************************************************80
void fdjac2 ( void fcn ( int m, int n, double x[], double fvec[], int &iflag ),
int m, int n, double x[], double fvec[], double fjac[], int ldfjac,
int &iflag, double epsfcn, double wa[] )
//****************************************************************************80
//
// Purpose:
//
// FDJAC2 estimates an M by N Jacobian matrix using forward differences.
//
// Discussion:
//
// This function computes a forward-difference approximation
// to the M by N jacobian matrix associated with a specified
// problem of M functions in N variables.
//
// Licensing:
//
// This code may freely be copied, modified, and used for any purpose.
//
// Modified:
//
// 05 April 2010
//
// Author:
//
// Original FORTRAN77 version by Jorge More, Burt Garbow, Ken Hillstrom.
// C++ version by John Burkardt.
//
// Reference:
//
// Jorge More, Burton Garbow, Kenneth Hillstrom,
// User Guide for MINPACK-1,
// Technical Report ANL-80-74,
// Argonne National Laboratory, 1980.
//
// Parameters:
//
// 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
// double precision 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 fdjac2.
// in this case set iflag to a negative integer.
//
// Input, int M, the number of functions.
//
// Input, int N, the number of variables. N must not exceed M.
//
// Input, double X[N], the point at which the jacobian is to be estimated.
//
// fvec is an input array of length m which must contain the
// functions evaluated at x.
//
// fjac is an output m by n array which contains the
// approximation to the jacobian matrix evaluated at x.
//
// ldfjac is a positive integer input variable not less than m
// which specifies the leading dimension of the array fjac.
//
// iflag is an integer variable which can be used to terminate
// the execution of fdjac2. see description of fcn.
//
// 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.
//
// wa is a work array of length m.
//
{
double eps;
double epsmch;
double h;
int i;
int j;
double temp;
//
// EPSMCH is the machine precision.
//
epsmch = r8_epsilon ( );
eps = sqrt ( r8_max ( epsfcn, epsmch ) );
for ( j = 0; j < n; j++ )
{
temp = x[j];
if ( temp == 0.0 )
{
h = eps;
}
else
{
h = eps * fabs ( temp );
}
x[j] = temp + h;
fcn ( m, n, x, wa, iflag );
if ( iflag < 0 )
{
break;
}
x[j] = temp;
for ( i = 0; i < m; i++ )
{
fjac[i+j*ldfjac] = ( wa[i] - fvec[i] ) / h;
}
}
return;
}
//****************************************************************************80
int hybrd ( void fcn ( int n, double x[], double fvec[], int &iflag ),
int n, double x[],
double fvec[], double xtol, int maxfev, int ml, int mu, double epsfcn,
double diag[], int mode, double factor, int nprint, int nfev,
double fjac[], int ldfjac, double r[], int lr, double qtf[], double wa1[],
double wa2[], double wa3[], double wa4[] )
//****************************************************************************80
//
// Purpose:
//
// HYBRD finds a zero of a system of N nonlinear equations.
//
// Discussion:
//
// The purpose of HYBRD is to find a zero of a system of
// N nonlinear functions in N variables by a modification
// of the Powell hybrid method.
//
// The user must provide FCN, which calculates the functions.
//
// The jacobian is calculated by a forward-difference approximation.
//
// Licensing:
//
// This code may freely be copied, modified, and used for any purpose.
//
// Modified:
//
// 08 April 2010
//
// Author:
//
// Original FORTRAN77 version by Jorge More, Burt Garbow, Ken Hillstrom.
// C++ version by John Burkardt.
//
// Reference:
//
// Jorge More, Burton Garbow, Kenneth Hillstrom,
// User Guide for MINPACK-1,
// Technical Report ANL-80-74,
// Argonne National Laboratory, 1980.
//
// Parameters:
//
// 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(n,x,fvec,iflag)
// integer n,iflag
// double precision x(n),fvec(n)
// ----------
// 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 hybrd.
// in this case set iflag to a negative integer.
//
// Input, int N, the number of functions and variables.
//
// Input/output, double X[N]. On input an initial estimate of the solution.
// On output, the final estimate of the solution.
//
// Output, double FVEC[N], the functions evaluated at the output value of X.
//
// Input, double XTOL, a nonnegative value. Termination occurs when the
// relative error between two consecutive iterates is at most XTOL.
//
// Input, int MAXFEV. Termination occurs when the number of calls to FCN
// is at least MAXFEV by the end of an iteration.
//
// ml is a nonnegative integer input variable which specifies
// the number of subdiagonals within the band of the
// jacobian matrix. if the jacobian is not banded, set
// ml to at least n - 1.
//
// mu is a nonnegative integer input variable which specifies
// the number of superdiagonals within the band of the
// jacobian matrix. if the jacobian is not banded, set
// mu to at least n - 1.
//
// 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 relative error between two consecutive iterates
// is at most xtol.
//
// info = 2 number of calls to fcn has reached or exceeded
// maxfev.
//
// info = 3 xtol is too small. no further improvement in
// the approximate solution x is possible.
//
// info = 4 iteration is not making good progress, as
// measured by the improvement from the last
// five jacobian evaluations.
//
// info = 5 iteration is not making good progress, as
// measured by the improvement from the last
// ten iterations.
//
// nfev is an integer output variable set to the number of
// calls to fcn.
//
// fjac is an output n by n array which contains the
// orthogonal matrix q produced by the qr factorization
// of the final approximate jacobian.
//
// ldfjac is a positive integer input variable not less than n
// which specifies the leading dimension of the array fjac.
//
// r is an output array of length lr which contains the
// upper triangular matrix produced by the qr factorization
// of the final approximate jacobian, stored rowwise.
//
// lr is a positive integer input variable not less than
// (n*(n+1))/2.
//
// qtf is an output array of length n which contains
// the vector (q transpose)*fvec.
//
// wa1, wa2, wa3, and wa4 are work arrays of length n.
//
{
double actred;
double delta;
double epsmch;
double fnorm;
double fnorm1;
int i;
int iflag;
int info;
int iter;
int iwa[1];
int j;
bool jeval;
int l;
int msum;
int ncfail;
int ncsuc;
int nslow1;
int nslow2;
const double p001 = 0.001;
const double p0001 = 0.0001;
const double p1 = 0.1;
const double p5 = 0.5;
double pnorm;
double prered;
double ratio;
bool sing;
double sum;
double temp;
double xnorm;
//
// Certain loops in this function were kept closer to their original FORTRAN77
// format, to avoid confusing issues with the array index L. These loops are
// marked "DO NOT ADJUST", although they certainly could be adjusted (carefully)
// once the initial translated code is tested.
//
//
// EPSMCH is the machine precision.
//
epsmch = r8_epsilon ( );
info = 0;
iflag = 0;
nfev = 0;
//
// Check the input parameters.
//
if ( n <= 0 )
{
info = 0;
return info;
}
if ( xtol < 0.0 )
{
info = 0;
return info;
}
if ( maxfev <= 0 )
{
info = 0;
return info;
}
if ( ml < 0 )
{
info = 0;
return info;
}
if ( mu < 0 )
{
info = 0;