## edu.rit.numeric Class NonLinearLeastSquares

```java.lang.Object
edu.rit.numeric.NonLinearLeastSquares
```

`public class NonLinearLeastSquaresextends Object`

Class NonLinearLeastSquares provides a method for minimizing the sum of the squares of a series of nonlinear functions. There are M functions, each of which has N inputs. These functions are represented by an object that implements interface VectorFunction. The solve() method finds a vector x such that Σi [fi(x)]2 is minimized. The inputs to and outputs from the solve() method are stored in the fields of an instance of class NonLinearLeastSquares. The Levenberg-Marquardt method is used to find the solution.

The Java code is a translation of the Fortran subroutine LMDER from the MINPACK library. MINPACK was developed by Jorge Moré, Burt Garbow, and Ken Hillstrom at Argonne National Laboratory. For further information, see http://www.netlib.org/minpack/.

Field Summary
` VectorFunction` `fcn`
The nonlinear functions fi(x) to be minimized.
` double[][]` `fjac`
The M×N-element Jacobian matrix.
` double[]` `fvec`
The M-element result vector.
` int` `info`
` int[]` `ipvt`
The N-element permutation vector for fjac.
` int` `M`
The number of functions.
` int` `N`
The number of arguments for each function.
` int` `nprint`
Debug printout flag.
` double` `tol`
Tolerance.
` double[]` `x`
The N-element x vector for the least squares problem.

Constructor Summary
`NonLinearLeastSquares(VectorFunction theFunction)`
Construct a new nonlinear least squares problem for the given functions.

Method Summary
` void` `solve()`
Solve this nonlinear least squares minimization problem.
`protected  void` `subclassDebug(int iter)`
Print debugging information.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### fcn

`public final VectorFunction fcn`
The nonlinear functions fi(x) to be minimized.

### M

`public final int M`
The number of functions.

### N

`public final int N`
The number of arguments for each function.

### x

`public final double[] x`
The N-element x vector for the least squares problem. On input to the solve() method, x contains an initial estimate of the solution vector. On output from the solve() method, x contains the final solution vector.

### fvec

`public final double[] fvec`
The M-element result vector. On output from the solve() method, for i = 0 to M−1, fveci = fi(x), where x is the final solution vector.

### fjac

`public final double[][] fjac`
The M×N-element Jacobian matrix. On output from the solve() method, the upper N×N submatrix of fjac contains an upper triangular matrix R with diagonal elements of nonincreasing magnitude such that

PT (JT JP = RT R

where P is a permutation matrix and J is the final calculated Jacobian. Column j of P is column ipvt[j] (see below) of the identity matrix. The lower trapezoidal part of fjac contains information generated during the computation of R.

fjac is used to calculate the covariance matrix of the solution. This calculation is not yet implemented.

### ipvt

`public final int[] ipvt`
The N-element permutation vector for fjac. On output from the solve() method, ipvt defines a permutation matrix P such that JP = QR, where J is the final calculated Jacobian, Q is orthogonal (not stored), and R is upper triangular with diagonal elements of nonincreasing magnitude. Column j of P is column ipvt[j] of the identity matrix.

### tol

`public double tol`
Tolerance. An input to the solve() method. Must be >= 0. Termination occurs when the algorithm estimates either that the relative error in the sum of squares is at most tol or that the relative error between x and the solution is at most tol. The default tolerance is 1×10−6.

### info

`public int info`
Information about the outcome. An output of the solve() method. The possible values are:
• 0 -- Improper input parameters. Also, an IllegalArgumentException is thrown.
• 1 -- Algorithm estimates that the relative error in the sum of squares is at most tol.
• 2 -- Algorithm estimates that the relative error between x and the solution is at most tol.
• 3 -- Conditions for info = 1 and info = 2 both hold.
• 4 -- fvec is orthogonal to the columns of the Jacobian to machine precision.
• 5 -- Number of function evaluations has reached 100(N+1). Also, a TooManyIterationsException is thrown.
• 6 -- tol is too small. No further reduction in the sum of squares is possible.
• 7 -- tol is too small. No further improvement in the approximate solution x is possible.

### nprint

`public int nprint`
Debug printout flag. An input to the solve() method. If nprint > 0, the subclassDebug() method is called at the beginning of the first iteration and every nprint iterations thereafter and immediately prior to return. If nprint <= 0, the subclassDebug() method is not called. The default setting is 0.

Constructor Detail

### NonLinearLeastSquares

`public NonLinearLeastSquares(VectorFunction theFunction)`
Construct a new nonlinear least squares problem for the given functions. Field fcn is set to theFunction. Fields M and N are set by calling the resultLength() and argumentLength() methods of theFunction. The vector and matrix fields x, fvec, fjac, and ipvt are allocated with the proper sizes but are not filled in.

Parameters:
`theFunction` - Nonlinear functions to be minimized.
Throws:
`NullPointerException` - (unchecked exception) Thrown if theFunction is null.
`IllegalArgumentException` - (unchecked exception) Thrown if M <= 0, N <= 0, or M < N.
Method Detail

### solve

`public void solve()`
Solve this nonlinear least squares minimization problem. The solve() method finds a vector x such that Σi [fi(x)]2 is minimized. On input, the field x must be filled in with an initial estimate of the solution vector, and the field tol must be set to the desired tolerance. On output, the other fields are filled in with the solution as explained in the documentation for each field.

Throws:
`IllegalArgumentException` - (unchecked exception) Thrown if tol <= 0.
`TooManyIterationsException` - (unchecked exception) Thrown if too many iterations occurred without finding a minimum (100(N+1) iterations).

### subclassDebug

`protected void subclassDebug(int iter)`
Print debugging information. If the nprint field is greater than zero, the subclassDebug() method is called at the beginning of the first iteration and every nprint iterations thereafter and immediately prior to return. The fields of this object contain the current state of the algorithm. The fields of this object must not be altered.

The default implementation of the subclassDebug() method does nothing. A subclass can override the subclassDebug() method to do something, such as print debugging information.

Parameters:
`iter` - Iteration number.