/* nag_smooth_spline_fit (g10abc) Example Program. * * Copyright 2000 Numerical Algorithms Group. * * Mark 6a revised, 2001. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; 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); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_smooth_spline_fit (g10abc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%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))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } fscanf(fpin, "%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); fscanf(fpin, "%s", nag_enum_arg); weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%lf", &rho); if (!weight) { for (i = 1; i <= n; ++i) fscanf(fpin, "%lf %lf ", &x[i - 1], &y[i - 1]); wtptr = 0; } else { for (i = 1; i <= n; ++i) fscanf(fpin, "%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) { fprintf(fpout, "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) { fprintf(fpout, "Error from nag_smooth_spline_fit (g10abc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print results */ fprintf(fpout, "\n"); fprintf(fpout, "%s%10.3f\n", " rho = ", rho); fprintf(fpout, "\n"); fprintf(fpout, "%s%10.3f\n", " Residual sum of squares = ", rss); fprintf(fpout, "%s%10.3f\n", " Degrees of freedom = ", df); fprintf(fpout, "\n"); fprintf(fpout, "%s\n", " Ordered input data Output results"); fprintf(fpout, "\n"); fprintf(fpout, "%s\n", " x y Fitted Values"); fprintf(fpout, "\n"); for (i = 1; i <= nord; ++i) { fprintf(fpout, "%8.4f %8.4f %8.4f\n", xord[i - 1], yord[i - 1], yhat[i - 1]); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); 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; }