```/* nag_opt_lsq_no_deriv (e04fcc) Example Program.
*
* Copyright 2017 Numerical Algorithms Group.
*
* Mark 26.2, 2017.
*
*/

#include <nag.h>
#include <stdio.h>
#include <string.h>
#include <nag_stdlib.h>
#include <math.h>
#include <nage04.h>
#include <nagf16.h>
#include <nagx02.h>

#ifdef __cplusplus
extern "C"
{
#endif
static void NAG_CALL lsqfun(Integer m, Integer n, const double x[],
double fvec[], Nag_Comm *comm);
static void NAG_CALL lsqgrd(Integer m, Integer n, double *fvec, double *fjac,
Integer ldfjac, double *g);
#ifdef __cplusplus
}
#endif

#define MMAX 15
#define TMAX 3

/* Define a user structure template to store data in lsqfun. */
struct user
{
double y[MMAX];
double t[MMAX][TMAX];
};

int main(void)
{
const char *optionsfile = "e04fcce.opt";
Integer exit_status = 0;
Nag_Boolean print;
Integer i, j, m, n, nt, tdfjac;
Nag_Comm comm;
Nag_E04_Opt options;
double *fjac = 0, fsumsq, *fvec = 0, *x = 0, *g = 0;
struct user s;
NagError fail;

INIT_FAIL(fail);

printf("nag_opt_lsq_no_deriv (e04fcc) Example Program Results\n");
fflush(stdout);
scanf(" %*[^\n]"); /* Skip heading in data file */
n = 3;
m = 15;
if (m >= 1 && n <= m) {
if (!(fjac = NAG_ALLOC(m * n, double)) ||
!(fvec = NAG_ALLOC(m, double)) ||
!(x = NAG_ALLOC(n, double)) ||
!(g = NAG_ALLOC(n, double)))
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
tdfjac = n;
}
else {
printf("Invalid m or n.\n");
exit_status = 1;
return exit_status;
}

/* Read data into structure.
* Observations t (j = 0, 1, 2) are held in s->t[i][j]
* (i = 0, 1, 2, . . .,  14)
*/
nt = 3;
for (i = 0; i < m; ++i) {
scanf("%lf", &s.y[i]);
for (j = 0; j < nt; ++j)
scanf("%lf", &s.t[i][j]);
}

/* Set up the starting point */
x = 0.5;
x = 1.0;
x = 1.5;

/* nag_opt_init (e04xxc).
* Initialization function for option setting
*/
nag_opt_init(&options); /* Initialize options structure */
/* Set one option directly. */
/* nag_machine_precision (x02ajc).
* The machine precision
*/
options.optim_tol = 10.0 * sqrt(nag_machine_precision);

/* Read remaining option values from file */
print = Nag_FALSE;
/* nag_opt_read (e04xyc).
* Read options from a text file
*/
nag_opt_read("e04fcc", optionsfile, &options, print, "stdout", &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_opt_read (e04xyc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}

/* Assign address of user defined structure to
* comm.p for communication to lsqfun().
*/
comm.p = (Pointer) &s;

/* nag_opt_lsq_no_deriv (e04fcc), see above. */
nag_opt_lsq_no_deriv(m, n, lsqfun, x, &fsumsq, fvec, fjac, tdfjac,
&options, &comm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error/Warning from nag_opt_lsq_no_deriv (e04fcc).\n%s\n",
fail.message);
if (fail.code != NW_COND_MIN)
exit_status = 1;
}

if (fail.code == NE_NOERROR || fail.code == NW_COND_MIN)
{
printf("On exit, the sum of squares is %12.4f\n", fsumsq);
printf("at the point");
for (i=0; i<n; i++)
printf("%12.4f", x[i]);
printf("\n");

lsqgrd(m,n,fvec,fjac,tdfjac,g);
printf("The estimated gradient is");
for (i=0; i<n; i++)
printf("%13.4e", g[i]);
printf("\n");
printf("                           (machine dependent)\n");
printf("and the residuals are\n");
for (i=0; i<m; i++)
printf("%9.1e\n", fvec[i]);
}

/* Free memory allocated to pointers s and v */
/* nag_opt_free (e04xzc).
* Memory freeing function for use with option setting
*/
nag_opt_free(&options, "all", &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_opt_free (e04xzc).\n%s\n", fail.message);
exit_status = 2;
goto END;
}
END:
NAG_FREE(fjac);
NAG_FREE(fvec);
NAG_FREE(x);
NAG_FREE(g);

return exit_status;
}

static void NAG_CALL lsqfun(Integer m, Integer n, const double x[],
double fvec[], Nag_Comm *comm)
{
/* Function to evaluate the residuals.
*
* To avoid the use of a global varibale this example assigns the address
* of a user defined structure to comm.p in the main program (where the
* data was also read in).
* The address of this structure is recovered in each call to lsqfun()
* from comm->p and the structure used in the calculation of the residuals.
*/

Integer i;
struct user *s = (struct user *) comm->p;

for (i = 0; i < m; ++i)
fvec[i] = x + s->t[i] /
(x * s->t[i] + x * s->t[i]) - s->y[i];
} /* lsqfun */

static void NAG_CALL lsqgrd(Integer m, Integer n, double *fvec, double *fjac,
Integer ldfjac, double *g)
{
/* Function to evaluate gradient of the sum of squares */
NagError fail;
Integer i;
INIT_FAIL(fail);
nag_dgemv(Nag_RowMajor,Nag_Trans,m,n,1.0,fjac,ldfjac,fvec,1,0.0,g,1,&fail);
for (i=0; i<n; i++)
g[i] = 2.0*g[i];
return;
}
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.5331109,"math_prob":0.95985794,"size":4532,"snap":"2021-21-2021-25","text_gpt3_token_len":1506,"char_repetition_ratio":0.121024735,"word_repetition_ratio":0.08454811,"special_character_ratio":0.37555164,"punctuation_ratio":0.25239617,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98969024,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-22T16:15:13Z\",\"WARC-Record-ID\":\"<urn:uuid:01bdae7c-d505-4649-bea1-0a4935f21741>\",\"Content-Length\":\"28454\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b80b641f-3589-4344-a8cd-2b467d8feb7d>\",\"WARC-Concurrent-To\":\"<urn:uuid:abb85123-8d47-4f3a-876e-3dd6f118f97e>\",\"WARC-IP-Address\":\"78.129.168.4\",\"WARC-Target-URI\":\"https://www.nag.com/numeric/cl/nagdoc_cl26.2/examples/source/e04fcce.c.html\",\"WARC-Payload-Digest\":\"sha1:XYDDHOWM6DFCYHX6XMZAZKQ2XUY3KL7R\",\"WARC-Block-Digest\":\"sha1:A6KJ6TQCM6GQYSFQ3MJWJX3TNAO7DJUR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623488519183.85_warc_CC-MAIN-20210622155328-20210622185328-00514.warc.gz\"}"}