NAG Library Manual, Mark 30.2
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_fit_dim1_cheb_deriv (e02ahc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

#include <nag.h>
#include <stdio.h>

int main(void) {
  /* Initialized data */
  const double xmin = -0.5;
  const double xmax = 2.5;
  const double a[7] = {2.53213, 1.13032, 0.2715, 0.04434,
                       0.00547, 5.4e-4,  4e-5};

  /* Scalars */
  double deriv, deriv2, patm1, x;
  Integer exit_status, i, m, n, one;
  NagError fail;

  /* Arrays */
  double *adif = 0, *adif2 = 0;

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_fit_dim1_cheb_deriv (e02ahc) Example Program Results\n");

  n = 6;
  one = 1;

  /* Allocate memory */
  if (!(adif = NAG_ALLOC(n + 1, double)) ||
      !(adif2 = NAG_ALLOC(n + 1, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* nag_fit_dim1_cheb_deriv (e02ahc).
   * Derivative of fitted polynomial in Chebyshev series form
   */
  nag_fit_dim1_cheb_deriv(n, xmin, xmax, a, one, &patm1, adif, one, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_fit_dim1_cheb_deriv (e02ahc) call 1.\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_fit_dim1_cheb_deriv (e02ahc), see above. */
  nag_fit_dim1_cheb_deriv(n, xmin, xmax, adif, one, &patm1, adif2, one, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_fit_dim1_cheb_deriv (e02ahc) call 2.\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  m = 4;
  printf("\n");
  printf("   i  Argument    1st deriv    2nd deriv\n");

  for (i = 1; i <= m; ++i) {
    x = (xmin * (double)(m - i) + xmax * (double)(i - 1)) / (double)(m - 1);
    /* nag_fit_dim1_cheb_eval2 (e02akc).
     * Evaluation of fitted polynomial in one variable from
     * Chebyshev series form
     */
    nag_fit_dim1_cheb_eval2(n, xmin, xmax, adif, one, x, &deriv, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_fit_dim1_cheb_eval2 (e02akc) call 1.\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
    /* nag_fit_dim1_cheb_eval2 (e02akc), see above. */
    nag_fit_dim1_cheb_eval2(n, xmin, xmax, adif2, one, x, &deriv2, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_fit_dim1_cheb_eval2 (e02akc) call 2.\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
    printf("%4" NAG_IFMT "%9.4f    %9.4f    %9.4f    \n", i, x, deriv, deriv2);
  }

END:
  NAG_FREE(adif);
  NAG_FREE(adif2);

  return exit_status;
}