HyperMath

NLCurveFit

NLCurveFit

Previous topic Next topic No expanding text in this topic  

NLCurveFit

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

Nonlinear curve fitting with an arbitrary model. The second form allows for specifying the analytical Jacobian of the model.

Syntax

p,r, ph, oh = NLCurveFit(model, init, ind, dep, maxIter,tol, userData)

p,r, ph, oh = NLCurveFit(model, jac, init, ind, dep, maxIter, tol, userData)

Arguments

Name

Description

 

model

A string containing the function name that implements the model. It must return a real vector of equation residuals. See Comments.

 

jac

A string containing the function name that implements the Jacobian matrix of the model. See Comments.

 

init

A vector of initial estimates of the parameters in the model.

 

ind

A vector of the independent variable data.

 

dep

A vector of the dependent variable data.

 

maxIter

A one element vector containing the maximum number of iterations allowed. If it is empty or omitted, it defaults to 200.

 

tol

A one element vector containing the convergence tolerance for the algorithm.  If empty or omitted, it defaults to 1.0E-6.

 

userData

Data matrix that is passed to the user defined function at each iteration.  This can be used to supply data that are defined outside the function.

Outputs

Name

Description

 

p

A vector of the updated parameter estimates of the model.  See Comments.

 

r

A vector of the statistical fit quality.  See Comments.

 

ph

A matrix containing the parameter history.  Each column of the matrix contains the values for an iteration step.

 

oh

A row vector containing the objective function history at each iteration step.

Example

For a given set of independent and dependent data, find the best exponential fit.

 

Syntax

// define the fitting function

function FittingFunc(c, t, d)

   // c is the parameter vector

   // t is the independent variable

   // d is the optional user data matrix

  // the model is y = c1 * exp(-c2*t)

   return c(1) * Exp(-c(2)*t)

end

 

// Now the fit. Assume the independent & dependant data

// are t & x respectively, the initial parameter estimates

// are in vector init

t = [1; 2; 3; 4]

x = [8.1; 3.9; 2.05; 0.95]

init = [15, 1]

p, f = NLCurveFit("FittingFunc", init, t, x, [100],

                 [1.0e-4])

 

Result

 

p = 16.414       0.70854

f = 0.0037648    0.99975      

Example 2

Modify the previous example to use the analytical Jacobian matrix.

 

Syntax

// define the Jacobian function

function Jacobian(c, t, d)

   // c is the parameter vector

   // t is the independent variable

   // d is the optional user data matrix

   // the model is y = c1 * exp(-c2*t)

   out = []

   out(:,1) = Exp(-c(2)*t)

   out(:,2) = -t * c(1) .* Exp(-c(2)*t)

end

 

// Now the fit. Assume the independent and dependant data

// are t and x respectively, the initial parameter estimates

// are in vector init

t = [1; 2; 3; 4]

x = [8.1; 3.9; 2.05; 0.95]

init = [15, 1]

p, f = NLCurveFit("FittingFunc", "Jacobian", init, t, x,

                 [100], [1.0e-4])

 

Result

p = 16.414       0.70854

f = 0.0037648    0.99975      

Comments

The fit is done by iterative method until the parameter updates converge based on the given tolerance.

The model function requires a vector of the parameters and a vector or matrix of the independent data, respectively, as inputs. The independent data can be a matrix if the domain is multi-variate. One dimension of the independent data must equal the length of the dependent data vector. An optional matrix of user data may be supplied as a third input. The function must return a vector that contains the residuals of the model.

If the function that computes the Jacobian matrix is omitted, the matrix will be computed internally with numerical derivatives.

The final parameter vector p will have the same orientation as the initial condition vector init. However, the vector for the parameters that is passed to the user functions will be a column vector.

The statistical fit quality consists of the mean squared error (MSE) and the correlation coefficient (R).

See Also:

MultiRegress

PolyFit