XLPack 7.0
XLPack Numerical Library (Excel VBA) Reference Manual
Loading...
Searching...
No Matches

◆ Lmdif1()

Sub Lmdif1 ( F As  LongPtr,
M As  Long,
N As  Long,
X() As  Double,
Fvec() As  Double,
Tol As  Double,
Info As  Long,
Optional Info2 As  Long 
)

Nonlinear least squares approximation by Levenberg-Marquardt method (Jacobian not required) (simple driver)

Purpose
This routine minimizes the sum of the squares of M nonlinear functions in N variables by a modification of the Levenberg-Marquardt algorithm.
minimize the sum of fi(x1, x2, ..., xN)^2 (sum for i = 1 to M)
The user must provide a subroutine which calculates the functions. Since the Jacobian is calculated by a forward-difference approximation within the routine, the user is not required to provide the Jacobian.

Lmdif1 is equivalent to using Lmdif with setting FTol = Tol, XTol = Tol, GTol = 0 and Mode = 1.
Parameters
[in]FUser supplied subroutine which calculates the functions fi(x) defined as follows.
Sub F(M As Long, N As Long, X() As Double, Fvec() As Double, IFlag As Long)
If IFlag = 1 or 2:
Calculate the function values fi(x) at X() and return in Fvec(i-1) (i = 1 to M). Other variables should not be changed.
End Sub
The value of IFlag should not be changed unless the user wants to terminate the execution. In this case, set IFlag to a negative integer.
[in]MNumber of functions. (M > 0)
[in]NNumber of variables. (0 < N <= M)
[in,out]X()Array X(LX - 1) (LX >= N)
[in] An initial estimate of the solution vector.
[out] The obtained solution vector.
[out]Fvec()Array Fvec(LFvec - 1) (LFvec >= M)
The function values evaluated at the solution vector X().
[in]TolRelative error desired in the sum of squares and the approximate solution. (tol >= 0)
[out]Info= 0: Successful exit. (Sub-code is set to Info2)
= -2: The argument M had an illegal value. (M < N)
= -3: The argument N had an illegal value. (N <= 0)
= -4: The argument X() is invalid. (Array X() is not big enough)
= -5: The argument Fvec() is invalid. (Array Fvec() is not big enough)
= -6: The argument Tol had an illegal value. (Tol < 0)
= 1: Number of calls to F with IFlag = 1 or 2 has reached Maxfev.
= 2: Tol is too small. No further reduction in the sum of squares is possible.
= 3: Tol is too small. No further improvement in the approximate solution X is possible.
= 5: User imposed termination (returned from F with IFlag < 0).
[out]Info2(Optional)
Sub-code on return with Info = 0.
= 1: Both actual and predicted relative reductions in the sum of squares are at most Tol.
= 2: Relative error between two consecutive iterates is at most Tol.
= 3: Both of above are satisfied.
= 4: Fvec is orthogonal to the columns of the Jacobian to machine precision.
Reference
netlib/minpack
Example Program
Approximate the following data with model function f(x) = c1*(1 - exp(-c2*x)). Two parameters c1 and c2 are determined by the nonlinear least squares method.
f(x) x
10.07 77.6
29.61 239.9
50.76 434.8
81.78 760.0
The initial estimates of the solution are c1 = 500 and c2 = 0.0001.
Sub FLmdif(M As Long, N As Long, X() As Double, Fvec() As Double, IFlag As Long)
Dim Xdata(3) As Double, Ydata(3) As Double, I As Long
Ydata(0) = 10.07: Xdata(0) = 77.6
Ydata(1) = 29.61: Xdata(1) = 239.9
Ydata(2) = 50.76: Xdata(2) = 434.8
Ydata(3) = 81.78: Xdata(3) = 760
If IFlag = 1 Or IFlag = 2 Then
For I = 0 To M - 1
Fvec(I) = Ydata(I) - X(0) * (1 - Exp(-Xdata(I) * X(1)))
Next
End If
End Sub
Sub Ex_Lmdif1()
Const M As Long = 4, N As Long = 2
Dim X(N - 1) As Double, Fvec(M - 1) As Double, Tol As Double, Info As Long
Tol = 0.00000001 '1.0e-8
X(0) = 500: X(1) = 0.0001
Call Lmdif1(AddressOf FLmdif, M, N, X(), Fvec(), Tol, Info)
Debug.Print "C1, C2 =", X(0), X(1)
Debug.Print "Info =", Info
End Sub
Sub Lmdif1(F As LongPtr, M As Long, N As Long, X() As Double, Fvec() As Double, Tol As Double, Info As Long, Optional Info2 As Long)
Nonlinear least squares approximation by Levenberg-Marquardt method (Jacobian not required) (simple d...
Example Results
C1, C2 = 241.084897132837 5.44942231312974E-04
Info = 0