/* C05QS_P0W_F C++ Header Example Program.
*
* Copyright 2022 Numerical Algorithms Group.
* Mark 28.7, 2022.
*/
#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <nagx02.h>
#include <nagx04.h>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
// Scalars
int exit_status = 0;
const Integer n = 7, lrcomm = 12 + 3 * n, licomm = 8 * n + 19 + 3 * n;
cout << "C05QS_P0W_F C++ Header Example Program Results\n\n";
// problem parameters and starting value
double ruser[5], x[7];
ruser[0] = -1.0;
ruser[1] = 3.0;
ruser[2] = -2.0;
ruser[3] = -2.0;
ruser[4] = -1.0;
for (int i = 0; i < n; ++i)
{
x[i] = -1.0;
}
Integer ifail = 0;
nag::ad::handle_t ad_handle;
double fvec[n], rcomm[lrcomm], xtol;
Integer icomm[licomm];
logical init = Nag_TRUE;
xtol = sqrt(X02AJC);
auto fcn = [&](nag::ad::handle_t & ad_handle,
const Integer & n,
const Integer & lindf,
const Integer indf[],
const double *x,
double *fvec,
Integer & iflag)
{
for (int ind = 0; ind < lindf; ++ind)
{
int i = indf[ind] - 1;
fvec[i] = (ruser[1] + ruser[2] * x[i]) * x[i] - ruser[4];
if (i > 0)
fvec[i] = fvec[i] + ruser[0] * x[i - 1];
if (i < n - 1)
fvec[i] = fvec[i] + ruser[3] * x[i + 1];
}
iflag = 0;
};
nag::ad::c05qs(ad_handle, fcn, n, x, fvec, xtol, init, rcomm, lrcomm, icomm, licomm, ifail);
cout.setf(ios::scientific, ios::floatfield);
cout.precision(4);
cout << " Solution:\n";
for (int i = 0; i < n; ++i)
{
cout.width(10);
cout << i + 1;
cout.width(20);
cout << x[i] << endl;
}
return exit_status;
}