/* nag_interval_zero_cont_func (c05avc) 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>

int main(void)
{
  /* Scalars */
  Integer exit_status = 0;
  double boundl, boundu, fx, h, x, y;
  Integer ind;
  /* Arrays */
  double c[11];
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_interval_zero_cont_func (c05avc) Example Program Results\n");

  x = 3.0;
  h = 0.1;
  boundl = 0.0;
  boundu = 4.0;
  ind = 1;
  fx = 0.0;
  /* nag_interval_zero_cont_func (c05avc).
   * Locates an interval containing a simple zero of a continuous
   * function using binary search and reverse communication.
   */
  while (ind != 0) {
    nag_interval_zero_cont_func(&x, fx, &h, boundl, boundu, &y, c, &ind,
                                &fail);

    if (ind != 0)
      fx = pow(x, 2) - 3.0 * x + 2.0;
  }

  if (fail.code == NE_NOERROR) {
    printf("Interval containing root is [x,y], where\n");
    printf("x = %12.4f, y = %12.4f\n", x, y);
    printf("Values of f at x and y are\n");
    printf("f(x) = %12.2f, f(y) = %12.2f\n", fx, c[0]);
  }
  else {
    printf("%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

END:
  return exit_status;
}