/* nag_zero_cont_func_cntin_rcomm (c05axc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <math.h>
#include <nagc05.h>
#include <nagx02.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_zero_cont_func_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_zero_cont_func_cntin_rcomm (c05axc).
     * Locates a zero of a continuous function.
     * Reverse communication.
     */

    while (ind != 0) {
      nag_zero_cont_func_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_zero_cont_func_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;
}