/* nag_ode_bvp_ps_lin_grid_vals (d02uwc) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.1, 2024.
*/
#include <math.h>
#include <nag.h>
#ifdef __cplusplus
extern "C" {
#endif
static double NAG_CALL exact(double x);
#ifdef __cplusplus
}
#endif
int main(void) {
/* Scalars */
Integer exit_status = 0;
Integer i, n, nip;
double a = -1.0, b = 1.0;
double uerr = 0.0;
double teneps = 10.0 * nag_machine_precision;
/* Arrays */
double *f = 0, *fip = 0, *x = 0, *xip = 0;
/* NAG types */
Nag_Boolean reqerr = Nag_FALSE;
NagError fail;
INIT_FAIL(fail);
printf("nag_ode_bvp_ps_lin_grid_vals (d02uwc) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n] ");
scanf("%" NAG_IFMT "%*[^\n] ", &n);
scanf("%" NAG_IFMT "%*[^\n] ", &nip);
if (!(f = NAG_ALLOC((n + 1), double)) || !(fip = NAG_ALLOC((nip), double)) ||
!(xip = NAG_ALLOC((nip), double)) || !(x = NAG_ALLOC((n + 1), double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Set up solution grid:
* nag_ode_bvp_ps_lin_cgl_grid (d02ucc).
* Generate Chebyshev Gauss-Lobatto grid.
*/
nag_ode_bvp_ps_lin_cgl_grid(n, a, b, x, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_ode_bvp_ps_lin_cgl_grid (d02ucc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Set up problem right hand sides for grid. */
for (i = 0; i < n + 1; i++)
f[i] = exact(x[i]);
/* Map to an equally spaced grid:
* nag_ode_bvp_ps_lin_grid_vals (d02uwc).
* Interpolate a function from Chebyshev grid to uniform grid
* using barycentric Lagrange interpolation.
*/
nag_ode_bvp_ps_lin_grid_vals(n, nip, x, f, xip, fip, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_ode_bvp_ps_lin_grid_vals (d02uwc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Print solution. */
printf("Numerical solution f\n\n");
printf(" x f\n");
for (i = 0; i < nip; i++)
printf("%10.4f %10.4f\n", xip[i], fip[i]);
if (reqerr) {
for (i = 0; i < nip; i++)
uerr = MAX(uerr, fabs(fip[i] - exact(xip[i])));
printf("f is within a multiple %" NAG_IFMT " of machine precision.\n",
10 * ((Integer)(uerr / teneps) + 1));
}
END:
NAG_FREE(f);
NAG_FREE(fip);
NAG_FREE(x);
NAG_FREE(xip);
return exit_status;
}
static double NAG_CALL exact(double x) { return x + cos(5.0 * x); }