/* nag_opt_conj_grad (e04dgc) Example Program * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 7 revised, 2001. * */ #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void objfun(Integer n, double x[], double *objf, double g[], 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("e04dgc Example Program Results.\n"); Vscanf(" %*[^\n]"); /* Skip heading in data file */ ex1(); ex2(); return EXIT_SUCCESS; } static void objfun(Integer n, double x[], double *objf, double g[], Nag_Comm *comm) { /* Function to evaluate objective function and its 1st derivatives. */ double ex1, x1, x2; ex1 = exp(x[0]); x1 = x[0]; x2 = x[1]; *objf = ex1*(4*x1*x1 + 2*x2*x2 + 4*x1*x2 + 2*x2 + 1); g[0] = 4*ex1*(2*x1 + x2) + *objf; g[1] = 2*ex1*(2*x2 + 2*x1 + 1); } /* objfun */ static void ex1(void) { Integer n; double objf; double x[2], g[2]; static NagError fail; Vprintf("\ne04dgc example 1: no option setting.\n"); n = 2; /* Number of variables */ /* Set the initial estimate of the solution. */ x[0] = -1.0; x[1] = 1.0; /* Solve the problem. */ fail.print = TRUE; e04dgc(n, objfun, x, &objf, g, E04_DEFAULT, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) exit(EXIT_FAILURE); } /* ex1 */ static void ex2(void) { Integer n; double objf; double x[2], g[2]; Boolean print; Nag_E04_Opt options; static NagError fail; Vprintf("\n\ne04dgc example 2: using option setting.\n"); /* Initialise options structure and read option values from file */ fail.print = TRUE; print = TRUE; e04xxc(&options); e04xyc("e04dgc", "stdin", &options, print, "stdout", &fail); if (fail.code != NE_NOERROR) exit(EXIT_FAILURE); n = 2; /* Number of variables */ /* Set the initial estimate of the solution. */ x[0] = -1.0; x[1] = 1.0; /* Solve the problem. */ e04dgc(n, objfun, x, &objf, g, &options, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) exit(EXIT_FAILURE); } /* ex2 */