/* nag_zero_cont_func_brent_rcomm (c05azc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 9, 2009.
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <math.h>
#include <nagc05.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_zero_cont_func_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_zero_cont_func_brent_rcomm (c05azc).
   * Locates a simple zero of a continuous function.
   * Reverse communication.
   */
  while (ind != 0)
    {
      nag_zero_cont_func_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 = %2ld\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;
}