cminpack软件包中lmdif求最小二乘解的函数使用

NAME

lmdif_, lmdif1_ - minimize the sum of squares of m nonlinear functions   

SYNOPSIS

include <minpack.h>
void lmdif1_ ( void (* fcn )(int * m , int * n , double * x , double * fvec , int * iflag ),

int * m , int *  n , double * x , double * fvec ,
double * tol , int * info , int * iwa , double * wa , int * lwa );
void lmdif_ ( void (* fcn )(int * m , int * n , double * x , double * fvec , int * iflag ),

int * m , int * n , double * x , double * fvec ,
double * ftol , double * xtol , double * gtol , int * maxfev , double * epsfcn , double * diag , int * mode ,double * factor , int * nprint , int * info , int * nfev , double * fjac ,
int * ldfjac , int * ipvt , double * qtf ,
double * wa1 , double * wa2 , double * wa3 , double * wa4  );

 

DESCRIPTION

 

The purpose of  lmdif_  is to minimize the sum of the squares of  m  nonlinear functions in  n variables by a modification of the Levenberg-Marquardt algorithm. The user must provide a subroutine which calculates the functions. The Jacobian is then calculated by a forward-difference approximation.

lmdif1_ serves the same purpose but has a simplified calling sequence.
 

Language notes

These functions are written in FORTRAN. If calling from C, keep these points in mind:
Name mangling.
With  g77 version 2.95 or 3.0, all the function names end in an underscore. This may change with future versions of  g77.
Compile with  g77.
Even if your program is all C code, you should link with  g77 so it will pull in the FORTRAN libraries automatically. It's easiest just to use  g77 to do all the compiling. (It handles C just fine.)
Call by reference.
All function parameters must be pointers.
Column-major arrays.
Suppose a function returns an array with 5 rows and 3 columns in an array  z and in the call you have declared a leading dimension of 7. The FORTRAN and equivalent C references are:

 

z(1,1) z[0] z(2,1) z[1] z(5,1) z[4] z(1,2) z[7] z(1,3) z[14] z(i,j) z[(i-1) + (j-1)*7]

fcn is the name of the user-supplied subroutine which calculates the functions. In FORTRAN,  fcnmust be declared in an external statement in the user calling program, and should be written as follows:

 

subroutine fcn(m,n,x,fvec,iflag) integer m,n,iflag double precision x(n),fvec(m) ---------- calculate the functions at x and return this vector in fvec. ---------- return end

In C, fcn should be written as follows:

 

void fcn(int m, int n, double *x, double *fvec, int *iflag) { }

The value of iflag should not be changed by fcn unless the user wants to terminate execution oflmdif_ (or lmdif1_). In this case set iflag to a negative integer.

 

Parameters for both lmdif_ and lmdif1_

m is a positive integer input variable set to the number of functions.

n is a positive integer input variable set to the number of variables. n must not exceed m.

x is an array of length n. On input x must contain an initial estimate of the solution vector. On output x contains the final estimate of the solution vector.

fvec is an output array of length m which contains the functions evaluated at the output x.
 

Parameters for lmdif1_

tol is a nonnegative input variable. 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.

info is an integer output variable. if the user has terminated execution, info is set to the (negative) value of iflag. see description of fcn. otherwise, info is set as follows.


  info  improper input parameters.


  info  algorithm estimates that the relative error in the sum of squares is at most tol.


  info  algorithm estimates that the relative error between x and the solution is at most tol.


  info  conditions for info and info both hold.


  info  fvec is orthogonal to the columns of the Jacobian to machine precision.


  info  number of calls to fcn has reached or exceeded 200*(n+1).


  info  tol is too small. no further reduction in the sum of squares is possible.


  info  tol is too small. no further improvement in the approximate solution x is possible.

iwa is an integer work array of length n.

wa is a work array of length lwa.

lwa is an integer input variable not less than m*n + 5*n + m.
 

Parameters for lmdif_

ftol is a nonnegative input variable. Termination occurs when both the actual and predicted relative reductions in the sum of squares are at most ftol. Therefore, ftol measures the relative error desired in the sum of squares.

xtol is a nonnegative input variable. Termination occurs when the relative error between two consecutive iterates is at most xtol. Therefore, xtol measures the relative error desired in the approximate solution.

gtol is a nonnegative input variable. Termination occurs when the cosine of the angle between fvecand any column of the Jacobian is at most gtol in absolute value. Therefore, gtol measures the orthogonality desired between the function vector and the columns of the Jacobian.

maxfev is a positive integer input variable. Termination occurs when the number of calls to fcn is at least maxfev by the end of an iteration.

epsfcn is an input variable used in determining a suitable step length for the forward-difference approximation. This approximation assumes that the relative errors in the functions are of the order of epsfcn. If epsfcn is less than the machine precision, it is assumed that the relative errors in the functions are of the order of the machine precision.

diag is an array of length n. If mode = 1 (see below), diag is internally set. If mode = 2, diagmust contain positive entries that serve as multiplicative scale factors for the variables.

mode is an integer input variable. If mode = 1, the variables will be scaled internally. If mode = 2, the scaling is specified by the input diag. Other values of mode are equivalent to mode = 1.

factor is a positive input variable used in determining the initial step bound. This bound is set to the product of factor and the euclidean norm of diag*x if the latter is nonzero, or else tofactor itself. In most cases factor should lie in the interval (.1,100.). 100. is a generally recommended value.

nprint is an integer input variable that enables controlled printing of iterates if it is positive. In this case, fcn is called with iflag = 0 at the beginning of the first iteration and every nprint iterations thereafter and immediately prior to return, with x and fvec available for printing. If nprint is not positive, no special calls of fcn with iflag = 0 are made.

info is an integer output variable. If the user has terminated execution, info is set to the (negative) value of iflag. See description of fcn. Otherwise, info is set as follows.


  info  improper input parameters.


  info  both actual and predicted relative reductions in the sum of squares are at most ftol.


  info  relative error between two consecutive iterates is at most xtol.


  info  conditions for info and info both hold.


  info  the cosine of the angle between fvec and any column of the Jacobian is at most gtol in absolute value.


  info  number of calls to fcn has reached or exceeded maxfev.


  info  ftol is too small. No further reduction in the sum of squares is possible.


  info  xtol is too small. No further improvement in the approximate solution x is possible.


  info gtol is too small. fvec is orthogonal to the columns of the Jacobian to machine precision.

nfev is an integer output variable set to the number of calls to fcn.

fjac is an output m by n array. The upper n by n submatrix of fjac contains an upper triangular matrix r with diagonal elements of nonincreasing magnitude such that


                       t
        *(jac *jac)*p *r,

where p is a permutation matrix and jac is the final calculated Jacobian. column j of p is columnipvt(j) (see below) of the identity matrix. The lower trapezoidal part of fjac contains information generated during the computation of r.

ldfjac is a positive integer input variable not less than m which specifies the leading dimension of the array fjac.

ipvt is an integer output array of length nipvt defines a permutation matrix p such that jac*p =q*r, where jac 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.

qtf is an output array of length n which contains the first n elements of the vector (qtranspose)*fvec.

wa1wa2, and wa3 are work arrays of length n.

wa4  is a work array of length  m .

示例函数:




#include <stdio.h>
#include <math.h>
#include <cminpack.h>

int fcn(void *p, int m, int n, const double *x, double *fvec, int iflag);

int main()
{
   int m, n, info, lwa, iwa[3];
   double tol, fnorm, x[3], fvec[15], wa[75];

   m = 15;
   n = 3;

 

   x[0] = 1.e0;
   x[1] = 1.e0;
   x[2] = 1.e0;

   lwa = 75;

 

   tol = 1e-15;
     
   info = lmdif1(fcn, 0, m, n, x, fvec, tol, iwa, wa, lwa);

   fnorm = enorm(m, fvec);

   printf("           FINAL L2 NORM OF THE RESIDUALS%15.7f\n\n",fnorm);
   printf("           EXIT PARAMETER                               %10i\n\n", info);
   printf("           FINAL APPROXIMATE SOLUTION\n\n %15.7f%15.7f%15.7f\n",
         x[0], x[1], x[2]);
   return 0;
}

int fcn(void *p, int m, int n, const double *x, double *fvec, int iflag)
{
 

   int i;
   double tmp1,tmp2,tmp3;
   double y[15]={1.4e-1,1.8e-1,2.2e-1,2.5e-1,2.9e-1,3.2e-1,3.5e-1,3.9e-1,
              3.7e-1,5.8e-1,7.3e-1,9.6e-1,1.34e0,2.1e0,4.39e0};
  double sum=0;
   for (i=0; i<15; i++)
       {
           tmp1 = i+1;
           tmp2 = 15 - i;
           tmp3 = tmp1;
         
           if (i >= 8) tmp3 = tmp2;
           fvec[i] = y[i] - (x[0] + tmp1/(x[1]*tmp2 + x[2]*tmp3));
       sum += fvec[i] * fvec[i];
       }
       printf("%lf sum\n",sqrt(sum));
   return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值