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

NAG CL Interface Introduction
Example description
/* nag_roots_contfn_cntin_rcomm (c05axc) 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;
  int i;
  double fx, tol, x, scal;
  Integer ind;
  Nag_ErrorControl ir;
  /* Arrays */
  double c[26];
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_roots_contfn_cntin_rcomm (c05axc) Example Program Results\n");

  scal = sqrt(nag_machine_precision);
  ir = Nag_Mixed;

  for (i = 3; i <= 4; i++) {
    tol = pow(10.0, -i);
    printf("\ntol = %13.4e\n\n", tol);
    x = 1.0;
    ind = 1;
    fx = 0.0;

    /* nag_roots_contfn_cntin_rcomm (c05axc).
     * Locates a zero of a continuous function.
     * Reverse communication.
     */

    while (ind != 0) {
      nag_roots_contfn_cntin_rcomm(&x, fx, tol, ir, scal, c, &ind, &fail);
      if (ind != 0)
        fx = x - exp(-x);
    }

    if (fail.code == NE_NOERROR) {
      printf("Root is %14.5f\n", x);
    } else {
      printf("Error from nag_roots_contfn_cntin_rcomm (c05axc) %s\n",
             fail.message);

      if (fail.code == NE_CONTIN_PROB_NOT_SOLVED ||
          fail.code == NE_FINAL_PROB_NOT_SOLVED) {
        printf("Final value = %14.5f, theta = %10.2f\n", x, c[4]);
      }

      exit_status = 1;
      goto END;
    }
  }

END:

  return exit_status;
}