-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver3.c
132 lines (117 loc) · 2.72 KB
/
driver3.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
/* In the line search for first iteration, there is very little information
available for choosing a suitable stepsize. By default, the code employs
very low order approximations to the function to estimate a suitable
stepsize. In some cases, this initial stepsize can be problematic.
For example, if the cost function contains a log function, the initial
step might cause the code to try to compute the log of a negative number.
If the cost function contains an exponential, then the initial stepsize
might lead to an overflow. In either case, NaNs are potentially generated.
If the default stepsize is unsuitable, you can input the starting
stepsize using the parameter ``step''.
In the following example, the initial stepsize is set to 1.
The output is below:
Termination status: 0
Convergence tolerance for gradient satisfied
maximum norm for gradient: 7.305971e-09
function value: -6.530787e+02
cg iterations: 31
function evaluations: 51
gradient evaluations: 44 */
#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_stats Stats ;
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 step = 1. */
Parm.step = 1. ;
/* solve the problem */
cg_descent(x, n, &Stats, &Parm, 1.e-8, myvalue, mygrad, myvalgrad, NULL, NULL, NULL) ;
free (x) ; /* free work space */
}
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) ;
}