风中追风YF 2019-05-25 20:28 采纳率: 0%
浏览 232

求大神帮忙看一下 这段MATLAB无约束优化程序有什么问题

global xpath
clear FF
x1=(-3:.1:3)'; x2=x1; N=length(x1);
for ii=1:N
for jj=1:N
FF(ii,jj)=rosen([x1(ii) x2(jj)]');
end
end
% quasi-newton
xpath=[];t0=clock;
opt=optimset('fminunc');
opt=optimset(opt,'Hessupdate','bfgs','gradobj','on','Display','Iter',...
'LargeScale','off','InitialHessType','identity',...
'MaxFunEvals',150,'OutputFcn',@outftn);
x0=[-1.9 2]';
xout1=fminunc('rosen',x0,opt); % quasi-newton
xbfgs=xpath;
% gradient search
xpath=[];
opt=optimset('fminunc');
opt=optimset(opt,'Hessupdate','steepdesc','gradobj','on','Display','Iter',...
'LargeScale','off','InitialHessType','identity','MaxFunEvals',2000,...
'MaxIter',1000,'OutputFcn',@outftn);
xout=fminunc('rosen',x0,opt);
xgs=xpath;
% hybrid GS and BFGS
xpath=[];
opt=optimset('fminunc');
opt=optimset(opt,'Hessupdate','steepdesc','gradobj','on','Display','Iter',...
'LargeScale','off','InitialHessType','identity','MaxFunEvals',5,'OutputFcn',@outftn);
xout=fminunc('rosen',x0,opt);
opt=optimset('fminunc');
opt=optimset(opt,'Hessupdate','bfgs','gradobj','on','Display','Iter',...
'LargeScale','off','InitialHessType','identity','MaxFunEvals',150,'OutputFcn',@outftn);
xout=fminunc('rosen',xout,opt);
xhyb=xpath;
figure(1);clf
contour(x1,x2,FF',[0:2:10 15:50:1000])
hold on
xlabel('x_1')
ylabel('x_2')
title('Rosenbrock with BFGS')
pause(1)
plot(x0(1),x0(2),'ro','Markersize',12); pause(1)
plot(1,1,'rs','Markersize',12); pause(1)
for ii = 1:size(xbfgs,1)
plot(xbfgs(ii,1),xbfgs(ii,2),'bd','Markersize',12); pause(1)
end
hold off
figure(2);clf
contour(x1,x2,FF',[0:2:10 15:50:1000]);
hold on
xlabel('x_1')
ylabel('x_2')
title('Rosenbrock with GS')
pause(1)
plot(x0(1),x0(2),'ro','Markersize',12); pause(1)
plot(1,1,'rs','Markersize',12); pause(1)
for ii = 1:size(xgs,1)
plot(xgs(ii,1),xgs(ii,2),'m+','Markersize',12);% pause(1)
end
hold off
figure(3);clf
contour(x1,x2,FF',[0:2:10 15:50:1000]);
hold on
xlabel('x_1')
ylabel('x_2')
title('Rosenbrock with GS(5) and BFGS')
pause(1)
plot(x0(1),x0(2),'ro','Markersize',12); pause(1)
plot(1,1,'rs','Markersize',12); pause(1)
for ii = 1:size(xhyb,1)
plot(xhyb(ii,1),xhyb(ii,2),'m+','Markersize',12); pause(1)
end
hold off
figure(4);clf
mesh(x1,x2,FF');
axis([-3 3 -3 3 0 1000])
xlabel('x_1')
ylabel('x_2')
title('Rosenbrock with BFGS')
hold on
pause
plot3(x0(1),x0(2),rosen(x0')+5,'ro','Markersize',12,'MarkerFaceColor','r'); pause(1)
plot3(1,1,rosen([1 1]),'ms','Markersize',12,'MarkerFaceColor','m'); pause(1)
for ii = 1:size(xbfgs,1)
plot3(xbfgs(ii,1),xbfgs(ii,2),rosen(xbfgs(ii,:))+5,'gd','MarkerFaceColor','g'); pause(1)
end
%for ii = 1:size(xgs,1)
% plot3(xgs(ii,1),xgs(ii,2),rosen(xgs(ii,:))+5,’m+’); pause(1);
%end
hold off
hh=get(gcf,'children');
set(hh,'View',[-177 89.861],'CameraPosition',[-0.585976 11.1811 5116.63]);
function stop = outftn(x,optimValues,state)
global xpath
stop = 0;
xpath=[xpath;x'];
end

%
function [F,G]=rosen(x)
if size(x,1)==2, x=x'; end
F=100*(x(:,2)-x(:,1).^2).^2+(1-x(:,1)).^2;
G=[100*(4*x(1)^3-4*x(1)*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1)^2)];

end

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?