/* nag_1d_spline_interpolant (e01bac) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * * Mark 6 revised, 2000. * Mark 8 revised, 2004. */ #include #include #include #include #include #include #include #define MMAX 7 int main(int argc, char *argv[]) { FILE *fpout; Integer exit_status = 0, i, j, m = MMAX; NagError fail; Nag_Spline spline; double fit, *x = 0, xarg, *y = 0; INIT_FAIL(fail); /* Initialise spline */ spline.lamda = 0; spline.c = 0; /* Check for command-line IO options */ fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_1d_spline_interpolant (e01bac) Example Program Results\n"); if (m >= 1) { if (!(y = NAG_ALLOC(m, double)) || !(x = NAG_ALLOC(m, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { exit_status = 1; return exit_status; } x[0] = 0.0; x[1] = 0.2; x[2] = 0.4; x[3] = 0.6; x[4] = 0.75; x[5] = 0.9; x[6] = 1.0; for (i = 0; i < m; ++i) y[i] = exp(x[i]); /* nag_1d_spline_interpolant (e01bac). * Interpolating function, cubic spline interpolant, one * variable */ nag_1d_spline_interpolant(m, x, y, &spline, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_1d_spline_interpolant (e01bac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nNumber of distinct knots = %ld\n\n", m-2); fprintf(fpout, "Distinct knots located at \n\n"); for (j = 3; j < m+1; j++) fprintf(fpout, "%8.4f%s", spline.lamda[j], (j-3)%5 == 4 || j == m?"\n":" "); fprintf(fpout, "\n\n J B-spline coeff c\n\n"); for (j = 0; j < m; ++j) fprintf(fpout, " %ld %13.4f\n", j+1, spline.c[j]); fprintf(fpout, "\n J Abscissa Ordinate Spline\n\n"); for (j = 0; j < m; ++j) { /* nag_1d_spline_evaluate (e02bbc). * Evaluation of fitted cubic spline, function only */ nag_1d_spline_evaluate(x[j], &fit, &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, " %ld %13.4f %13.4f %13.4f\n", j+1, x[j], y[j], fit); if (j < m-1) { xarg = (x[j] + x[j+1]) * 0.5; /* nag_1d_spline_evaluate (e02bbc), see above. */ nag_1d_spline_evaluate(xarg, &fit, &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, " %13.4f %13.4f\n", xarg, fit); } } /* Free memory allocated by nag_1d_spline_interpolant (e01bac) */ END: if (fpout != stdout) fclose(fpout); if (y) NAG_FREE(y); if (x) NAG_FREE(x); NAG_FREE(spline.lamda); NAG_FREE(spline.c); return exit_status; }