CMinpack使用介绍

github: https://github.com/devernay/cminpack
主页: http://devernay.github.io/cminpack/
使用手册: http://devernay.github.io/cminpack/man.html

LMDIF使用说明


官方英文介绍:http://devernay.github.io/cminpack/lmdif_.html

包括函数名

lmdif,lmdif1_ - 最小化非线性函数平方和

函数概要

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 );

详细描述

lmdif_的目的是最小化m个n元非线性方程的平方和,使用的方法是LM算法的改进。用户需要提供计算方程的子程序。Jacobian矩阵会通过一个前向差分(forward-difference)近似计算得到。
lmdif1_是相同的目的,但是调用方法更简单一些。

语言备注

这些函数是通过FORTRAN写的,如果从C调用,需要记住以下几点:

  • 名称重编:
    • 2.95/3.0版本的g77下,所有函数以下划线结尾,后续版本可能会更改;
  • 使用g77编译:
    • 即使你的程序全部用C语言写成,你也需要使用gcc进行链接,因为这样它会自动导入FORTRAN库。只使用g77进行编译是最方便的(它处理C语言也是OK的);
  • 通过引用调用:
    • 所有函数参数都是指针;
  • 列优先数组:
    • z( i , j ) = z[ ( i - 1 ) + ( j - 1 ) * n
    • fcn是用户提供用于计算函数的子程序。在C语言当中fcn需要如下定义:
void fcn(int m, int n, double *x, double *fvec, int *iflag) {
    /* 计算函数在x点的值,通过fvec返回。*/
}

iflag的值不能被fcn所修改,除非用户想要终止lmdif/lmdif1_。在这个例子中iflag设置为负整数。

lmdif_和lmdif1_的共同参数

m:函数个数;
n:变量个数(n<=m)
x:长度为n的数组,设置为初始的估计解向量。输出的时候x内容为最终估计的解向量。
fvec:输出长度为m的数组,内容为最终输出x计算得到的函数解。

lmdif1_的参数

tol:作为输入,非负数。用于函数终止的条件判断:

  • 平方和小于tol;
  • x之间的相对误差小于tol;

info:作为输出。如果用户终止了函数的执行,info将被设置为iflag的值(负数)(详细见fcn的描述),否则,info的值如下几种情况:

  • 0:输入参数不合适;
  • 1:平方和的相对误差小于tol;
  • 2:x之间的相对误差小于tol;
  • 3:1/2两种情况同时符合;
  • 4: fvec is orthogonal to the columns of the Jacobian to machine precision(这个情况是什么暂时不是很清楚)
  • 5:调用fcn的次数达到了200*(n+1)次;
  • 6:tol设置过小,平方和无法达到那么小;
  • 7:tol设置过小,x的近似解无法优化到误差达到那么小。

iwa:长度n的工作数组;
wa:长度lwa的工作数组;
lwa:作为输入,整数,不能小于mn+5n+m;
[NOTE] 这三个输入我也不知道作用,从样例来看不需要初始化。

lmdif_的参数

暂时不用这部分,跳过。

官方样例解读

源码: https://github.com/devernay/cminpack/blob/d1f5f5a273862ca1bbcf58394e4ac060d9e22c76/examples/tlmdif1_.c

/*     driver for lmdif1 example. */
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <minpack.h>
#define real __minpack_real__

// 用户自定义的函数f()
// real -> __cminpack_real__ -> 浮点数(double)
void fcn(const int *m, const int *n, const real *x, real *fvec, int *iflag);

int main()
{
  int j, m, n, info, lwa, iwa[3], one=1;
  real tol, fnorm, x[3], fvec[15], wa[75];

  // 函数个数15; 变量数3
  m = 15;
  n = 3;

  /* the following starting values provide a rough fit. */
  // 初始位置
  // 1.e0 = 1.0e0 = 1.0
  x[0] = 1.e0;
  x[1] = 1.e0;
  x[2] = 1.e0;

  // 为什么要设置75?
  lwa = 75;

  /* set tol to the square root of the machine precision.  unless high
     precision solutions are required, this is the recommended
     setting. */
  // (建议打印一下看值是多少)

  tol = sqrt(__minpack_func__(dpmpar)(&one));

  // 需要注意指针
  __minpack_func__(lmdif1)(&fcn, &m, &n, x, fvec, &tol, &info, iwa, wa, &lwa);

  // 最终的2范数(即平方和开根号)
  fnorm = __minpack_func__(enorm)(&m, fvec);
  printf("      final l2 norm of the residuals%15.7g\n\n", (double)fnorm);
  printf("      exit parameter                %10i\n\n", info);
  printf("      final approximate solution\n");
  for (j=1; j<=n; j++) {
    printf("%s%15.7g", j%3==1?"\n ":"", (double)x[j-1]);
  }
  printf("\n");
  return 0;
}

//        The problem is to determine the values of x(1), x(2), and x(3)
//        which provide the best fit (in the least squares sense) of
//              x(1) + u(i)/(v(i)*x(2) + w(i)*x(3)),  i = 1, 15
//        to the data
//              y = (0.14,0.18,0.22,0.25,0.29,0.32,0.35,0.39,
//                   0.37,0.58,0.73,0.96,1.34,2.10,4.39),
//        where u(i) = i, v(i) = 16 - i, and w(i) = min(u(i),v(i)).  The
//        i-th component of FVEC is thus defined by
//              y(i) - (x(1) + u(i)/(v(i)*x(2) + w(i)*x(3))).

void fcn(const int *m, const int *n, const real *x, real *fvec, int *iflag)
{
  /* function fcn for lmdif1 example */

  int i;
  real tmp1,tmp2,tmp3;

  // 实际的y值
  real 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};
  assert(*m == 15 && *n == 3);


  if (*iflag == 0) {
    /*      insert print statements here when nprint is positive. */
    /* if the nprint parameter to lmder is positive, the function is
       called every nprint iterations with iflag=0, so that the
       function may perform special operations, such as printing
       residuals. */
    // 这段没有很看懂,在??情况下打印信息
    return;
  }

  /* compute residuals */

  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));
  }
}

有待更新

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值