/* nag_monotonic_interpolant (e01bec) Example Program. * * Copyright 1990 Numerical Algorithms Group * * Mark 2 revised, 1992. * Mark 8 revised, 2004. */ #include #include #include #include int main(void) { Integer exit_status=0, i, m, n, r; NagError fail; double *d=0, *f=0, *pf=0, *px=0, step, *x=0; INIT_FAIL(fail); Vprintf("nag_monotonic_interpolant (e01bec) Example Program Results\n"); Vscanf("%*[^\n]"); /* Skip to end of line */ Vscanf("%ld",&n); if (n>=2) { if ( !( d = NAG_ALLOC(n, double)) || !( f = NAG_ALLOC(n, double)) || !( x = NAG_ALLOC(n, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } } else { Vprintf("Invalid n.\n"); exit_status = 1; return exit_status; } for (r = 0; r < n; r++) Vscanf("%lf%lf",&x[r],&f[r]); /* Abort on error in nag_monotonic_interpolant (e01bec) */ /* nag_monotonic_interpolant (e01bec). * Interpolating function, monotonicity-preserving, * piecewise cubic Hermite, one variable */ nag_monotonic_interpolant(n, x, f, d, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_monotonic_interpolant (e01bec).\n%s\n", fail.message); exit_status = 1; goto END; } Vscanf("%ld",&m); if (m>=1) { if ( !( pf = NAG_ALLOC(m, double)) || !( px = NAG_ALLOC(m, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } } else { Vprintf("Invalid m.\n"); exit_status = 1; return exit_status; } /* Compute M equally spaced points from x[0] to x[n-1]. */ step = (x[n-1] - x[0]) / (double)(m-1); for (i = 0; i < m; i++) px[i] = MIN(x[0]+ i*step,x[n-1]); /* nag_monotonic_evaluate (e01bfc). * Evaluation of interpolant computed by * nag_monotonic_interpolant (e01bec), function only */ nag_monotonic_evaluate(n, x, f, d, m, px, pf, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_monotonic_evaluate (e01bfc).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf(" Interpolated\n"); Vprintf(" Abscissa Value\n"); for (i = 0; i < m; i++) Vprintf("%13.4f%13.4f\n", px[i], pf[i]); END: if (d) NAG_FREE(d); if (f) NAG_FREE(f); if (pf) NAG_FREE(pf); if (px) NAG_FREE(px); if (x) NAG_FREE(x); return exit_status; }