-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver2.c
150 lines (131 loc) · 3.55 KB
/
driver2.c
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
/* The operation of the code is mostly controlled by the parameters
in the cg_parameter structure. In the following example,
the parameter QuadStep is set to FALSE. When QuadStep is TRUE,
the trial step in each iteration is computed as the minimizer
of a quadratic interpolant along the search direction. In
performing the quadstep, we hope to find a suitable line search
point right away, completely by-passing the secant iteration.
However, as the iterates approach a minimizer, the numerical accuracy of
the minimizer of the quadratic interpolant becomes worse. When the relative
change in the function values for two consecutive iterations reach
QuadCutOff, then the code completely turns off the quadstep. The user
can turn off the quadstep by setting QuadStep FALSE. By leaving
QuadStep TRUE, but increasing QuadCutOff (default 1.d-12), the code turns
off the QuadStep sooner. Below, we run the code twice, first with
the QuadStep turned off, then with the QuadStep turned on. Notice that
the performance improves with the QuadStep is on. This behavior is typical.
Termination status: 0
Convergence tolerance for gradient satisfied
maximum norm for gradient: 4.823094e-09
function value: -6.530787e+02
cg iterations: 32
function evaluations: 36
gradient evaluations: 68
===================================
Termination status: 0
Convergence tolerance for gradient satisfied
maximum norm for gradient: 6.269565e-09
function value: -6.530787e+02
cg iterations: 32
function evaluations: 54
gradient evaluations: 46 */
#include <math.h>
#include "cg_user.h"
double myvalue
(
double *x,
INT n,
void *User
) ;
void mygrad
(
double *g,
double *x,
INT n,
void *User
) ;
double myvalgrad
(
double *g,
double *x,
INT n,
void *User
) ;
int main (void)
{
double *x ;
INT i, n ;
cg_parameter Parm ;
/* allocate space for solution */
n = 100 ;
x = (double *) malloc (n*sizeof (double)) ;
/* set starting guess */
for (i = 0; i < n; i++) x [i] = 1. ;
cg_default (&Parm) ; /* set default parameter values */
Parm.QuadStep = FALSE ; /* change QuadStep to FALSE */
/* run the code */
cg_descent(x, n, NULL, &Parm, 1.e-8, myvalue, mygrad, myvalgrad, NULL, NULL, NULL) ;
/* set starting guess */
for (i = 0; i < n; i++) x [i] = 1. ;
Parm.QuadStep = TRUE ; /* change QuadStep to TRUE */
/* run the code */
cg_descent(x, n, NULL, &Parm, 1.e-8, myvalue, mygrad, myvalgrad, NULL, NULL, NULL) ;
free (x) ; /* free workspace */
}
double myvalue
(
double *x,
INT n,
void *User
)
{
double f, t ;
INT i ;
f = 0. ;
for (i = 0; i < n; i++)
{
t = i+1 ;
t = sqrt (t) ;
f += exp (x [i]) - t*x [i] ;
}
return (f) ;
}
void mygrad
(
double *g,
double *x,
INT n,
void *User
)
{
double t ;
INT i ;
for (i = 0; i < n; i++)
{
t = i + 1 ;
t = sqrt (t) ;
g [i] = exp (x [i]) - t ;
}
return ;
}
double myvalgrad
(
double *g,
double *x,
INT n,
void *User
)
{
double ex, f, t ;
INT i ;
f = (double) 0 ;
for (i = 0; i < n; i++)
{
t = i + 1 ;
t = sqrt (t) ;
ex = exp (x [i]) ;
f += ex - t*x [i] ;
g [i] = ex - t ;
}
return (f) ;
}