/* nag_opt_conj_grad (e04dgc) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 7 revised, 2001. * Mark 8 revised, 2004. * */ #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 int ex1(void); static int ex2(void); int main(void) { Integer exit_status_ex1=0; Integer exit_status_ex2=0 ; Vprintf("nag_opt_conj_grad (e04dgc) Example Program Results.\n"); Vscanf(" %*[^\n]"); /* Skip heading in data file */ /* 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. */ exit_status_ex1 = ex1(); exit_status_ex2 = ex2(); return exit_status_ex1 == 0 && exit_status_ex2 == 0 ? 0 : 1; } 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 int ex1(void) { Integer exit_status=0, n; NagError fail; double *g=0, objf, *x=0; INIT_FAIL(fail); Vprintf("\nnag_opt_conj_grad (e04dgc) example 1: no option setting.\n"); n = 2; /* Number of variables */ if (n>=1) { if ( !( x = NAG_ALLOC(n, double)) || !( g = NAG_ALLOC(n, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } } else { Vprintf("Invalid n.\n"); exit_status = 1; return exit_status; } /* Set the initial estimate of the solution. */ x[0] = -1.0; x[1] = 1.0; /* Solve the problem. */ /* nag_opt_conj_grad (e04dgc). * Unconstrained minimization using conjugate gradients */ nag_opt_conj_grad(n, objfun, x, &objf, g, E04_DEFAULT, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_conj_grad (e04dgc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (x) NAG_FREE(x); if (g) NAG_FREE(g); return exit_status; } /* ex1 */ static int ex2(void) { Nag_Boolean print; Integer exit_status=0, n; NagError fail; Nag_E04_Opt options; double *g=0, objf, *x=0; INIT_FAIL(fail); Vprintf("\n\nnag_opt_conj_grad (e04dgc) example 2: using option setting.\n"); /* Initialise options structure and read option values from file */ print = Nag_TRUE; n = 2; /* Number of variables */ if (n>=1) { if ( !( x = NAG_ALLOC(n, double)) || !( g = NAG_ALLOC(n, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } } else { Vprintf("Invalid n.\n"); exit_status = 1; return exit_status; } /* nag_opt_init (e04xxc). * Initialization function for option setting */ nag_opt_init(&options); /* nag_opt_read (e04xyc). * Read options from a text file */ nag_opt_read("e04dgc", "stdin", &options, print, "stdout", &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_read (e04xyc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Set the initial estimate of the solution. */ x[0] = -1.0; x[1] = 1.0; /* Solve the problem. */ /* nag_opt_conj_grad (e04dgc), see above. */ nag_opt_conj_grad(n, objfun, x, &objf, g, &options, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from e0dgc.\n%s\n", fail.message); exit_status = 1; goto END; } END: if (x) NAG_FREE(x); if (g) NAG_FREE(g); return exit_status; } /* ex2 */