/* nag_zero_nonlin_eqns_easy (c05qbc) Example Program. * * Copyright 2011 Numerical Algorithms Group. * * Mark 23, 2011. */ #include #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void NAG_CALL fcn(Integer n, const double x[], double fvec[], Nag_Comm *comm, Integer *iflag); #ifdef __cplusplus } #endif int main(void) { Integer exit_status = 0, i, j, n = 9; NagError fail; Nag_Comm comm; double *fvec = 0, *x = 0, xtol; INIT_FAIL(fail); printf("nag_zero_nonlin_eqns_easy (c05qbc) Example Program Results\n"); if (n > 0) { if (!(fvec = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } } else { printf("Invalid n.\n"); exit_status = 1; goto END; } /* The following starting values provide a rough solution. */ for (j = 0; j < n; j++) x[j] = -1.0; /* nag_machine_precision (x02ajc). * The machine precision */ xtol = sqrt(nag_machine_precision); /* nag_zero_nonlin_eqns_easy (c05qbc). * Solution of a system of nonlinear equations (function * values only) */ nag_zero_nonlin_eqns_easy(fcn, n, x, fvec, xtol, &comm, &fail); if (fail.code == NE_NOERROR) { printf("Final approximate solution\n\n"); for (j = 0; j < n; j++) printf("%12.4f%s", x[j], (j%3 == 2 || j == n-1)?"\n":" "); } else { printf("Error from nag_zero_nonlin_eqns_easy (c05qbc).\n%s\n", fail.message); if (fail.code == NE_TOO_MANY_FEVALS || fail.code == NE_TOO_SMALL || fail.code == NE_NO_IMPROVEMENT) { printf("Approximate solution\n\n"); for (i = 0; i < n; i++) printf("%12.4f%s", x[i], (i%3 == 2 || i == n-1)?"\n":" "); exit_status = 2; } } END: if (fvec) NAG_FREE(fvec); if (x) NAG_FREE(x); return exit_status; } static void NAG_CALL fcn(Integer n, const double x[], double fvec[], Nag_Comm *comm, Integer *iflag) { Integer k; for (k = 0; k < n; ++k) { fvec[k] = (3.0-x[k]*2.0)*x[k]+1.0; if (k > 0) fvec[k] -= x[k-1]; if (k < n-1) fvec[k] -= x[k+1]*2.0; } }