/* 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; double df, rho, rss; double *coeff = 0, *comm_ar = 0, *h = 0, *res = 0, *weights = 0; double *wtptr, *wwt = 0, *x = 0, *xord = 0, *y = 0, *yhat = 0; double *yord = 0; char nag_enum_arg[40]; Nag_SmoothFitType mode; Nag_Boolean weight; NagError fail; INIT_FAIL(fail); printf("nag_smooth_spline_fit (g10abc) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n]"); scanf("%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))) { printf("Allocation failure\n"); exit_status = -1; goto END; } scanf("%s", nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ mode = (Nag_SmoothFitType) nag_enum_name_to_value(nag_enum_arg); scanf("%s", nag_enum_arg); weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg); scanf("%lf", &rho); if (!weight) { for (i = 1; i <= n; ++i) scanf("%lf %lf ", &x[i - 1], &y[i - 1]); wtptr = 0; } else { for (i = 1; i <= n; ++i) scanf("%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) { printf("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, nord, xord, yord, wwt, rho, yhat, coeff, &rss, &df, res, h, comm_ar, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_smooth_spline_fit (g10abc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print results */ printf("\n"); printf("%s%10.3f\n", " rho = ", rho); printf("\n"); printf("%s%10.3f\n", " Residual sum of squares = ", rss); printf("%s%10.3f\n", " Degrees of freedom = ", df); printf("\n"); printf("%s\n", " Ordered input data Output results"); printf("\n"); printf("%s\n", " x y Fitted Values"); printf("\n"); for (i = 1; i <= nord; ++i) { printf("%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; }