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

NAG AD Library Introduction
Example description
/* nag::ad::c05au Passive Example Program.
 */

#include <dco.hpp>
#include <iostream>
#include <nagad.h>

// Function which calls NAG AD Library routines.
// Finds the zero point f(xv) = 0 for the function
// f(x) = x - exp(-r*x) using an initial approximation of xv.
template <typename T> void func(T r, T &x);

int main()
{
  std::cout << " nag::ad::c05au Passive Example Program Results\n";

  // Set problem parameters
  double rv = 1.0;
  // Solution x
  double xv;

  // Call the NAG AD Lib functions
  func(rv, xv);

  // Print outputs
  std::cout.setf(std::ios::scientific, std::ios::floatfield);
  std::cout.precision(12);
  std::cout << "\n Solution:\n";
  std::cout << " x = " << xv << std::endl;

  return 0;
}

// Function which calls NAG AD Library routines.
// Finds the zero point f(xv) = 0 for the function
// f(x) = x - exp(-r*x) using an initial approximation of xv.
template <typename T> void func(T r, T &x)
{
  // Active variables
  T a, b, h = 0.1;
  T eps = 1.0e-5, eta = 0.0;
  // Initial guess to the zero
  dco::passive_value(x) = 1.0;
  // Create AD configuration data object
  Integer           ifail = 0;
  nag::ad::handle_t ad_handle;

  // Call routine for computing an approximation to a simple zero of the
  // continuous function f using an initial approximation.
  ifail = 0;
  nag::ad::c05au(ad_handle, x, h, eps, eta, 
                [&](nag::ad::handle_t const& handle, T const& x, T & z) {
                  z = x - exp(-r * x);
                },
                a, b, ifail);
}