-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdlinmin.c
66 lines (60 loc) · 1.5 KB
/
oldlinmin.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
/* dscf_line is called by dscf_krymin()
*
* it searches for the closest local minimum
*
*/
float dscf_line( search,where,params,n, what )
int n;
float search[],params[],*where[];
float (*what)();
{
float step,dstep,myval,bestval;
#define MAXBUF 100
float steps[MAXBUF],vals[MAXBUF];
int inbuf;
int i,irun,itry;;
inbuf = 1;
steps[0] = 0.;
vals[0] = (*what)();
bestval = vals[0];
step = 0.;
dstep = 1.;
for( itry = 0; itry < 8; itry ++)
{
/* use irun to search in terms of dstep */
for( irun = 0; irun < 100; irun ++)
{
/* first look in the table */
for( i=0; i< inbuf; i++)
{
if( dstep == steps[i] ) {
myval = vals[i];
goto STEP_FOUND;
}
}
/* if here then we have to calculate the function */
for( i=0; i< n; i++)
{
*where[i] = params[i] + search[i]*dstep;
}
myval = (*what)();
if( inbuf < MAXBUF)
{vals[inbuf] = myval;steps[inbuf++] = dstep;}
STEP_FOUND:
if( myval > bestval)
{
dstep = -dstep*.5;
break;
} else {
bestval = myval;
step = dstep;
}
}/* irun */
}/* itry */
/* update to the best found */
for( i=0; i< n; i++)
{
*where[i] = params[i] + search[i]*step;
}
return step;
}