/* nag_smooth_spline_fit (g10abc) Example Program. * * Copyright 2000 Numerical Algorithms Group. * * Mark 6a revised, 2001. */ #include #include #include #include int main (void) { Integer exit_status=0, i, n, nord; NagError fail; Nag_SmoothFitType mode_enum; char mode[2], weight[2]; double *coeff=0, *comm_ar=0, df, *h=0, *res=0, rho, rss, *weights=0, *wtptr; double *wwt=0, *x=0, *xord=0, *y=0, *yhat=0, *yord=0; INIT_FAIL(fail); Vprintf("nag_smooth_spline_fit (g10abc) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf("%ld", &n); if (!(coeff = NAG_ALLOC((n-1)*3, double)) || !(h = NAG_ALLOC(n, double)) || !(res = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)) || !(weights = NAG_ALLOC(n, double)) || !(xord = NAG_ALLOC(n, double)) || !(yord = NAG_ALLOC(n, double)) || !(wwt = NAG_ALLOC(n, double)) || !(yhat = NAG_ALLOC(n, double)) || !(comm_ar = NAG_ALLOC(9*n+14, double))) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } Vscanf(" %s %s ", mode, weight); if (*mode == 'P') mode_enum = Nag_SmoothFitPartial; else if (*mode == 'Q') mode_enum = Nag_SmoothFitQuick; else if (*mode == 'F') mode_enum = Nag_SmoothFitFull; else mode_enum = (Nag_SmoothFitType)-999; Vscanf("%lf", &rho); 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; } /* Sort data into increasing X and */ /* remove tied observations and weight accordingly */ /* nag_order_data (g10zac). * Reorder data to give ordered distinct observations */ nag_order_data(n, x, y, wtptr, &nord, xord, yord, wwt, &rss, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_order_data (g10zac).\n%s\n", fail.message); exit_status = 1; goto END; } /* Fit cubic spline */ /* nag_smooth_spline_fit (g10abc). * Fit cubic smoothing spline, smoothing parameter given */ nag_smooth_spline_fit(mode_enum, nord, xord, yord, wwt, rho, yhat, coeff, &rss, &df, res, h, comm_ar, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_smooth_spline_fit (g10abc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print results */ Vprintf("\n"); Vprintf("%s%10.3f\n", " rho = ", rho); Vprintf("\n"); Vprintf("%s%10.3f\n", " Residual sum of squares = ", rss); Vprintf("%s%10.3f\n", " Degrees of freedom = ", df); Vprintf("\n"); Vprintf("%s\n", " Ordered input data Output results"); Vprintf("\n"); Vprintf("%s\n", " x y Fitted Values"); Vprintf("\n"); for (i = 1; i <= nord; ++i) { Vprintf("%8.4f %8.4f %8.4f\n", xord[i - 1], yord[i - 1], yhat[i - 1]); } END: if (coeff) NAG_FREE(coeff); if (h) NAG_FREE(h); if (res) NAG_FREE(res); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (weights) NAG_FREE(weights); if (xord) NAG_FREE(xord); if (yord) NAG_FREE(yord); if (wwt) NAG_FREE(wwt); if (yhat) NAG_FREE(yhat); if (comm_ar) NAG_FREE(comm_ar); return exit_status; }