我的脚本中有一个fmincon优化,它通过角度phi通过(迭代)旋转坐标来对齐笛卡尔坐标系中的几何曲面。 我现在想通过使用输出函数来保存优化的输出。但它只返回一个1,1,1,1,1的矩阵...... 优化工作正常!!
这是我调用以运行优化并显示输出的函数。我使用这个example来编写输出函数,但我认为我必须调整变量x和fval到我的例子......
function [history,searchdir] = runopt_Teil2
global stepsX yyPos
global fDiffLiRe phi2
% Set up shared variables with OUTFUN
history.x = [];
history.fval = [];
searchdir = [];
% call optimization
options = optimoptions(@fmincon,'OutputFcn',@outfun,'Display', 'off', 'Algorithm','sqp','MaxIter',20);
r=1;
ss=[0 1];
oo=0;
while (ss(1)<ss(2))
o=1+oo;
Startwert=[0.05 -0.05];
[phi2,fDiffLiRe,output]=fmincon(@(phi)opt_Teil2(phi),Startwert(r),[],[],[],[],-0.1,0.1,[],options);
ss=yyPos(size(stepsX));
r=r+1;
if (ss(1)>ss(2))
oo=1;
break
end
end
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold on
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
% Concatenate current search direction with
% searchdir.
searchdir = [searchdir;...
optimValues.searchdirection'];
% plot(x(1),x(2),'o');
% Label points with iteration number and add title.
% Add .15 to x(1) to separate label from plotted 'o'
% text(x(1)+.15,x(2),...
num2str(optimValues.iteration);
title('Sequence of Points Computed by fmincon');
case 'done'
hold off
otherwise
end
end
end
这是优化功能
function fDiffLiRe=opt_Teil2(phi)
global xNeg yNeg xPos yPos stepsX yyPos yyNeg fLiDiff fReDiff fistLi fistRe fsollLi fsollRe xxxPos xxxNeg yyyPos
global yyyNeg
global interpolationsmethode
%Rotation of coordinates
xxxPos=xPos*cos(phi)+yPos*sin(phi);
yyyPos=-xPos*sin(phi)+yPos*cos(phi);
xxxNeg=xNeg*cos(phi)+yNeg*sin(phi);
yyyNeg=-xNeg*sin(phi)+yNeg*cos(phi);
% Interpolation
yyPos = interp1(yyyPos,xxxPos,stepsX, interpolationsmethode);
yyNeg = interp1(yyyNeg,xxxNeg,stepsX, interpolationsmethode);
%Shape surfaces left
fistLi=trapz(stepsX,yyNeg);
fsollLi=-0.93;
fLiDiff=fsollLi-fistLi;
%Shape surfaces right
fistRe=trapz(stepsX,yyPos');
fsollRe=0.93;
fReDiff=fsollRe-fistRe;
fDiffLiRe=(fReDiff+fLiDiff)^2;
你可以告诉我哪里弄错了吗?
非常感谢你!最好的祝福,
Oensen