/* nag_example_file_io (x04aec) Example Program. * * Copyright 2011, Numerical Algorithms Group. * * Mark 23, 2011. */ #include #include #include #include #include int main(int argc, char *argv[]) { /* Scalars */ FILE *fpin = 0, *fpout = 0; Integer exit_status = 0; Integer i, j, k, n; double x; NagError fail; /* Arrays */ double *a = 0, *b = 0, *c = 0; INIT_FAIL(fail); /* Check for command-line IO options */ fpout = nag_example_file_io(argc, argv, "-results", NULL); fpin = nag_example_file_io(argc, argv, "-data", NULL); fprintf(fpout, "nag_example_file_io (x04aec) Example Program Results\n"); /* Skip heading in data file*/ fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%"NAG_IFMT "", &n); fscanf(fpin, "%lf", &x); fscanf(fpin, "%*[^\n] "); /* Allocate memory */ if (!(a = NAG_ALLOC(n + 1, double)) || !(b = NAG_ALLOC(n + 1, double)) || !(c = NAG_ALLOC(n * (n + 1)/2, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } for (i = 0; i < n + 1; i++){ fscanf(fpin, "%lf", &a[i]); } fscanf(fpin, "%*[^\n] "); for (i = 0; i < n + 1; i++){ fscanf(fpin, "%lf", &b[i]); } fscanf(fpin, "%*[^\n] "); /* nag_1d_aitken_interp (e01aac). * Interpolated values, Aitken's technique, * unequally spaced data, one variable. */ nag_1d_aitken_interp(n, a, b, c, x, &fail); if (fail.code != NE_NOERROR){ fprintf(fpout, "Error from nag_1d_aitken_interp (e01aac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nInterpolated values\n"); k = 0; for (i = 1; i <= n - 1; i++){ for (j = k; j <= k + n - i; j++){ fprintf(fpout, "%12.5f", c[j]); } fprintf(fpout, "\n"); k = j; } fprintf(fpout, "\nInterpolation point = %12.5f\n", x); fprintf(fpout, "\nFunction value at interpolation point = %12.5f\n", c[n * (n + 1)/2 - 1]); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (c) NAG_FREE(c); return exit_status; }