| 4 | 1/1 | 返回列表 |
| 查看: 1660 | 回復: 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 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; //如果不收斂,允許的最大迭代次數(shù) 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_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 temp0=.0; for (i=0;i m[0]=temp0; temp0=.0; for (i=0;i m[1]=temp0; m[2]=temp0; temp0=.0; for (i=0;i 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 temp2[0]=temp0; temp0=.0; for (i=0;i 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 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 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_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 temp0=.0; for (i=0;i m[0]=temp0; temp0=.0; for (i=0;i m[1]=temp0; m[2]=temp0; temp0=.0; for (i=0;i 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 temp2[0]=temp0; temp0=.0; for (i=0;i 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 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("迭代次數(shù):%5.0f\n",r[2]); if (r[2]<100) { printf("\n希什科夫模型sigma_H2O-sigma_H2O*a*ln(1+x/b)中\(zhòng)n\n參數(shù)a的最優(yōu)值為:%9.9f\n",r[0]); printf("\n參數(shù)b的最優(yōu)值為:%9.9f\n\n",r[1]); } else printf("\n用Levenberg法失敗\n\n" ;for(int i=0;i<1;i++)//延時幾秒。 { printf("按回車鍵結束程序:" ;getchar(); } } |
軟件學習 |
木蟲 (正式寫手)
觀天

木蟲 (職業(yè)作家)

| 4 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 考研求調(diào)劑 +3 | 橘頌. 2026-03-17 | 4/200 |
|
|---|---|---|---|---|
|
[考研] 268求調(diào)劑 +7 | 好運連綿不絕 2026-03-12 | 8/400 |
|
|
[考研] 301求調(diào)劑 +4 | A_JiXing 2026-03-16 | 4/200 |
|
|
[碩博家園] 湖北工業(yè)大學 生命科學與健康學院-課題組招收2026級食品/生物方向碩士 +3 | 1喜春8 2026-03-17 | 5/250 |
|
|
[考研] 考研化學學碩調(diào)劑,一志愿985 +4 | 張vvvv 2026-03-15 | 6/300 |
|
|
[考研] 312求調(diào)劑 +4 | 陌宸希 2026-03-16 | 5/250 |
|
|
[考研] 工科材料085601 279求調(diào)劑 +3 | 困于星晨 2026-03-17 | 3/150 |
|
|
[考研] 材料專碩274一志愿陜西師范大學求調(diào)劑 +5 | 薛云鵬 2026-03-13 | 5/250 |
|
|
[考研] 274求調(diào)劑 +5 | 時間點 2026-03-13 | 5/250 |
|
|
[考研] 環(huán)境工程調(diào)劑 +6 | 大可digkids 2026-03-16 | 6/300 |
|
|
[考研] 0703一志愿211 285分求調(diào)劑 +5 | ly3471z 2026-03-13 | 5/250 |
|
|
[考研] 0703化學調(diào)劑 290分有科研經(jīng)歷,論文在投 +7 | 膩膩gk 2026-03-14 | 7/350 |
|
|
[考研] 297一志愿上交085600求調(diào)劑 +5 | 指尖八千里 2026-03-14 | 5/250 |
|
|
[考研] 337一志愿華南理工0805材料求調(diào)劑 +7 | mysdl 2026-03-11 | 9/450 |
|
|
[考研] 0703化學調(diào)劑 +4 | 快樂的香蕉 2026-03-11 | 4/200 |
|
|
[考研] 求材料調(diào)劑 085600英一數(shù)二總分302 前三科235 精通機器學習 一志愿哈工大 +4 | 林yaxin 2026-03-12 | 4/200 |
|
|
[考研] 290求調(diào)劑 +9 | ADT 2026-03-11 | 9/450 |
|
|
[考研] 311求調(diào)劑 +3 | 冬十三 2026-03-13 | 3/150 |
|
|
[考研] 【考研調(diào)劑求收留】 +3 | Ceciilia 2026-03-11 | 3/150 |
|
|
[考研] 277求調(diào)劑 +4 | anchor17 2026-03-12 | 4/200 |
|