/* nag_smooth_spline_estim (g10acc) Example Program. * * Copyright 2000 Numerical Algorithms Group. * * Mark 6a revised, 2001. */ #include #include #include #include int main (void) { char method[2], weight[2]; double *coeff=0, crit, df, *h=0, *res=0, rho, rss, tol, u, *weights=0, *wtptr; double *wwt=0, *x=0, *xord=0, *y=0, *yhat=0, *yord=0; Integer i, maxcal, n, nord; Integer exit_status=0; NagError fail; Nag_SmoothParamMethods method_enum; INIT_FAIL(fail); Vprintf("g10acc Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf("%ld", &n); if (!(x = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)) || !(weights = NAG_ALLOC(n, double)) || !(yhat = NAG_ALLOC(n, double)) || !(coeff = NAG_ALLOC((n-1)*3, double)) || !(res = NAG_ALLOC(n, double)) || !(h = NAG_ALLOC(n, double)) || !(wwt = NAG_ALLOC(n, double)) || !(yord = NAG_ALLOC(n, double)) || !(xord = NAG_ALLOC(n, double))) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } Vscanf(" %s %s ", method, weight); if (*method == 'C') method_enum = Nag_SmoothParamCV; else if (*method == 'G') method_enum = Nag_SmoothParamGCV; else if (*method == 'D') method_enum = Nag_SmoothParamDF; else method_enum = (Nag_SmoothParamMethods)-999; if (*weight == 'U') { for (i = 1; i <= n; ++i) Vscanf("%lf %lf", &x[i - 1], &y[i - 1]); wtptr = 0; } else { for (i = 1; i <= n; ++i) Vscanf("%lf %lf %lf", &x[i - 1], &y[i - 1], &weights[i - 1]); wtptr = weights; } Vscanf("%lf %lf %ld %lf", &u, &tol, &maxcal, &crit); /* Sort data, removing ties and weighting accordingly */ g10zac(n, x, y, wtptr, &nord, xord, yord, wwt, &rss, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from g10zac.\n%s\n", fail.message); exit_status = 1; goto END; } /* Fit cubic spline */ g10acc(method_enum, nord, xord, yord, wwt, yhat, coeff, &rss, &df, res, h, &crit, &rho, u, tol, maxcal, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from g10acc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Print results */ Vprintf("\n"); Vprintf("%s%10.2f\n", " Residual sum of squares = ", rss); Vprintf("%s%10.2f\n"," Degrees of freedom = ", df); Vprintf("%s%10.2f\n", " rho = ", rho); Vprintf("\n"); Vprintf("\n%s%s%s\n%s%s%s\n", " Input data", " ", "Output results", " i x y ", " ", "yhat h"); for (i = 1; i <= nord; ++i) Vprintf("%4ld %8.3f %8.3f %8.3f %8.3f\n", i, xord[i - 1], yord[i - 1], yhat[i - 1], h[i - 1]); END: if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (weights) NAG_FREE(weights); if (yhat) NAG_FREE(yhat); if (coeff) NAG_FREE(coeff); if (res) NAG_FREE(res); if (h) NAG_FREE(h); if (wwt) NAG_FREE(wwt); if (yord) NAG_FREE(yord); if (xord) NAG_FREE(xord); return exit_status; }