matlab中 fmincon PlotFcn显示每次迭代结果图
在 fmincon 函数中,可以使用 PlotFcn 参数来指定一个绘图函数,以在每次迭代时绘制收敛图。具体步骤如下:
- 定义一个绘图函数,例如:
function stop = plotfun(x,optimValues,state)
persistent history
switch state
case 'init'
history.fval = [];
history.x = [];
case 'iter'
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
plot(history.fval,'o-');
xlabel('iteration');
ylabel('objective function');
drawnow;
end
stop = false;
- 在 fmincon 函数中设置 PlotFcn 参数为该绘图函数:
x0 = [1,1];
lb = [-1,-1];
ub = [3,3];
options = optimoptions('fmincon','PlotFcn',@plotfun);
[x,fval,exitflag,output] = fmincon(@rosenbrock,x0,[],[],[],[],lb,ub,[],options);
这样,每次迭代都会在当前图形窗口中绘制一个新的点,并实时更新收敛曲线。
原文地址: https://www.cveoy.top/t/topic/b15g 著作权归作者所有。请勿转载和采集!