/* S4MIN.C xref: input: output: does: Scan for a minimum of a function in an interval */ /************************************************************/ #include "mtb.h" /************************************************************/ void s4min (double f(double), double lower, double upper, int n, double *x_min, double *f_min) /* scan the interval [lower, upper] for a min of f() */ /* evaluate f() at n points in the interval */ { double d, x, fx; if (lower>upper) {d=upper; upper=lower; lower=d;} /* swap */ else if (lower==upper) { *x_min=upper; *f_min=f(upper); return; } if (n < 2) n = 2; d = (upper - lower) / (n-1); *x_min = lower; *f_min = f(*x_min); for (x = lower+d; x <= upper; x += d) { fx = f(x); if (fx < *f_min) {*x_min = x; *f_min = fx;} } }