# 2.9.2 - Least Squares Refinement - JdlLeastSquares

John W. Campbell

## 2.9.2.1 Introduction

This class provides methods for a non-linear least squares refinement using a version of the Levenberg-Marquardt algorithm.

Class, fields and methods:

Class Details
Accessible Fields
Refinement Methods
Curve Fitting

## 2.9.2.2 Class Details

Package:
Jdl.JdlLib;
Class name:
JdlLeastSquares
Class definition:
public class JdlLeastSquares
Extends:
Object
Implements:
none
Actions:
none

## 2.9.2.3 Accessible Fields

No fields with public, package or protected access defined.

## 2.9.2.4 Refinement Methods

### 2.9.2.4.1 Introduction

This section contains methods to carry out a non-linear least squares parameter refinement and to calculate sigma values following such a refinement.

Methods:

Least squares refinement - leastSquaresMQL
Calculate sigma values - leastSquaresSigmas

### 2.9.2.4.2 Least squares refinement - leastSquaresMQL

This method uses a version Levenberg-Marquardt algorithm for a non-linear least squares refinement.

The function minimised is:

```               m-1
F(x) = Sigma [f(j)(x[0],x[1],...x[n-1])]**2         (m>=n)
j=0
```
The user must supply an initial approximation to the required solution and an object supporting the LeastSquaresRefineable interface which implements a method to calculate the values of the functions f(j), j=0,1,...m-1 for any values of the refineable parameters.

The refinement code has been directly derived from SLATEC public domain Fortran routines written by K.L. Hiebert (SNLA).

Method Definition:
public static int leastSquaresMQL (int m, int n, double[] x, double acc, double h, JdlLeastSquaresRefineable lfn, JdlDouble fmin, double[] fvecc)
Parameters List:
m
The number of functions f(j) occuring in the sum of squares.
n
The number of variables x[0] ... x[n-1] to be refined. Estimated values must be given on input and refined values are returned.
x
An array containing the 'n' refineable parameters x[0] ... x[n-1]. Estimated values must be given on input and refined values are returned.
acc
Tolerance in the Function value such that failure to decrease by more than this amount in an iteration signals completness. (Or, more precisely, used as the values for FTOL and XTOL in call to the SLATEC library routine DNLS1).
h
The step size to be used in calculating the partial derivatives (EPSFCN parameter in call to the SLATEC library routine DNLS1).
lfn
The object with the method to calculate the values of the functions f(j), j=0,1,..,m-1.
fmin
Returns the minimum value sum of squares in fmin.value.
fvecc
An array, dimensioned to at least m, returning the m values of f(j), j=0,1,...m-1 calculated using the refined parameter values.
Method Return:
Output flag:
= 0, OK
= -1, error in parameters
= 1, algorithm estimates that the relative error in the sum of squares is at most 'acc'
= 2, algorithm estimates that the relative error between 'x' and the solution is at most 'acc'
= 3, conditions for flag = 1 and flag = 2 both hold
= 4, 'fvecc' is orthogonal to the columns of the Jacobian to machine precision
= 5, number of calls to function method has reached 200*(n+1). = 6, 'acc' is too small. No further reduction in the sum of squares is possible
= 7, 'acc' is too small. No further improvement in the approximate solution 'x' is possible

### 2.9.2.4.3 Calculate sigma values - leastSquaresSigmas

This method calculates approximate sigma values following a non-linear least squares refinement procedure.

Method Definition:
public static int leastSquaresSigmas(int m, int n, double[] x, JdlLeastSquaresRefineable lfn, double[] sigma, double[] fvecc)
Parameters List:
m
The number of functions f(j) occuring in the sum of squares.
n
The number of variables x[0] ... x[n-1] refined.
x
An array containing the 'n' refined parameters x[0] ... x[n-1].
lfn
The object with the method to calculate the values of the functions f(j), j=0,1,..,m-1.
sigma
This array returns the 'n' sigma values for the 'n' refined parameters.
fvecc
An array, dimensioned to at least m, returning the m values of f(j), j=0,1,...m-1 calculated using the refined parameter values.
Method Return:
Output flag:
= 0, OK
= 1, Error in the parameters
= 2, Singular matrix

## 2.9.2.5 Curve Fitting

### 2.9.2.5.1 Introduction

This section contains methods to derive a polynomial curve to which gives the best least squares fit to a set of points.

Methods:

Fit a curve - fitCurve
Fit a curve - fitCurve

### 2.9.2.5.2 Fit a curve - fitCurve

This method fits a Chebyshev Polynomial based curve to a set of points using the least squares refinement methods from this class. This version uses unit weights for the points.

Method Definition:
public static JdlChebyshevPolynomial fitCurve(int n, JdlXY[] xy, int iord, JdlDouble fmin, JdlError err)
Parameters List:
n
The number of points to be used in the curve fitting.
JdlXY
The array of points to which the curve is to be fitted. 'x' is taken as the independent variable and 'y' as the dependent variable.
iord
The order of the polynomial to be used in the curve fitting (<= n-1).
fmin
Returns the least squares residual (sum of squares).
err
An error object in which to set error return codes. If an error is found 'err.err' will be set to true, an error message will be returned in 'err.msg1' and the flag 'err.flag' will be set as follows:
= 0, OK
= -1, Invalid parameters
= -2, Order too large for number of points
> 0, Flag as from the leastSquaresMQL(..) method
Method Return:
A JdlChebyshevPolynomial object containing the fitted curve parameters - null if an error was found (see 'err') (Note that the stored 'outlim' parameter will be set to 0.0 by this method).

### 2.9.2.5.3 Fit a curve - fitCurve

This method fits a Chebyshev Polynomial based curve to a set of points using the least squares refinement methods from this class. This version uses given weights for the points.

Method Definition:
public static JdlChebyshevPolynomial fitCurve(int n, JdlXY[] xy, double[] wt, int iord, JdlDouble fmin, JdlError err)
Parameters List:
n
The number of points to be used in the curve fitting.
JdlXY
The array of points to which the curve is to be fitted. 'x' is taken as the independent variable and 'y' as the dependent variable.
wt
Weights for the 'n' points to be fitted.
iord
The order of the polynomial to be used in the curve fitting (<= n-1).
fmin
Returns the least squares residual (sum of squares).
err
An error object in which to set error return codes. If an error is found 'err.err' will be set to true, an error message will be returned in 'err.msg1' and the flag 'err.flag' will be set as follows:
= 0, OK
= -1, Invalid parameters
= -2, Order too large for number of points
> 0, Flag as from the leastSquaresMQL(..) method
Values of -1, -2, and 4 are treated as fatal errors.
Method Return:
A JdlChebyshevPolynomial object containing the fitted curve parameters - null if a fatal error was found (see 'err') (Note that the stored 'outlim' parameter will be set to 0.0 by this method).

⇑ Up 2   ⇑ Up 1   ⇑ Top of this