CMP EMBEDDED.COM

Login | Register     Welcome Guest   IPS  
HOME DESIGN PRODUCTS COLUMNS E-LEARNING CONFERENCES CODE FORUMS/BLOGS NEWSLETTERS CONTACT FEATURES RSS RSS

Listing 4: New version of step Listing 4: New version of step

/* Step along the function, looking for the first dip in its
* value. To support functions that start off increasing, we
* allow for the possibility of an initial rise.
*
* This function works with the outer state machine, and returns
* the next state. The returned state depends upon whether or not
* an internal minimum (triplet) was found.
*/
STATE step(double (*f)(double), double xL, double xR, long steps){
assert(steps > 1);
// state variable for step search
typedef enum{FIRST_STEP, STEP_UP, STEP_DOWN} STEP_STATE;
STEP_STATE step_state = FIRST_STEP;
// local variables
long n = 0; // step counter
double range = xR-xL;
double xx, yy; // local data point
int lower = FALSE;
int descending = FALSE;
// initialize search
npoints = 0;
index = 0;
// manually insert first two points
xx = xL;
test_min(f, xx, yy);
append(xx, yy);
xx += range/steps;
if(test_min(f, xx, yy))
index = 1;
append(xx, yy);
// begin step loop
step_state = STEP_UP;
for(n=2; n<=steps; n++){
xx = xL + (double(n)/steps) * range;
lower = test_min(f, xx, yy);
append(xx, yy);
switch(step_state){
case STEP_UP:
if(lower){
descending = TRUE;
step_state = STEP_DOWN;
index = npoints - 1;
}
break;
case STEP_DOWN:
if(!lower){
index = npoints - 2;
return HAVE_TRIPLET;
}
break;
}
}
if(descending)
return RIGHT_EDGE;
else
return LEFT_EDGE;
}
Embedded.com Career Center
Ready for a change?
SEARCH JOBS

Browse all jobs

SPONSOR
RECENT JOB POSTINGS





 :