/* nag_opt_one_var_func (e04abc) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.3, 2024.
*
*/
#include <math.h>
#include <nag.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
static void NAG_CALL funct(double xc, double *fc, Nag_Comm *comm);
#ifdef __cplusplus
}
#endif
static void NAG_CALL funct(double xc, double *fc, Nag_Comm *comm) {
if (comm->user[0] == -1.0) {
printf("(User-supplied callback funct, first invocation.)\n");
comm->user[0] = 0.0;
}
*fc = sin(xc) / xc;
}
/* funct */
int main(void) {
static double ruser[1] = {-1.0};
Integer exit_status = 0, max_fun;
NagError fail;
Nag_Comm comm;
double a, b, e1, e2, f, x;
INIT_FAIL(fail);
printf("nag_opt_one_var_func (e04abc) Example Program Results\n\n");
/* For communication with user-supplied functions: */
comm.user = ruser;
/* e1 and e2 are set to zero so that nag_opt_one_var_func (e04abc) will
* reset them to their default values.
*/
e1 = 0.0;
e2 = 0.0;
/* The minimum is known to lie in the range (3.5, 5.0) */
a = 3.5;
b = 5.0;
/* Allow 30 calls of funct */
max_fun = 30;
/* nag_opt_one_var_func (e04abc).
* Minimizes a function of one variable, using function values only.
*/
nag_opt_one_var_func(funct, e1, e2, &a, &b, max_fun, &x, &f, &comm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_opt_one_var_func (e04abc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
printf("The minimum lies in the interval %7.5f to %7.5f.\n", a, b);
printf("Its estimated position is %7.5f,\n", x);
printf("where the function value is %13.4e.\n", f);
printf("%1" NAG_IFMT " function evaluations were required.\n", comm.nf);
END:
return exit_status;
}