|
|
◆ lmdif1()
| def lmdif1 |
( |
f |
, |
|
|
m |
, |
|
|
n |
, |
|
|
x |
, |
|
|
fvec |
, |
|
|
tol |
= 1.0e-10 |
|
) |
| |
Nonlinear least squares approximation (Levenberg-Marquardt method) (Jacobian not required) (simple driver)
- Purpose
- lmdif1 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 function values. Since the Jacobian is calculated by finite difference approximation within the routine, the user is not required to calculate the Jacobian.
lmdif1 is equivalent to using lmdif with setting ftol = tol, xtol = tol, gtol = 0, maxfev = 100*(n+1), epsfcn = 0, mode = 1, factor = 100 and nprint = 0.
- Returns
- info (int)
= 0: Successful exit (sub-code is set to work[0])
= -1: The argument f is invalid. = -2: The argument m had an illegal value (m < n)
= -3: The argument n had an illegal value (n < 1)
= -4: The argument x is invalid. = -5: The argument fvec is invalid. = -6: The argument tol had an illegal value (tol < 0)
= 1: Number of calls to fcn 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
= 4: gtol is too small. fvec is orthogonal to the columns of the Jacobian to machine precision
- Parameters
-
| [in] | f | User supplied subroutine which calculates the functions fi(x) defined as follows. _CODE def f(m, n, x, fvec, iflag): if iflag == 1 or iflag == 2: fvec[0] = function value f1(x[0], x[1], ..., x[n-1]) fvec[1] = function value f2(x[0], x[1], ..., x[n-1]) ... fvec[m-1] = function value fn(x[0], x[1], ..., x[n-1]) _ENDCODE When iflag = 1 or 2, calculate the function values fi(x) and set to fvec. |
| [in] | m | Number of functions. (m > 0) |
| [in] | n | Number of variables. (0 < n <= m) |
| [in,out] | x | Numpy ndarray (1-dimensional, float, length n)
[in] An initial estimate of the solution vector.
[out] The obtained solution vector. |
| [out] | fvec | Numpy ndarray (1-dimensional, float, length m)
The function values evaluated at the solution vector x. |
| [in] | tol | (Optional)
Relative error desired in the sum of squares and the approximate solution. (tol >= 0) (default = 1.0e-10) |
- 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. def f(m, n, x, fvec, iflag):
if iflag == 1 or iflag == 2:
xdata = np.array([77.6, 239.9, 434.8, 760.0])
ydata = np.array([10.07, 29.61, 50.76, 81.78])
for i in range(m):
fvec[i] = ydata[i] - x[0]*(1 - exp(-xdata[i]*x[1]))
def TestLmdif1():
m = 4
n = 2
x = np.array([500.0, 0.0001])
fvec = np.empty(m)
info = lmdif1(f, m, n, x, fvec)
print(x)
print(fvec)
print(info)
def lmdif1(f, m, n, x, fvec, tol=1.0e-10) Nonlinear least squares approximation (Levenberg-Marquardt method) (Jacobian not required) (simple dr...
- Example Results
>>> TestLmdif1()
[2.41084895e+02 5.44942237e-04]
[ 0.08766976 0.06582092 -0.09976828 0.02757617]
0
|