/* nag_opt_simplex (e04ccc) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 6 revised, 2000. * Mark 7 revised, 2001. * Mark 7b revised, 2004. * Mark 8 revised, 2004. */ #include #include #include #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void NAG_CALL funct(Integer n, double *xc, double *fc, Nag_Comm *comm); #ifdef __cplusplus } #endif int main(int argc, char *argv[]) { FILE *fpout; char *optionsfile = 0; char *outfile = 0; Nag_Boolean print; Integer exit_status = 0, n; Nag_Comm comm; Nag_E04_Opt options; double objf, *x = 0; NagError fail; INIT_FAIL(fail); /* Check for command-line IO options */ fpout = nag_example_file_io(argc, argv, "-results", NULL); (void) nag_example_file_io(argc, argv, "-options", &optionsfile); (void) nag_example_file_io(argc, argv, "-nag_write", &outfile); if (!outfile) { outfile = NAG_ALLOC(7, char); strcpy(outfile, "stdout"); } fprintf(fpout, "nag_opt_simplex (e04ccc) Example Program Results\n"); n = 2; if (n >= 1) { if (!(x = NAG_ALLOC(n, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid n.\n"); exit_status = 1; return exit_status; } /* nag_opt_init (e04xxc). * Initialization function for option setting */ nag_opt_init(&options); strcpy(options.outfile, outfile); /* Read remaining option value from file */ print = Nag_TRUE; /* nag_opt_read (e04xyc). * Read options from a text file */ if (strcmp(outfile, "stdout")) fclose(fpout); nag_opt_read("e04ccc", optionsfile, &options, print, options.outfile, &fail); if (strcmp(outfile, "stdout")) { fpout = fopen(outfile, "a"); } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_opt_read (e04xyc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Starting values */ x[0] = -1.0; x[1] = 1.0; /* nag_opt_simplex (e04ccc), see above. */ if (strcmp(outfile, "stdout")) fclose(fpout); nag_opt_simplex(n, funct, x, &objf, &options, &comm, &fail); if (strcmp(outfile, "stdout")) { fpout = fopen(outfile, "a"); } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_opt_simplex (e04ccc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (optionsfile) NAG_FREE(optionsfile); if (outfile) NAG_FREE(outfile); return exit_status; } static void NAG_CALL 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); }