| 5 | 1/1 | 返回列表 |
| 查看: 1671 | 回復(fù): 3 | ||||
| 當(dāng)前只顯示滿足指定條件的回帖,點擊這里查看本話題的所有回帖 | ||||
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("按回車鍵結(jié)束程序:" ;getchar(); } } |
軟件學(xué)習(xí) |
木蟲 (正式寫手)
觀天

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

| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 一志愿西安交大材料學(xué)碩(英一數(shù)二)347,求調(diào)劑到高分子/材料相關(guān)專業(yè) +4 | zju51 2026-03-31 | 6/300 |
|
|---|---|---|---|---|
|
[考研]
材料調(diào)劑
5+6
|
想要一壺桃花水 2026-03-25 | 13/650 |
|
|
[考研] 08工科275分求調(diào)劑 +6 | AaAa7420 2026-03-31 | 6/300 |
|
|
[考研] 安徽大學(xué)專碩生物與醫(yī)藥專業(yè)(086000)324分,英語已過四六級,六級521,求調(diào)劑 +10 | 美味可樂雞翅 2026-03-26 | 11/550 |
|
|
[考研] 311求調(diào)劑一志愿合肥工業(yè)大學(xué) +11 | 秋二十二 2026-03-30 | 11/550 |
|
|
[考研] 08開頭看過來。! +3 | wwwwffffff 2026-03-31 | 5/250 |
|
|
[考研] 080500-315分復(fù)試調(diào)劑 +9 | 上岸3821 2026-03-31 | 9/450 |
|
|
[考研] 求收留 +8 | 1943443204 2026-03-28 | 8/400 |
|
|
[考研] 生物學(xué) 296 求調(diào)劑 +7 | 朵朵- 2026-03-26 | 9/450 |
|
|
[考研] 0856調(diào)劑 +6 | 曲聽筠 2026-03-30 | 6/300 |
|
|
[考研] 085601一志愿中山大學(xué)深圳材料工程330求調(diào)劑 +5 | pipiver 2026-03-30 | 5/250 |
|
|
[有機(jī)交流] 考研調(diào)劑 +8 | watb 2026-03-26 | 8/400 |
|
|
[考研] 317分 一志愿南理工材料工程 本科湖工大 求調(diào)劑 +12 | 芋泥小鈴鐺 2026-03-28 | 12/600 |
|
|
[考研] 總分293求調(diào)劑 +8 | 加一一九 2026-03-25 | 11/550 |
|
|
[考研] 數(shù)一英一271專碩(085401)求調(diào)劑,可跨 +7 | 前行必有光 2026-03-28 | 8/400 |
|
|
[考研] 本科新能源科學(xué)與工程,一志愿華理能動285求調(diào)劑 +3 | AZMK 2026-03-27 | 5/250 |
|
|
[考研] 312,生物學(xué)求調(diào)劑 +3 | 小譯同學(xué)abc 2026-03-28 | 3/150 |
|
|
[考研] 265求調(diào)劑 +8 | 小木蟲085600 2026-03-27 | 8/400 |
|
|
[考研] 機(jī)械學(xué)碩310分,數(shù)一英一,一志愿211本科雙非找調(diào)劑信息 +3 | @357 2026-03-25 | 3/150 |
|
|
[考研] 303求調(diào)劑 +6 | 藍(lán)山月 2026-03-25 | 6/300 |
|