/* nag_opt_lsq_check_deriv (e04yac) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 7 revised, 2001. * Mark 8 revised, 2004. * */ #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void lsqfun(Integer m, Integer n, double x[], double fvec[], double fjac[], Integer tdfjac, Nag_Comm *comm); #ifdef __cplusplus } #endif #define Y(I) comm.user[I] #define T(I,J) comm.user[(I)*n + (J) + m] #define YC(I) comm->user[(I)] #define TC(I,J) comm->user[(I)*n + (J) + m] #define FJAC(I,J) fjac[(I)*tdfjac + (J)] int main(void) { Integer exit_status=0, i, j, m, n, tdfjac; NagError fail; Nag_Comm comm; double *fjac=0, *fvec=0, *work=0, *x=0; INIT_FAIL(fail); Vprintf("nag_opt_lsq_check_deriv (e04yac) Example Program Results\n"); Vscanf(" %*[^\n]"); /* Skip heading in data file */ n = 3; m = 15; if (n>=1 && m>=1 && n<=m) { if ( !( fjac = NAG_ALLOC(m*n, double)) || !( fvec = NAG_ALLOC(m, double)) || !( x = NAG_ALLOC(n, double)) || !( work = NAG_ALLOC(m + m*n, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tdfjac = n; } else { Vprintf("Invalid n or m.\n"); exit_status = 1; return exit_status; } /* Allocate memory to communication array */ comm.user = work; /* Observations t (j = 0, 1, 2) are held in T(i, j) * (i = 0, 1, 2, . . ., 14) */ for (i = 0; i < m; ++i) { Vscanf("%lf", &Y(i)); for (j = 0; j < n; ++j) Vscanf("%lf", &T(i,j)); } /* Set up an arbitrary point at which to check the 1st derivatives */ x[0] = 0.19; x[1] = -1.34; x[2] = 0.88; Vprintf("\nThe test point is "); for (j = 0; j < n; ++j) Vprintf(" %9.3e", x[j]); Vprintf("\n"); /* nag_opt_lsq_check_deriv (e04yac). * Least-squares derivative checker for use with * nag_opt_lsq_deriv (e04gbc) */ nag_opt_lsq_check_deriv(m, n, lsqfun, x, fvec, fjac, tdfjac, &comm, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_lsq_check_deriv (e04yac).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\nDerivatives are consistent with residual values.\n"); Vprintf("\nAt the test point, lsqfun() gives\n\n"); Vprintf(" Residuals 1st derivatives\n"); for (i = 0; i < m; ++i) { Vprintf(" %9.3e ", fvec[i]); for (j = 0; j < n; ++j) Vprintf(" %9.3e", FJAC(i,j)); Vprintf("\n"); } END: if (fjac) NAG_FREE(fjac); if (fvec) NAG_FREE(fvec); if (x) NAG_FREE(x); if (work) NAG_FREE(work); return exit_status; } static void lsqfun(Integer m, Integer n, double x[], double fvec[], double fjac[], Integer tdfjac, Nag_Comm *comm) { /* Function to evaluate the residuals and their 1st derivatives. */ Integer i; double denom, dummy; for (i = 0; i < m; ++i) { denom = x[1]*TC(i,1) + x[2]*TC(i,2); if (comm->flag != 1) fvec[i] = x[0] + TC(i,0)/denom - YC(i); if (comm->flag != 0) { FJAC(i,0) = 1.0; dummy = -1.0 / (denom * denom); FJAC(i,1) = TC(i,0)*TC(i,1)*dummy; FJAC(i,2) = TC(i,0)*TC(i,2)*dummy; } } } /* lsqfun */