在 fmincon 函数中,可以使用 PlotFcn 参数来指定一个绘图函数,以在每次迭代时绘制收敛图。具体步骤如下:

  1. 定义一个绘图函数,例如:
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;
  1. 在 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 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录