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

NAG CL Interface Introduction
Example description
/* nag_roots_contfn_brent_rcomm (c05azc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.0, 2023.
 */

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

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  double fx, tolx, x, y;
  Integer ind;
  Nag_ErrorControl ir;
  /* Arrays */
  double c[17];
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_roots_contfn_brent_rcomm (c05azc) Example Program Results\n");
  printf("\n Iterations\n");

  tolx = 1e-05;
  x = 0.0;
  y = 1.0;
  ir = Nag_Mixed;
  ind = 1;
  fx = 0.0;
  /* nag_roots_contfn_brent_rcomm (c05azc).
   * Locates a simple zero of a continuous function.
   * Reverse communication.
   */
  while (ind != 0) {
    nag_roots_contfn_brent_rcomm(&x, &y, fx, tolx, ir, c, &ind, &fail);

    if (ind != 0) {
      fx = exp(-x) - x;
      printf(" x = %8.5f   fx = %13.4e   ind = %2" NAG_IFMT "\n", x, fx, ind);
    }
  }

  if (fail.code == NE_NOERROR) {
    printf("\n Solution\n");
    printf(" x = %8.5f   y = %8.5f\n", x, y);
  } else {
    printf("%s\n", fail.message);
    if (fail.code == NE_PROBABLE_POLE ||
        fail.code == NW_TOO_MUCH_ACC_REQUESTED) {
      printf(" x = %8.5f   y = %8.5f\n", x, y);
    }
    exit_status = 1;
    goto END;
  }

END:
  return exit_status;
}