# NAG Library Function Document

## 1Purpose

nag_opt_handle_set_nlnls (e04rmc) is a part of the NAG optimization modelling suite and defines the number of residuals in a sum of squares objective function (nonlinear least squares problems) and, optionally, the sparsity structure of their first derivatives.

## 2Specification

 #include #include
 void nag_opt_handle_set_nlnls (void *handle, Integer nres, Integer isparse, Integer nnzrd, const Integer irowrd[], const Integer icolrd[], NagError *fail)

## 3Description

After the initialization function nag_opt_handle_init (e04rac) has been called and unless the objective function has already been defined, nag_opt_handle_set_nlnls (e04rmc) may be used to declare the objective function of the optimization problem as a sum of squares. It will typically be used in data fitting or calibration problems of the form
 $minimize x∈ℝn fx= ∑ j=1 mr rj x 2 subject to lx≤x≤ux ,$
where $x$ is an $n$-dimensional variable vector and ${r}_{i}\left(x\right)$ are nonlinear residuals (see Section 2.2.3 in the e04 Chapter Introduction). The values of the residuals, and possibly their derivatives, will be communicated to the solver by a user-supplied function. nag_opt_handle_set_nlnls (e04rmc) also allows the user to declare the structured first derivative matrix
 $∂rjx ∂xi i=1,…,n , ​ j=1,…,mr$
as being dense or sparse. If declared as sparse, its sparsity structure must be specified here.
See nag_opt_handle_init (e04rac) for more details.

None.

## 5Arguments

1:    $\mathbf{handle}$void *Input
On entry: the handle to the problem. It needs to be initialized by nag_opt_handle_init (e04rac) and must not be changed.
2:    $\mathbf{nres}$IntegerInput
On entry: ${m}_{r}$, the number of residuals in the objective function.
If ${\mathbf{nres}}=0$, no objective function will be defined and irowrd and icolrd will not be referenced and may be NULL.
Constraint: ${\mathbf{nres}}\ge 0$.
3:    $\mathbf{isparse}$IntegerInput
On entry: is a flag indicating if the nonzero structure of the first derivative matrix is dense or sparse.
${\mathbf{isparse}}=0$
The first derivative matrix is considered dense and irowrd and icolrd will not be referenced and may be specified as NULL. The ordering is assumed to be column-wise, namely the function will behave as if ${\mathbf{nnzrd}}=n×{m}_{r}$ and the vectors irowrd and icolrd filled as:
• ${\mathbf{irowrd}}=\left(1,2,\dots ,n,1,2,\dots ,n,\dots ,1,2,\dots ,n\right)$;
• ${\mathbf{icolrd}}=\left(1,1,\dots ,1,2,2,\dots ,2,\dots ,{m}_{r},{m}_{r},\dots ,{m}_{r}\right)$.
${\mathbf{isparse}}=1$
The sparsity structure of the first derivative matrix will be supplied by nnzrd, irowrd and icolrd.
Constraint: ${\mathbf{isparse}}=0$ or $1$.
4:    $\mathbf{nnzrd}$IntegerInput
On entry: the number of nonzeros in the first derivative matrix.
Constraint: if ${\mathbf{nres}}>0$, ${\mathbf{nnzrd}}>0$.
5:    $\mathbf{irowrd}\left[{\mathbf{nnzrd}}\right]$const IntegerInput
6:    $\mathbf{icolrd}\left[{\mathbf{nnzrd}}\right]$const IntegerInput
On entry: arrays irowrd and icolrd store the sparsity structure (pattern) of the first derivative matrix as nnzrd nonzeros in coordinate storage (CS) format (see Section 2.1.1 in the f11 Chapter Introduction). The matrix has dimensions $n×{m}_{r}$. irowrd specifies one-based row indices and icolrd specifies one-based column indices. No particular order of elements is expected, but elements should not repeat and the same order should be used when the first derivative matrix is evaluated for the solver.
Constraints:
• $1\le {\mathbf{irowrd}}\left[\mathit{l}-1\right]\le n$, for $\mathit{l}=1,2,\dots ,{\mathbf{nnzrd}}$;
• $1\le {\mathbf{icolrd}}\left[\mathit{l}-1\right]\le {\mathbf{nres}}$, for $\mathit{l}=1,2,\dots ,{\mathbf{nnzrd}}$.
7:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
NE_ALREADY_DEFINED
The objective function has already been defined.
NE_BAD_PARAM
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_HANDLE
The supplied handle does not define a valid handle to the data structure for the NAG optimization modelling suite. It has not been initialized by nag_opt_handle_init (e04rac) or it has been corrupted.
NE_INT
On entry, ${\mathbf{isparse}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{isparse}}=0$ or $1$.
On entry, ${\mathbf{nnzrd}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nnzrd}}>0$.
On entry, ${\mathbf{nres}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nres}}\ge 0$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_INVALID_CS
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{icolrd}}\left[\mathit{i}-1\right]=〈\mathit{\text{value}}〉$ and ${\mathbf{nres}}=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{icolrd}}\left[\mathit{i}-1\right]\le {\mathbf{nres}}$.
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{irowrd}}\left[\mathit{i}-1\right]=〈\mathit{\text{value}}〉$ and $n=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{irowrd}}\left[\mathit{i}-1\right]\le n$.
On entry, more than one element of first derivative matrix has row index $〈\mathit{\text{value}}〉$ and column index $〈\mathit{\text{value}}〉$.
Constraint: each element of first derivative matrix must have a unique row and column index.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
NE_PHASE
The Hessians of nonlinear functions have already been defined, a nonlinear objective cannot be added.
The problem cannot be modified in this phase any more, the solver has already been called.

Not applicable.

## 8Parallelism and Performance

nag_opt_handle_set_nlnls (e04rmc) is not threaded in any implementation.

None.

## 10Example

In this example, we demonstrate how to declare a least squares problem through nag_opt_handle_set_nlnls (e04rmc) and solve it with nag_opt_handle_solve_dfls (e04ffc) on a very simple example. Here $n=2$, ${m}_{r}=3$ and the residuals are computed by:
 $r1x = x1+ x2- 0.9 r2x = 2x1+ x2- 1.9 r3x = 3x1+ x2- 3.0$
The expected result is:
 $x=0.95,0.10$
with an objective value of $0.015$.

### 10.1Program Text

Program Text (e04rmce.c)

None.

### 10.3Program Results

Program Results (e04rmce.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017