-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver4.c
152 lines (129 loc) · 3.04 KB
/
driver4.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
151
152
/* When the line search routine tries to bracket a local minimizer in
the search direction, it may expand the initial line search interval.
The default expansion factor is 5. You can modify this factor using
the parameter rho. In the following example, we choose a small initial
stepsize (initial step is 1.e-5), QuadStep is FALSE, and rho is 1.5.
The code has to do a number of expansions to reach a suitable
interval bracketing the minimizer in the initial search direction.
Termination status: 0
Convergence tolerance for gradient satisfied
maximum norm for gradient: 6.283573e-09
function value: -6.530787e+02
cg iterations: 31
function evaluations: 55
gradient evaluations: 86
===================================
We then restore the default value of rho to obtain:
Termination status: 0
Convergence tolerance for gradient satisfied
maximum norm for gradient: 9.605754e-09
function value: -6.530787e+02
cg iterations: 31
function evaluations: 38
gradient evaluations: 69 */
#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)) ;
/* starting guess */
for (i = 0; i < n; i++) x [i] = 1. ;
/* initialize default parameter values */
cg_default (&Parm) ;
/* set parameter values */
Parm.step = 1.e-5 ;
Parm.QuadStep = FALSE ;
Parm.rho = 1.5 ;
/* solve the problem */
cg_descent(x, n, NULL, &Parm, 1.e-8, myvalue, mygrad, myvalgrad, NULL, NULL, NULL) ;
/* starting guess */
for (i = 0; i < n; i++) x [i] = 1. ;
/* set rho = 5. */
Parm.rho = 5. ;
/* solve the problem */
cg_descent(x, n, NULL, &Parm, 1.e-8, myvalue, mygrad, myvalgrad, NULL, NULL, NULL) ;
/* free work space */
free (x) ;
}
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) ;
}