您的当前位置:首页正文

香蕉函数的各类最优解法

2024-03-08 来源:客趣旅游网
%用牛顿法求香蕉函数minf=(x2-x1^2)^2+(x2-1)^2 X(:,1)=[2,2]'; eps=0.00001; k=1; l=0%迭代的次数 x1=X(1,1);x2=X(2,1); %f=(x2-x1^2)^2+(x2-1)^2 f_val(k)=(x2-x1^2)^2+(x2-1)^2; %求梯度和hesse矩阵 g(1,1)=-4*(x2-x1^2)*x1; g(2,1)=4*x2-2*x1^2-2; q =[12*x1^2-4*x2,-4*x1;-4*x1,4]; %梯度的模 g_mo(k)=sqrt(g(1,1)^2+g(2,1)^2); while(g_mo(k)>eps) l=l+1; %确定搜索方向 p(:,k)=-inv(q)*g(:,k); %一维搜索 t=sym('t'); x1=X(1,k)+t*p(1,k); x2=X(2,k)+t*p(2,k); f=(x2-x1^2)^2+(x2-1)^2; s=diff(f,t); s1=finverse(s); t=subs(s1,'0',t); s1=0; X(:,k+1)=X(:,k)+t*p(:,k); x1=X(1,k+1);x2=X(2,k+1); f_val(k+1)=(x2-x1^2)^2+(x2-1)^2; g(1,1)=-4*(x2-x1^2)*x1; g(2,1)=4*x2-2*x1^2-2; g_mo(k+1)=sqrt(g(1,1)^2+g(2,1)^2); k=k+1; end X(:,k) l %用最速下降法求香蕉函数minf=(x2-x1^2)^2+(x2-1)^2 X(:,1)=[2,2]'; eps=0.00001; k=1; l=0; x1=X(1,1);x2=X(2,1); %f=(x2-x1^2)^2+(x2-1)^2 f_val(k)=(x2-x1^2)^2+(x2-1)^2; %求梯度和hesse矩阵 g(1,1)=-4*(x2-x1^2)*x1; g(2,1)=4*x2-2*x1^2-2; q =[12*x1^2-4*x2,-4*x1;-4*x1,4]; %梯度的模 g_mo=sqrt(g(1,1)^2+g(2,1)^2); while(g_mo>eps) %迭代 l=l+1; X(:,k+1)=X(:,k)-g'*g/(g'*q*g)*g x1=X(1,k+1);x2=X(2,k+1); f_val(k+1)=(x2-x1^2)^2+(x2-1)^2; g(1,1)=-4*(x2-x1^2)*x1; g(2,1)=4*x2-2*x1^2-2; g_mo=sqrt(g(1,1)^2+g(2,1)^2); k=k+1; end X(:,k) l %用DFP法求香蕉函数 minf(X)=(x2-x1^2)^2+(x2-1)^2 clear X(:,1)=[2,2]'; eps=0.00001; k=1; n=2; H=eye(2,2); x1=X(1,1);x2=X(2,1); %f(X)=(x2-x1^2)^2+(x2-1)^2 g(1,1)=-4*(x2-x1^2)*x1; g(2,1)=4*x2-2*x1^2-2; g_mo(k)=sqrt(g(1,1)^2+g(2,1)^2); if(g_mo(k)%用共轭梯度法求香蕉函数minf(X)=(x2-x1^2)^2+(x2-1)^2 clear X(:,1)=[2,2]'; eps=0.00001; k=1; n=2; x1=X(1,1);x2=X(2,1); %f(X)=2*x1^2+x2^2-x1*x2 g(1,1)=-4*(x2-x1^2)*x1; g(2,1)=4*x2-2*x1^2-2; g_mo(k)=sqrt(g(1,1)^2+g(2,1)^2); if(g_mo(k)黄金分割 #include #include main() {float a,b,t,t1,t2,eps,f1,f2,beta; beta=0.618; printf(\"please input a,b,eps\\n \"); scanf(\"%f,%f,%f\ t2=a+beta*(b-a); f2=t2*(t2+2); t1=a+b-t2; f1=t1*(t1+2); while(t2-t1>eps) {if(f1<=f2) { else } t=(t1+t2)/2.0; { a=t1;t1=t2; f1=f2; t2=a+beta*(b-a); f2=t2*t2+2*t2; } b=t2;t2=t1; f2=f1; t1=a+b-t2;f1=t1*(t1+2); } 二分法 #include #include main() {float a,b,c,fc,t,eps; printf(\"please input a,b,eps\\n \"); scanf(\"%f,%f,%f\ while(b-a>eps) {c=(a+b)/2.0; fc=2*c+2; if(fc<0) a=c; else if(fc>0) b=c; else if(fc==0) {t=c;break;} } if(fc!=0) t=(a+b)/2; printf(\"f(x)=x*x+2*x+1 minmum point is:\"); printf(\"%f\\n\ getch(); } printf(\"f(x)=x*x+2*x minmum point is:\"); printf(\"%f\\n\ printf(\"f(x) minmum is:%f\\n\ getch(); }

牛顿法 #include #include float f(float *x) { float fi; fi=60-10*x[0]-4*x[1]+pow(x[0],2)+pow(x[1],2)-x[0]*x[1]; return (fi); } newton(float *x,float e) { float float g0[2],b[2]={0,0},q; int i,j; g0[0]=2*x[0]-10-x[1]; g0[1]=2*x[1]-4-x[0]; printf(\"(%f\%f)\%f\(%f\%f)\\n\q=sqrt(pow(g0[0],2)+pow(g0[1],2)); if(q>e) { } else main() { float x[2]={2,2},e; printf(\"\\nInput e\\n\"); scanf(\"%f\ printf(\"\\n\x[2]\\\ f(x)\\\ g\\n\"); newton(x,e); getch(); }

printf(\"\\nx*=(%f,%f)\ min f=%f\\n\for(i=0;i<2;i++) for(j=0;j<2;j++) b[i]+=H0[i][j]*g0[j]; for(i=0;i<2;i++) x[i]=x[i]-b[i]; newton(x,e); H[2][2]={{2,-1},{-1,2}},H0[2][2]={{2.0/3.0,1.0/3.0},{1.0/3,2.0/3.0}}; }

抛物线法 #include #include float fine(float t) { float fi; fi=8*t*t*t-2*t*t-7*t+3; return (fi); } jixiao(float t1,float t2,float t0,float e) { float ti,a,b; b=(t0-t2)*fine(t1)+(t2-t1)*fine(t0)+(t1-t0)*fine(t2); ti=a/(2*b); { if(tifine(t0)) t1=ti; else { t1=ti;t2=t0,t0=(t1+t2)/2;} printf(\"%f\%f\%f\%f\\n\ if(fabs(ti-t0)>e) } else if(ti>t0) { if(fine(ti)fine(t0)) t2=ti; { t1=t0;t2=ti,t0=(t1+t2)/2;} jixiao(t1,t2,t0,e); a=(t0*t0-t2*t2)*fine(t1)+(t2*t2-t1*t1)*fine(t0)+(t1*t1-t0*t0)*fine(t2); printf(\"\\nt*=%f,min f=%f\ } main() { float t1=0,t2=2,t0,e; printf(\"\\nInput e\\n\"); scanf(\"%f\ printf(\"\\nInput t0 in [0,2]\\n\") ; scanf(\"%f\ printf(\"t1\\ t2\\ t0\\ ti\\n\"); jixiao(t1,t2,t0,e); getch(); } }

%min f(x)=x1^2+4*x2^2 用变尺度法求解 x0=[1,1]'; Q=[2,0;0,8]; g=[2,8]'; g0=g.*x0; x1=x0-(g0'*g0)/(g0'*Q*g0)*g0; g1=g.*x1; h0=eye(2,2); g2=g1; eps=10^(-6); k=1; while g2'*g2>eps s0=x1-x0; y0=g1-g0; p1=-h1*g1; t=-(g(1)*x1(1)*p1(1)+g(2)*x1(2)*p1(2))/(g(1) *p1(1)^2+g(2)*p1(2)^2); x2=x1+t*p1; g2=g.*x2; k=k+1; end disp('函数f(x)=x1^2+4*x2^2最优解为:') x2 disp('函数最小值为:') x2(1)^2+4*x2(2)^2 fprintf('用变尺度算法(DFP算法)求解一共迭代 %d 次\\n',k)

%min f(x)=x1^2+25*x2^2 用共轭梯度法求解 x0=[2,2]; eps=10^(-1); g0=[2,50]; p0=-g0.*x0; g1=g0; k=0; while g1*g1'>eps t=-(g0(1)*x0(1)*p0(1)+g0(2) *x0(2)*p0(2))/(g0(1)*p0(1)^2+g0(2)*p0(2)^2); x0=x0+t*p0; g1=g0.*x0; t0=(g1*g1')/(p0*p0'); k=k+1; end disp('函数最优解为:') x0 disp('函数最小值为:') x0(1)^2+25*x0(2)^2 fprintf('用共轭梯度法求解一共迭代 %d 次\\n',k) h1=h0+(s0*s0')/(s0'*y0)-(h0*y0*y0'*h0')/(y0'*h0*y0); p0=-g1+t0*p0;

% 坐标轮换法求 f(X)=x1^2+x2^2+x1*x2-10*x1-4*x2+60 n=2; x=[0,0]'; eps=0.1; e=eye(n); k=0; t=1; while(t>eps) t1=(10-2*x(1)+x(2))/2; x1=x+t1*e(:,1); t2=(x1(1)-2*x1(2)+4)/2; x2=x1+t2*e(:,2); t=(sum((x2-x1).^2))^(1/2); x=x2; k=k+1; end fprintf('用坐标轮换法解 min f(X)=x1^2+x2^2+x1*x2-10*x1-4*x2+60,最优解为:') x disp('最优值为:') x(1)^2+x(2)^2+x(1)*x(2)-10*x(1)-4*x(2)+60 fprintf('一共迭代了 %d 次\\n',k) 加步探索法 #include #include void main() {int t0=0,h=1,c=2,f1,f2,f3,t,t1,a,b; f1=t0*t0*t0-2*t0+1; t=t0+h; f2=t*t*t-2*t+1; while(f1>f2 && t>0){ h=c*h; h=-h; t=h+t; f3=t*t*t-2*t+1; while(f2>f3 ){//if(t<=0);break; h=c*h; t1=t; t=t+h; f1=f2; f2=t*t*t-2*t+1; } t=t+h; f1=f2; f2=t*t*t-2*t+1; } printf(\"%d %d\printf(\"\\n\"); if(t1>t){b=t1;a=t;} else {b=t;a=t1;} getchar(); }

printf(\"[%d,%d]\

因篇幅问题不能全部显示,请点此查看更多更全内容

Top