Integer type:** int32**** int64**** nag_int** show int32 show int32 show int64 show int64 show nag_int show nag_int

nag_opt_lsq_uncon_mod_deriv_comp (e04gd) is a comprehensive modified Gauss–Newton algorithm for finding an unconstrained minimum of a sum of squares of m$m$ nonlinear functions in n$n$ variables (m ≥ n)$(m\ge n)$. First derivatives are required.

The function is intended for functions which have continuous first and second derivatives (although it will usually work even if the derivatives have occasional discontinuities).

Mark 22: liw, lw have been removed from the interface

.nag_opt_lsq_uncon_mod_deriv_comp (e04gd) is essentially identical to the function LSQFDN in the NPL Algorithms Library. It is applicable to problems of the form

where
x
=
(x_{1},x_{2}, … ,x_{n})^{T}
$x={({x}_{1},{x}_{2},\dots ,{x}_{n})}^{\mathrm{T}}$ and m ≥ n$m\ge n$. (The functions f_{i}(x)${f}_{i}\left(x\right)$ are often referred to as ‘residuals’.)

$$\mathrm{Minimize}F\left(x\right)=\sum _{i=1}^{m}{\left[{f}_{i}\left(x\right)\right]}^{2}$$ |

You must supply a function to calculate the values of the f_{i}(x)${f}_{i}\left(x\right)$ and their first derivatives
( ∂ f_{i})/( ∂ x_{j})
$\frac{\partial {f}_{i}}{\partial {x}_{j}}$ at any point x$x$.

From a starting point x^{(1)}${x}^{\left(1\right)}$ supplied by you, the function generates a sequence of points x^{(2)},x^{(3)}, … ${x}^{\left(2\right)},{x}^{\left(3\right)},\dots $, which is intended to converge to a local minimum of F(x)$F\left(x\right)$. The sequence of points is given by

where the vector p^{(k)}${p}^{\left(k\right)}$ is a direction of search, and α^{(k)}${\alpha}^{\left(k\right)}$ is chosen such that F(x^{(k)} + α^{(k)}p^{(k)})$F({x}^{\left(k\right)}+{\alpha}^{\left(k\right)}{p}^{\left(k\right)})$ is approximately a minimum with respect to α^{(k)}${\alpha}^{\left(k\right)}$.

x ^{(k + 1)} = x^{(k)} + α^{(k)}p^{(k)}
$${x}^{(k+1)}={x}^{\left(k\right)}+{\alpha}^{\left(k\right)}{p}^{\left(k\right)}$$ |

The vector p^{(k)}${p}^{\left(k\right)}$ used depends upon the reduction in the sum of squares obtained during the last iteration. If the sum of squares was sufficiently reduced, then p^{(k)}${p}^{\left(k\right)}$ is the Gauss–Newton direction; otherwise finite difference estimates of the second derivatives of the f_{i}(x)${f}_{i}\left(x\right)$ are taken into account.

The method is designed to ensure that steady progress is made whatever the starting point, and to have the rapid ultimate convergence of Newton's method.

Gill P E and Murray W (1978) Algorithms for the solution of the nonlinear least squares problem *SIAM J. Numer. Anal.* **15** 977–992

- 1: m – int64int32nag_int scalar
- The number m$m$ of residuals, f
_{i}(x)${f}_{i}\left(x\right)$, and the number n$n$ of variables, x_{j}${x}_{j}$. - 2: lsqfun – function handle or string containing name of m-file
- lsqfun must calculate the vector of values f
_{i}(x)${f}_{i}\left(x\right)$ and Jacobian matrix of first derivatives ( ∂ f_{i})/( ∂ x_{j}) $\frac{\partial {f}_{i}}{\partial {x}_{j}}$ at any point x$x$. (However, if you do not wish to calculate the residuals or first derivatives at a particular x$x$, there is the option of setting a parameter to cause nag_opt_lsq_uncon_mod_deriv_comp (e04gd) to terminate immediately.)[iflag, fvec, fjac, user] = lsqfun(iflag, m, n, xc, ldfjac, user)**Input Parameters**- 1: iflag – int64int32nag_int scalar
- 2: m – int64int32nag_int scalar
- m$m$, the numbers of residuals.
- 3: n – int64int32nag_int scalar
- n$n$, the numbers of variables.
- 4: xc(n) – double array
- The point x$x$ at which the values of the f
_{i}${f}_{i}$ and the ( ∂ f_{i})/( ∂ x_{j}) $\frac{\partial {f}_{i}}{\partial {x}_{j}}$ are required. - 5: ldfjac – int64int32nag_int scalar
- The first dimension of the array fjac as declared in the (sub)program from which nag_opt_lsq_uncon_mod_deriv_comp (e04gd) is called.
- 6: user – Any MATLAB object
- lsqfun is called from nag_opt_lsq_uncon_mod_deriv_comp (e04gd) with the object supplied to nag_opt_lsq_uncon_mod_deriv_comp (e04gd).

**Output Parameters**- 1: iflag – int64int32nag_int scalar
- If it is not possible to evaluate the f
_{i}(x)${f}_{i}\left(x\right)$ or their first derivatives at the point given in xc (or if it wished to stop the calculations for any other reason), you should reset iflag to some negative number and return control to nag_opt_lsq_uncon_mod_deriv_comp (e04gd). nag_opt_lsq_uncon_mod_deriv_comp (e04gd) will then terminate immediately, with ifail set to your setting of iflag. - 2: fvec(m) – double array
- 3: fjac(ldfjac,n) – double array
- ldfjac ≥ m$\mathit{ldfjac}\ge {\mathbf{m}}$.Unless iflag is reset to a negative number, fjac(i,j)${\mathbf{fjac}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of ( ∂ f
_{i})/( ∂ x_{j}) $\frac{\partial {f}_{\mathit{i}}}{\partial {x}_{\mathit{j}}}$ at the point x$x$, for i = 1,2, … ,m$\mathit{i}=1,2,\dots ,m$ and j = 1,2, … ,n$\mathit{j}=1,2,\dots ,n$. - 4: user – Any MATLAB object

**Note:**lsqfun should be tested separately before being used in conjunction with nag_opt_lsq_uncon_mod_deriv_comp (e04gd). - 3: lsqmon – function handle or string containing name of m-file
- If iprint ≥ 0${\mathbf{iprint}}\ge 0$, you must supply lsqmon which is suitable for monitoring the minimization process. lsqmon must not change the values of any of its parameters.[user] = lsqmon(m, n, xc, fvec, fjac, ldfjac, s, igrade, niter, nf, user)
**Input Parameters**- 1: m – int64int32nag_int scalar
- m$m$, the numbers of residuals.
- 2: n – int64int32nag_int scalar
- n$n$, the numbers of variables.
- 3: xc(n) – double array
- The coordinates of the current point x$x$.
- 4: fvec(m) – double array
- The values of the residuals f
_{i}${f}_{i}$ at the current point x$x$. - 5: fjac(ldfjac,n) – double array
- fjac(i,j)${\mathbf{fjac}}\left(\mathit{i},\mathit{j}\right)$ contains the value of ( ∂ f
_{i})/( ∂ x_{j}) $\frac{\partial {f}_{\mathit{i}}}{\partial {x}_{\mathit{j}}}$ at the current point x$x$, for i = 1,2, … ,m$\mathit{i}=1,2,\dots ,m$ and j = 1,2, … ,n$\mathit{j}=1,2,\dots ,n$. - 6: ldfjac – int64int32nag_int scalar
- The first dimension of the array fjac as declared in the (sub)program from which nag_opt_lsq_uncon_mod_deriv_comp (e04gd) is called.
- 7: s(n) – double array
- The singular values of the current Jacobian matrix. Thus s may be useful as information about the structure of your problem. (If iprint > 0${\mathbf{iprint}}>0$, lsqmon is called at the initial point before the singular values have been calculated. So the elements of s are set to zero for the first call of lsqmon.)
- 8: igrade – int64int32nag_int scalar
- nag_opt_lsq_uncon_mod_deriv_comp (e04gd) estimates the dimension of the subspace for which the Jacobian matrix can be used as a valid approximation to the curvature (see Gill and Murray (1978)). This estimate is called the grade of the Jacobian matrix, and igrade gives its current value.
- 9: niter – int64int32nag_int scalar
- The number of iterations which have been performed in nag_opt_lsq_uncon_mod_deriv_comp (e04gd).
- 10: nf – int64int32nag_int scalar
- 11: user – Any MATLAB object
- lsqmon is called from nag_opt_lsq_uncon_mod_deriv_comp (e04gd) with the object supplied to nag_opt_lsq_uncon_mod_deriv_comp (e04gd).

**Output Parameters****Note:**you should normally print the sum of squares of residuals, so as to be able to examine the sequence of values of F(x)$F\left(x\right)$ mentioned in Section [Accuracy]. It is usually also helpful to print xc, the gradient of the sum of squares, niter and nf. - 4: xtol – double scalar
- The accuracy in x$x$ to which the solution is required.If x
_{true}${x}_{\mathrm{true}}$ is the true value of x$x$ at the minimum, then x_{sol}${x}_{\mathrm{sol}}$, the estimated position before a normal exit, is such thatwhere ‖y‖ = sqrt( ∑$$\Vert {x}_{\mathrm{sol}}-{x}_{\mathrm{true}}\Vert <{\mathbf{xtol}}\times (1.0+\Vert {x}_{\mathrm{true}}\Vert )$$_{j = 1}^{n}y_{j}^{2})$\Vert y\Vert =\sqrt{{\displaystyle \sum _{j=1}^{n}}{y}_{j}^{2}}$. For example, if the elements of x_{sol}${x}_{\mathrm{sol}}$ are not much larger than 1.0$1.0$ in modulus and if xtol = 1.0e−5${\mathbf{xtol}}=\text{1.0e\u22125}$, then x_{sol}${x}_{\mathrm{sol}}$ is usually accurate to about five decimal places. (For further details see Section [Accuracy].)If F(x)$F\left(x\right)$ and the variables are scaled roughly as described in Section [Further Comments] and ε$\epsilon $ is the machine precision, then a setting of order xtol = sqrt(ε)${\mathbf{xtol}}=\sqrt{\epsilon}$ will usually be appropriate. If xtol is set to 0.0$0.0$ or some positive value less than 10ε$10\epsilon $, nag_opt_lsq_uncon_mod_deriv_comp (e04gd) will use 10ε$10\epsilon $ instead of xtol, since 10ε$10\epsilon $ is probably the smallest reasonable setting. - 5: x(n) – double array
- n, the dimension of the array, must satisfy the constraint 1 ≤ n ≤ m$1\le {\mathbf{n}}\le {\mathbf{m}}$.x(j)${\mathbf{x}}\left(\mathit{j}\right)$ must be set to a guess at the j$\mathit{j}$th component of the position of the minimum, for j = 1,2, … ,n$\mathit{j}=1,2,\dots ,n$.

- 1: n – int64int32nag_int scalar
- The number m$m$ of residuals, f
_{i}(x)${f}_{i}\left(x\right)$, and the number n$n$ of variables, x_{j}${x}_{j}$. - 2: iprint – int64int32nag_int scalar
- The frequency with which lsqmon is to be called.
- iprint > 0${\mathbf{iprint}}>0$
- lsqmon is called once every iprint iterations and just before exit from nag_opt_lsq_uncon_mod_deriv_comp (e04gd).
- iprint = 0${\mathbf{iprint}}=0$
- lsqmon is just called at the final point.
- iprint < 0${\mathbf{iprint}}<0$
- lsqmon is not called at all.

iprint should normally be set to a small positive number.*Default*: 1$1$ - 3: maxcal – int64int32nag_int scalar
- Enables you to limit the number of times that lsqfun is called by nag_opt_lsq_uncon_mod_deriv_comp (e04gd). There will be an error exit (see Section [Error Indicators and Warnings]) after maxcal evaluations of the residuals (i.e., calls of lsqfun with iflag set to 2$2$). It should be borne in mind that, in addition to the calls of lsqfun which are limited directly by maxcal, there will be calls of lsqfun (with iflag set to 1$1$) to evaluate only first derivatives.
- 4: eta – double scalar
- Every iteration of nag_opt_lsq_uncon_mod_deriv_comp (e04gd) involves a linear minimization, i.e., minimization of F(x
^{(k)}+ α^{(k)}p^{(k)})$F({x}^{\left(k\right)}+{\alpha}^{\left(k\right)}{p}^{\left(k\right)})$ with respect to α^{(k)}${\alpha}^{\left(k\right)}$. eta specifies how accurately these linear minimizations are to be performed. The minimum with respect to α^{(k)}${\alpha}^{\left(k\right)}$ will be located more accurately for small values of eta (say, 0.01$0.01$) than for large values (say, 0.9$0.9$).*Default*:- if n = 1${\mathbf{n}}=1$, 0.0$0.0$;
- otherwise 0.5$0.5$.

- 5: stepmx – double scalar
- An estimate of the Euclidean distance between the solution and the starting point supplied by you. (For maximum efficiency, a slight overestimate is preferable.) nag_opt_lsq_uncon_mod_deriv_comp (e04gd) will ensure that, for each iteration,where k$k$ is the iteration number. Thus, if the problem has more than one solution, nag_opt_lsq_uncon_mod_deriv_comp (e04gd) is most likely to find the one nearest to the starting point. On difficult problems, a realistic choice can prevent the sequence of x
n ∑ (x _{j}^{(k)}− x_{j}^{(k − 1)})^{2}≤ (stepmx)^{2}j = 1 $$\sum _{j=1}^{n}{({x}_{j}^{\left(k\right)}-{x}_{j}^{(k-1)})}^{2}\le {\left({\mathbf{stepmx}}\right)}^{2}$$^{(k)}${x}^{\left(k\right)}$ entering a region where the problem is ill-behaved and can help avoid overflow in the evaluation of F(x)$F\left(x\right)$. However, an underestimate of stepmx can lead to inefficiency.*Default*: 100000.0$100000.0$ - 6: user – Any MATLAB object

- ldfjac ldv iw liw w lw

- 1: x(n) – double array
- 2: fsumsq – double scalar
- The value of F(x)$F\left(x\right)$, the sum of squares of the residuals f
_{i}(x)${f}_{i}\left(x\right)$, at the final point given in x. - 3: fvec(m) – double array
- The value of the residual f
_{i}(x)${f}_{\mathit{i}}\left(x\right)$ at the final point given in x, for i = 1,2, … ,m$\mathit{i}=1,2,\dots ,m$. - 4: fjac(ldfjac,n) – double array
- ldfjac ≥ m$\mathit{ldfjac}\ge {\mathbf{m}}$.The value of the first derivative ( ∂ f
_{i})/( ∂ x_{j}) $\frac{\partial {f}_{\mathit{i}}}{\partial {x}_{\mathit{j}}}$ evaluated at the final point given in x, for i = 1,2, … ,m$\mathit{i}=1,2,\dots ,m$ and j = 1,2, … ,n$\mathit{j}=1,2,\dots ,n$. - 5: s(n) – double array
- The singular values of the Jacobian matrix at the final point. Thus s may be useful as information about the structure of your problem.
- 6: v(ldv,n) – double array
- ldv ≥ n$\mathit{ldv}\ge {\mathbf{n}}$.The matrix V$V$ associated with the singular value decompositionof the Jacobian matrix at the final point, stored by columns. This matrix may be useful for statistical purposes, since it is the matrix of orthonormalized eigenvectors of JJ = USV
^{T}$$J=US{V}^{\mathrm{T}}$$^{T}J${J}^{\mathrm{T}}J$. - 7: niter – int64int32nag_int scalar
- The number of iterations which have been performed in nag_opt_lsq_uncon_mod_deriv_comp (e04gd).
- 8: nf – int64int32nag_int scalar
- 9: user – Any MATLAB object
- 10: ifail – int64int32nag_int scalar
- ifail = 0${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

Errors or warnings detected by the function:

Cases prefixed with `W` are classified as warnings and
do not generate an error of type NAG:error_*n*. See nag_issue_warnings.

`W`ifail < 0${\mathbf{ifail}}<0$

On entry, n < 1${\mathbf{n}}<1$, or m < n${\mathbf{m}}<{\mathbf{n}}$, or maxcal < 1${\mathbf{maxcal}}<1$, or eta < 0.0${\mathbf{eta}}<0.0$, or eta ≥ 1.0${\mathbf{eta}}\ge 1.0$, or xtol < 0.0${\mathbf{xtol}}<0.0$, or stepmx < xtol${\mathbf{stepmx}}<{\mathbf{xtol}}$, or ldfjac < m$\mathit{ldfjac}<{\mathbf{m}}$, or ldv < n$\mathit{ldv}<{\mathbf{n}}$, or liw < 1$\mathit{liw}<1$, or lw < 7 × n + m × n + 2 × m + n × n$\mathit{lw}<7\times {\mathbf{n}}+{\mathbf{m}}\times {\mathbf{n}}+2\times {\mathbf{m}}+{\mathbf{n}}\times {\mathbf{n}}$ when n > 1${\mathbf{n}}>1$, or lw < 9 + 3 × m$\mathit{lw}<9+3\times {\mathbf{m}}$ when n = 1${\mathbf{n}}=1$.

- There have been maxcal evaluations of the residuals. If steady reductions in the sum of squares, F(x)$F\left(x\right)$, were monitored up to the point where this exit occurred, then the exit probably occurred simply because maxcal was set too small, so the calculations should be restarted from the final point held in x. This exit may also indicate that F(x)$F\left(x\right)$ has no minimum.

`W`ifail = 3${\mathbf{ifail}}=3$- The conditions for a minimum have not all been satisfied, but a lower point could not be found. This could be because xtol has been set so small that rounding errors in the evaluation of the residuals and derivatives make attainment of the convergence conditions impossible.

- The method for computing the singular value decomposition of the Jacobian matrix has failed to converge in a reasonable number of sub-iterations. It may be worth applying nag_opt_lsq_uncon_mod_deriv_comp (e04gd) again starting with an initial approximation which is not too close to the point at which the failure occurred.

The values ifail = 2${\mathbf{ifail}}={\mathbf{2}}$, 3${\mathbf{3}}$ or 4${\mathbf{4}}$ may also be caused by mistakes in lsqfun, by the formulation of the problem or by an awkward function. If there are no such mistakes it is worth restarting the calculations from a different starting point (not the point at which the failure occurred) in order to avoid the region which caused the failure.

A successful exit (ifail = 0${\mathbf{ifail}}={\mathbf{0}}$) is made from nag_opt_lsq_uncon_mod_deriv_comp (e04gd) when the matrix of approximate second derivatives of F(x)$F\left(x\right)$ is positive definite, and when (B1, B2 and B3) or B4 or B5 hold, where

and where ‖ . ‖$\Vert .\Vert $ and ε$\epsilon $ are as defined in xtol, and F^{(k)}${F}^{\left(k\right)}$ and g^{(k)}${g}^{\left(k\right)}$ are the values of F(x)$F\left(x\right)$ and its vector of estimated first derivatives at x^{(k)}${x}^{\left(k\right)}$.

$$\begin{array}{lll}\mathrm{B1}& \equiv & {\alpha}^{\left(k\right)}\times \Vert {p}^{\left(k\right)}\Vert <({\mathbf{xtol}}+\epsilon )\times (1.0+\Vert {x}^{\left(k\right)}\Vert )\\ \mathrm{B2}& \equiv & |{F}^{\left(k\right)}-{F}^{(k-1)}|<{({\mathbf{xtol}}+\epsilon )}^{2}\times (1.0+{F}^{\left(k\right)})\\ \mathrm{B3}& \equiv & \Vert {g}^{\left(k\right)}\Vert <{\epsilon}^{1/3}\times (1.0+{F}^{\left(k\right)})\\ \mathrm{B4}& \equiv & {F}^{\left(k\right)}<{\epsilon}^{2}\\ \mathrm{B5}& \equiv & \Vert {g}^{\left(k\right)}\Vert <{(\epsilon \times \sqrt{{F}^{\left(k\right)}})}^{1/2}\end{array}$$ |

If ifail = 0${\mathbf{ifail}}={\mathbf{0}}$ then the vector in x on exit, x_{sol}${x}_{\mathrm{sol}}$, is almost certainly an estimate of x_{true}${x}_{\mathrm{true}}$, the position of the minimum to the accuracy specified by xtol.

If ifail = 3${\mathbf{ifail}}={\mathbf{3}}$, then x_{sol}${x}_{\mathrm{sol}}$ may still be a good estimate of x_{true}${x}_{\mathrm{true}}$, but to verify this you should make the following checks. If

(a) | the sequence {F(x^{(k)})}$\left\{F\left({x}^{\left(k\right)}\right)\right\}$ converges to F(x_{sol})$F\left({x}_{\mathrm{sol}}\right)$ at a superlinear or a fast linear rate, and |

(b) | g(x_{sol})^{T}g(x_{sol}) < 10ε$g{\left({x}_{\mathrm{sol}}\right)}^{\mathrm{T}}g\left({x}_{\mathrm{sol}}\right)<10\epsilon $, where T$\mathrm{T}$ denotes transpose, then it is almost certain that x_{sol}${x}_{\mathrm{sol}}$ is a close approximation to the minimum. |

When (b) is true, then usually F(x_{sol})$F\left({x}_{\mathrm{sol}}\right)$ is a close approximation to F(x_{true})$F\left({x}_{\mathrm{true}}\right)$. The values of F(x^{(k)})$F\left({x}^{\left(k\right)}\right)$ can be calculated in lsqmon, and the vector g(x_{sol})$g\left({x}_{\mathrm{sol}}\right)$ can be calculated from the contents of fvec and fjac on exit from nag_opt_lsq_uncon_mod_deriv_comp (e04gd).

Further suggestions about confirmation of a computed solution are given in the E04 Chapter Introduction.

The number of iterations required depends on the number of variables, the number of residuals, the behaviour of F(x)$F\left(x\right)$, the accuracy demanded and the distance of the starting point from the solution. The number of multiplications performed per iteration of nag_opt_lsq_uncon_mod_deriv_comp (e04gd) varies, but for m ≫ n$m\gg n$ is approximately n × m^{2} + O(n^{3})$n\times {m}^{2}+\mathit{O}\left({n}^{3}\right)$. In addition, each iteration makes at least one call of lsqfun. So, unless the residuals and their derivatives can be evaluated very quickly, the run time will be dominated by the time spent in lsqfun.

Ideally, the problem should be scaled so that, at the solution, F(x)$F\left(x\right)$ and the corresponding values of the x_{j}${x}_{j}$ are each in the range ( − 1, + 1)$(-1,+1)$, and so that at points one unit away from the solution, F(x)$F\left(x\right)$ differs from its value at the solution by approximately one unit. This will usually imply that the Hessian matrix of F(x)$F\left(x\right)$ at the solution is well-conditioned. It is unlikely that you will be able to follow these recommendations very closely, but it is worth trying (by guesswork), as sensible scaling will reduce the difficulty of the minimization problem, so that nag_opt_lsq_uncon_mod_deriv_comp (e04gd) will take less computer time.

When the sum of squares represents the goodness-of-fit of a nonlinear model to observed data, elements of the variance-covariance matrix of the estimated regression coefficients can be computed by a subsequent call to nag_opt_lsq_uncon_covariance (e04yc), using information returned in the arrays s and v. See nag_opt_lsq_uncon_covariance (e04yc) for further details.

Open in the MATLAB editor: nag_opt_lsq_uncon_mod_deriv_comp_example

function nag_opt_lsq_uncon_mod_deriv_comp_examplem = int64(15); xtol = 1.05418557512311e-07; x = [0.5; 1; 1.5]; y=[0.14,0.18,0.22,0.25,0.29,0.32,0.35,0.39,0.37,0.58,0.73,0.96,1.34,2.10,4.39]; t = [1.0, 15.0, 1.0; 2.0, 14.0, 2.0; 3.0, 13.0, 3.0; 4.0, 12.0, 4.0; 5.0, 11.0, 5.0; 6.0, 10.0, 6.0; 7.0, 9.0, 7.0; 8.0, 8.0, 8.0; 9.0, 7.0, 7.0; 10.0, 6.0, 6.0; 11.0, 5.0, 5.0; 12.0, 4.0, 4.0; 13.0, 3.0, 3.0; 14.0, 2.0, 2.0; 15.0, 1.0, 1.0]; user = {y; t}; [xOut, fsumsq, fvec, fjac, s, v, niter, nf, user, ifail] = ... nag_opt_lsq_uncon_mod_deriv_comp(m, @lsqfun, @lsqmon, xtol, x, 'user', user)function [iflag, fvecc, fjacc, user] = lsqfun(iflag, m, n, xc, ljc, user)y = user{1}; t = user{2}; fvecc = zeros(m, 1); fjacc = zeros(ljc, n); for i = 1:double(m) denom = xc(2)*t(i,2) + xc(3)*t(i,3); if (iflag ~= 1) fvecc(i) = xc(1) + t(i,1)/denom - y(i); end if (iflag ~= 0) fjacc(i,1) = 1; dummy = -1/(denom*denom); fjacc(i,2) = t(i,1)*t(i,2)*dummy; fjacc(i,3) = t(i,1)*t(i,3)*dummy; end endfunction [user] = ... lsqmon(m, n, xc, fvecc, fjacc, ljc, s, igrade, niter, nf, user)

xOut = 0.0824 1.1330 2.3437 fsumsq = 0.0082 fvec = -0.0059 -0.0003 0.0003 0.0065 -0.0008 -0.0013 -0.0045 -0.0200 0.0822 -0.0182 -0.0148 -0.0147 -0.0112 -0.0042 0.0068 fjac = 1.0000 -0.0401 -0.0027 1.0000 -0.0663 -0.0095 1.0000 -0.0824 -0.0190 1.0000 -0.0910 -0.0303 1.0000 -0.0941 -0.0428 1.0000 -0.0931 -0.0558 1.0000 -0.0890 -0.0692 1.0000 -0.0827 -0.0827 1.0000 -0.1064 -0.1064 1.0000 -0.1379 -0.1379 1.0000 -0.1820 -0.1820 1.0000 -0.2482 -0.2482 1.0000 -0.3585 -0.3585 1.0000 -0.5791 -0.5791 1.0000 -1.2409 -1.2409 s = 4.0965 1.5950 0.0613 v = -0.9354 0.3530 0.0214 0.2592 0.6432 0.7205 0.2405 0.6795 -0.6932 niter = 5 nf = 10 user = [ 1x15 double] [15x3 double] ifail = 0

Open in the MATLAB editor: e04gd_example

function e04gd_examplem = int64(15); xtol = 1.05418557512311e-07; x = [0.5; 1; 1.5]; y=[0.14,0.18,0.22,0.25,0.29,0.32,0.35,0.39,0.37,0.58,0.73,0.96,1.34,2.10,4.39]; t = [1.0, 15.0, 1.0; 2.0, 14.0, 2.0; 3.0, 13.0, 3.0; 4.0, 12.0, 4.0; 5.0, 11.0, 5.0; 6.0, 10.0, 6.0; 7.0, 9.0, 7.0; 8.0, 8.0, 8.0; 9.0, 7.0, 7.0; 10.0, 6.0, 6.0; 11.0, 5.0, 5.0; 12.0, 4.0, 4.0; 13.0, 3.0, 3.0; 14.0, 2.0, 2.0; 15.0, 1.0, 1.0]; user = {y; t}; [xOut, fsumsq, fvec, fjac, s, v, niter, nf, user, ifail] = ... e04gd(m, @lsqfun, @lsqmon, xtol, x, 'user', user)function [iflag, fvecc, fjacc, user] = lsqfun(iflag, m, n, xc, ljc, user)y = user{1}; t = user{2}; fvecc = zeros(m, 1); fjacc = zeros(ljc, n); for i = 1:double(m) denom = xc(2)*t(i,2) + xc(3)*t(i,3); if (iflag ~= 1) fvecc(i) = xc(1) + t(i,1)/denom - y(i); end if (iflag ~= 0) fjacc(i,1) = 1; dummy = -1/(denom*denom); fjacc(i,2) = t(i,1)*t(i,2)*dummy; fjacc(i,3) = t(i,1)*t(i,3)*dummy; end endfunction [user] = ... lsqmon(m, n, xc, fvecc, fjacc, ljc, s, igrade, niter, nf, user)

xOut = 0.0824 1.1330 2.3437 fsumsq = 0.0082 fvec = -0.0059 -0.0003 0.0003 0.0065 -0.0008 -0.0013 -0.0045 -0.0200 0.0822 -0.0182 -0.0148 -0.0147 -0.0112 -0.0042 0.0068 fjac = 1.0000 -0.0401 -0.0027 1.0000 -0.0663 -0.0095 1.0000 -0.0824 -0.0190 1.0000 -0.0910 -0.0303 1.0000 -0.0941 -0.0428 1.0000 -0.0931 -0.0558 1.0000 -0.0890 -0.0692 1.0000 -0.0827 -0.0827 1.0000 -0.1064 -0.1064 1.0000 -0.1379 -0.1379 1.0000 -0.1820 -0.1820 1.0000 -0.2482 -0.2482 1.0000 -0.3585 -0.3585 1.0000 -0.5791 -0.5791 1.0000 -1.2409 -1.2409 s = 4.0965 1.5950 0.0613 v = -0.9354 0.3530 0.0214 0.2592 0.6432 0.7205 0.2405 0.6795 -0.6932 niter = 5 nf = 10 user = [ 1x15 double] [15x3 double] ifail = 0

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013