/* nag_1d_spline_evaluate (e02bbc) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * * Mark 3 revised, 1994. * Mark 8 revised, 2004. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, j, m, ncap, ncap7, r; Nag_Spline spline; double a, b, s, x; NagError fail; INIT_FAIL(fail); /* Initialise spline */ spline.lamda = 0; spline.c = 0; /* 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_1d_spline_evaluate (e02bbc) Example Program Results\n"); fscanf(fpin, "%*[^\n]"); /* Skip heading in data file */ while (fscanf(fpin, "%ld", &m) != EOF) { if (m <= 0) { fprintf(fpout, "Invalid m.\n"); exit_status = 1; return exit_status; } fscanf(fpin, "%ld", &ncap); ncap7 = ncap+7; if (ncap > 0) { spline.n = ncap7; if (!(spline.c = NAG_ALLOC(ncap7, double)) || !(spline.lamda = NAG_ALLOC(ncap7, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid ncap.\n"); exit_status = 1; return exit_status; } for (j = 0; j < ncap7; j++) fscanf(fpin, "%lf", &(spline.lamda[j])); for (j = 0; j < ncap+3; j++) fscanf(fpin, "%lf", &(spline.c[j])); a = spline.lamda[3]; b = spline.lamda[ncap+3]; fprintf(fpout, "Augmented set of knots stored in spline.lamda:\n"); for (j = 0; j < ncap7; j++) fprintf(fpout, "%10.4f%s", spline.lamda[j], (j%6 == 5 || j == ncap7-1)?"\n":" "); fprintf(fpout, "\nB-spline coefficients stored in spline.c\n\n"); for (j = 0; j < ncap+3; j++) fprintf(fpout, "%10.4f%s", spline.c[j], (j%6 == 5 || j == ncap+2)?"\n":" "); fprintf(fpout, "\n x Value of cubic spline\n\n"); for (r = 1; r <= m; ++r) { x = ((double)(m-r) * a + (double)(r-1) * b) / (double)(m-1); /* nag_1d_spline_evaluate (e02bbc). * Evaluation of fitted cubic spline, function only */ nag_1d_spline_evaluate(x, &s, &spline, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_1d_spline_evaluate (e02bbc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "%10.4f%15.4f\n", x, s); } NAG_FREE(spline.c); NAG_FREE(spline.lamda); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); return exit_status; }