24小时热门版块排行榜    

查看: 1663  |  回复: 3
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

wsbl

金虫 (小有名气)

[交流] Levenberg-Marquardt 算法的例子代码 已有3人参与

#include "stdio.h"
#include
#include
void out(double*p,int n,int m)//输出矩阵
{int i,j;

for (i=0;i            for (j=0;j     {   
           if ((m*i+j)%m==0)
                   printf("\n";
           printf("%16.8f",*(p+m*i+j));
          
   }
   printf("\n\n";
}


void LM(double *c, double *sigma, int N, double sigma_H2O,double r[3])
{
int i,j,maxtime=100; //如果不收敛,允许的最大迭代次数
double s,snext,det,temp0;
   
  
    double x[]={.5,.5},xnext[2],ep[2];
   
   double f[6];
   double f_x1[6];
   double f_x2[6];
   
   double m[4];  //矩阵

   double temp1[4];//逆矩阵
        double temp2[2];
         double temp3[2];
       

   double alph,beita;

   j=0;
     for (i=0;i         {f=(sigma-(sigma_H2O-sigma_H2O*x[0]*log(1+c/x[1])));
     f_x1=sigma_H2O*log(1+c/x[1]);
     f_x2=-sigma_H2O*x[0]*c/(x[1]*x[1])/(1+c/x[1]);
        }

  s=.0;
  for (i=0;i     s=s+f*f;


  temp0=.0;
  for (i=0;i   temp0=temp0+f_x1*f_x1;
  m[0]=temp0;
       
  temp0=.0;
  for (i=0;i   temp0=temp0+f_x1*f_x2;
  m[1]=temp0;
  m[2]=temp0;

  temp0=.0;
  for (i=0;i   temp0=temp0+f_x2*f_x2;
  m[3]=temp0;


        //out(m,2,2);//到此步正确。
  alph=(fabs(*m)+fabs(*(m+3)))/2;
  //printf("%8.5e\n",alph);
  beita=2.0;

   m[0]=m[0]+alph;
   m[3]=m[3]+alph;
   //out(m,2,2);//到此步正确。

  det=1.0/(m[0]*m[3]-m[1]*m[2]);
//printf("迭%15.10f\n",det);
  temp1[0]=det*m[3];
  temp1[1]=-det*m[1];
  temp1[2]=-det*m[1];
  temp1[3]=det*m[0];

//out(temp1,2,2);//到此步正确。
//temp2=mult(temp,f,2,N,1);
  temp0=.0;
  for (i=0;i   temp0=temp0+f*f_x1;
  temp2[0]=temp0;

  temp0=.0;
  for (i=0;i   temp0=temp0+f*f_x2;
  temp2[1]=temp0;


//out(temp2,2,1);//到此步正确。
//temp3=mult(temp1,temp2,2,2,1);
  temp0=.0;
  for (i=0;i<2;i++)
        {                temp0=temp0+temp1*temp2;                   
        }
        temp3[0]=temp0;

  temp0=.0;
  for (i=0;i<2;i++)
        {                temp0=temp0+temp1[2+i]*temp2;                   
        }
        temp3[1]=temp0;


//out(temp3,2,1);//到此步正确。

  xnext[0]=x[0]-temp3[0];
  xnext[1]=x[1]-temp3[1];
//out(xnext,2,1);//到此步正确。

  snext=0;
  for (i=0;i   { f=(sigma-(sigma_H2O-sigma_H2O*xnext[0]*log(1+c/xnext[1])));
     snext=snext+f*f;
  }
//printf("%8.5e",snext);//到此步正确。

  ep[0]=xnext[0]-x[0];
  ep[1]=xnext[1]-x[1];

//out(ep,2,1);//到此步正确。

while (((fabs(ep[0])+fabs(ep[1]))/2)>1.0e-6 && j {j=j+1;
   if (snext>=s)
       alph=alph*beita;
   else
       alph=alph/beita;
      //printf("%8.3f\n",(float)j);
   
      x[0]=xnext[0];
          x[1]=xnext[1];
      s=snext;
   
     for (i=0;i         {f=(sigma-(sigma_H2O-sigma_H2O*x[0]*log(1+c/x[1])));
     f_x1=sigma_H2O*log(1+c/x[1]);
     f_x2=-sigma_H2O*x[0]*c/(x[1]*x[1])/(1+c/x[1]);
        }

   s=.0;
   for (i=0;i     s=s+f*f;

temp0=.0;
  for (i=0;i   temp0=temp0+f_x1*f_x1;
  m[0]=temp0;
       
  temp0=.0;
  for (i=0;i   temp0=temp0+f_x1*f_x2;
  m[1]=temp0;
  m[2]=temp0;

  temp0=.0;
  for (i=0;i   temp0=temp0+f_x2*f_x2;
  m[3]=temp0;

m[0]=m[0]+alph;
m[3]=m[3]+alph;
//out(m,2,2);//到此步正确。

  det=1.0/(m[0]*m[3]-m[1]*m[2]);
//printf("迭%15.10f\n",det);
  temp1[0]=det*m[3];
  temp1[1]=-det*m[1];
  temp1[2]=-det*m[1];
  temp1[3]=det*m[0];

//out(temp1,2,2);//到此步正确。
//temp2=mult(temp,f,2,N,1);
temp0=.0;
  for (i=0;i   temp0=temp0+f*f_x1;
  temp2[0]=temp0;

  temp0=.0;
  for (i=0;i   temp0=temp0+f*f_x2;
  temp2[1]=temp0;


//out(temp2,2,1);//到此步正确。
//temp3=mult(temp1,temp2,2,2,1);
  temp0=.0;
  for (i=0;i<2;i++)
        {                temp0=temp0+temp1*temp2;                   
        }
        temp3[0]=temp0;

  temp0=.0;
  for (i=0;i<2;i++)
        {                temp0=temp0+temp1[2+i]*temp2;                   
        }
        temp3[1]=temp0;


//out(temp3,2,1);//到此步正确。

xnext[0]=x[0]-temp3[0];
xnext[1]=x[1]-temp3[1];
//out(xnext,2,1);//到此步正确。

snext=0;
for (i=0;i { f=(sigma-(sigma_H2O-sigma_H2O*xnext[0]*log(1+c/xnext[1])));
     snext=snext+f*f;
}

ep[0]=xnext[0]-x[0];
ep[1]=xnext[1]-x[1];
   
}
r[0]=xnext[0];
r[1]=xnext[1];
r[2]=(double)j;
}

void main()
{
   //double p[] = {0.02,0.04,0.08,0.12,0.16,0.2,0.24,0.28};//mol/L
   //double q[] = {0.06810,0.06442,0.0601,0.05678,0.05467,0.05237,0.05108,0.05025};//N/m

   double p[] = {0.02,0.04,0.08,0.12,0.16,0.2};
   double q[] = {0.06810,0.0644,0.0601,0.0568,0.0547,0.0524};
   double r[3];
   LM(p,q,6,0.07197,r);
   
    printf("迭代次数:%5.0f\n",r[2]);

if (r[2]<100)
{  
    printf("\n希什科夫模型sigma_H2O-sigma_H2O*a*ln(1+x/b)中\n\n参数a的最优值为:%9.9f\n",r[0]);
    printf("\n参数b的最优值为:%9.9f\n\n",r[1]);
}
  else
    printf("\n用Levenberg法失败\n\n";

  for(int i=0;i<1;i++)//延时几秒。
        {
        printf("按回车键结束程序:";
        getchar();
        }  
}
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lingshuning

木虫 (职业作家)


小木虫(金币+0.5):给个红包,谢谢回帖
很完整,要是再有个程序注释就完美了,不过还是多谢楼主的分享!
不管未来如何,我必竭尽所能
4楼2011-12-03 19:18:50
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 4 个回答

lrs2008

木虫 (正式写手)

观天


小木虫(金币+0.5):给个红包,谢谢回帖
乖乖   这么长 ...
3楼2011-10-16 09:21:30
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿吉林大学材料学硕321求调剂 +7 Ymlll 2026-03-18 10/500 2026-03-19 19:43 by maocaozhuxi
[考研] 288求调剂,一志愿华南理工大学071005 +5 ioodiiij 2026-03-17 5/250 2026-03-19 18:22 by zcl123
[考研] 085600材料与化工求调剂 +6 绪幸与子 2026-03-17 6/300 2026-03-19 13:27 by houyaoxu
[考研] 332求调剂 +3 ydfyh 2026-03-17 3/150 2026-03-19 10:14 by 功夫疯狂
[教师之家] 焦虑 +9 水冰月月野兔 2026-03-13 13/650 2026-03-19 09:50 by otani
[考研] 本科郑州大学物理学院,一志愿华科070200学硕,346求调剂 +4 我不是一根葱 2026-03-18 4/200 2026-03-19 09:11 by 浮云166
[考研] 一志愿华中科技大学,080502,354分求调剂 +4 守候夕阳CF 2026-03-18 4/200 2026-03-18 22:16 by li123456789.
[考研] 330求调剂 +3 小材化本科 2026-03-18 3/150 2026-03-18 21:55 by 无懈可击111
[考研] 328求调剂,英语六级551,有科研经历 +3 生物工程调剂 2026-03-17 7/350 2026-03-18 20:41 by Wangjingyue
[考研] 【同济软件】软件(085405)考研求调剂 +3 2026eternal 2026-03-18 3/150 2026-03-18 19:09 by 搏击518
[考研] 085601求调剂 +4 Du.11 2026-03-16 4/200 2026-03-17 17:08 by ruiyingmiao
[考研] 材料与化工专硕调剂 +5 heming3743 2026-03-16 5/250 2026-03-17 14:03 by 勇敢太监王公公
[考研] 304求调剂 +5 素年祭语 2026-03-15 5/250 2026-03-16 17:00 by 我的船我的海
[考研] 321求调剂 +5 大米饭! 2026-03-15 5/250 2026-03-16 16:33 by houyaoxu
[考研] 中科院材料273求调剂 +4 yzydy 2026-03-15 4/200 2026-03-16 15:59 by Gaodh_82
[考研] 0703化学调剂 290分有科研经历,论文在投 +7 腻腻gk 2026-03-14 7/350 2026-03-16 10:12 by houyaoxu
[考研] 294求调剂 +3 Zys010410@ 2026-03-13 4/200 2026-03-15 10:59 by zhq0425
[考研] 材料与化工 323 英一+数二+物化,一志愿:哈工大 本人本科双一流 +4 自由的_飞翔 2026-03-13 5/250 2026-03-14 19:39 by hmn_wj
[考研] 一志愿哈工大材料324分求调剂 +5 闫旭东 2026-03-14 5/250 2026-03-14 14:53 by 木瓜膏
[考研] 295求调剂 +3 小匕仔汁 2026-03-12 3/150 2026-03-13 15:17 by vgtyfty
信息提示
请填处理意见