/* nag_opt_simplex(e04ccc) Example Program * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 6 revised, 2000. * Mark 7 revised, 2001. * */ #include #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void funct(Integer n, double *xc, double *fc, Nag_Comm *comm); static void monit(const Nag_Search_State *st, Nag_Comm *comm); #ifdef __cplusplus } #endif static void ex1(void); static void ex2(void); int main(void) { /* Two examples are called, ex1() which uses the * default settings to solve the problem and * ex2() which solves the same problem with * some optional parameters set by the user. */ Vprintf("e04ccc Example Program Results.\n"); ex1(); ex2(); return EXIT_SUCCESS; } static void ex1(void) { double objf; double x[2]; Integer n; Vprintf("\ne04ccc example 1: no option setting.\n"); n = 2; /* Set up the starting point */ x[0] = 0.4; x[1] = -0.8; e04ccc(n, funct, x, &objf, E04_DEFAULT, NAGCOMM_NULL, NAGERR_DEFAULT); } static void funct(Integer n, double *xc, double *objf, Nag_Comm *comm) { *objf = exp(xc[0]) * (xc[0] * 4.0 * (xc[0] + xc[1]) + xc[1] * 2.0 * (xc[1] + 1.0) + 1.0); } static void ex2(void) { double objf; double x[2]; Integer n; Integer monit_freq; Boolean print; Nag_Comm comm; Nag_E04_Opt options; static NagError fail; Vprintf("\n\ne04ccc example 2: using option setting.\n"); n = 2; monit_freq = 20; e04xxc(&options); options.print_fun = monit; /* Read remaining option value from file */ fail.print = TRUE; print = TRUE; e04xyc("e04ccc", "stdin", &options, print, "stdout", NAGERR_DEFAULT); comm.p = (Pointer)&monit_freq; /* Starting values */ x[0] = -1.0; x[1] = 1.0; e04ccc(n, funct, x, &objf, &options, &comm, &fail); } static void monit(const Nag_Search_State *st, Nag_Comm *comm) { #define SIM(I,J) sim[((I)-1)*n + (J)-1] double *sim; Integer i, j; Integer n, ncall, iter; double fmin, fmax; Integer *monit_freq=(Integer *)comm->p; fmin = st->fmin; fmax = st->fmax; sim=st->simplex; ncall = st->nf; iter = st->iter; n = st->n; if (iter % *monit_freq == 0) { Vprintf("\nAfter %1ld iteration and %1ld function calls," " the function value is %10.4e\n", iter, ncall, fmin); Vprintf("The simplex is\n"); for (i = 1; i <= n+1; ++i) { for (j = 1; j <= n; ++j) { Vprintf(" %12.4e", SIM(i,j)); } Vprintf("\n"); } } if (comm->sol_prt) { Vprintf("The final solution is\n"); for (i = 0; i x[i]); Vprintf("After %1ld iterations and %1ld function calls the function \n" "value at the current solution point is %12.4e.\n", iter, ncall, fmin); } } /* monit */