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

NAG CL Interface Introduction
Example description
/* nag_roots_contfn_interval_rcomm (c05avc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.2, 2023.
 */

#include <math.h>
#include <nag.h>
#include <stdio.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_roots_contfn_interval_rcomm (c05avc) Example Program Results\n");

  x = 3.0;
  h = 0.1;
  boundl = 0.0;
  boundu = 4.0;
  ind = 1;
  fx = 0.0;
  /* nag_roots_contfn_interval_rcomm (c05avc).
   * Locates an interval containing a simple zero of a continuous
   * function using binary search and reverse communication.
   */
  while (ind != 0) {
    nag_roots_contfn_interval_rcomm(&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;
}