Main Content

このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。

プロット関数

最適化実行時のプロット表示

ソルバーの実行中に進捗状況のさまざまな測定をプロットできます。PlotFcn の名前/値のペアを optimoptions で設定し、各反復でソルバーが呼び出すプロット関数を 1 つまたは複数指定します。関数ハンドルか、関数ハンドルの cell 配列を渡します。

さまざまな定義済みプロット関数を使用できます。ソルバー関数のリファレンス ページで PlotFcn オプションの説明を参照してください。

また、カスタムのプロット関数を使用することもできます。出力関数と同じ構造を使用して関数ファイルを記述します。この構造については、出力関数とプロット関数の構文を参照してください。

プロット関数を使用

この例では、プロット関数を使用して、fmincon 'interior-point' アルゴリズムの進行状況を表示する方法を示します。問題は、[最適化] ライブ エディター タスクまたはソルバーを使用した制約付き非線形問題 から取得されます。

勾配を含めて、非線形の目的関数と制約関数を記述します。目的関数は Rosenbrock 関数です。

type rosenbrockwithgrad
function [f,g] = rosenbrockwithgrad(x)
% Calculate objective f
f = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;

if nargout > 1 % gradient required
    g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
        200*(x(2)-x(1)^2)];
end

このファイルを rosenbrockwithgrad.m として保存します。

制約関数は、解が norm(x)^2 <= 1 を満たすものになります。

type unitdisk2
function [c,ceq,gc,gceq] = unitdisk2(x)
c = x(1)^2 + x(2)^2 - 1;
ceq = [ ];

if nargout > 2
    gc = [2*x(1);2*x(2)];
    gceq = [];
end

このファイルを unitdisk2.m として保存します。

3 つのプロット関数の呼び出しを含む、ソルバーのオプションを作成します。

options = optimoptions(@fmincon,'Algorithm','interior-point',...
 'SpecifyObjectiveGradient',true,'SpecifyConstraintGradient',true,...
 'PlotFcn',{@optimplotx,@optimplotfval,@optimplotfirstorderopt});

初期点 x0 = [0,0] を作成し、残りの入力を空 ([]) に設定します。

x0 = [0,0];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];

オプションを含む、fmincon を呼び出します。

fun = @rosenbrockwithgrad;
nonlcon = @unitdisk2;
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)

{"String":"Figure Optimization Plot Function contains 3 axes objects. Axes object 1 with title Current Point contains an object of type bar. Axes object 2 with title Current Function Value: 0.0456748 contains an object of type line. Axes object 3 with title First-order Optimality: 2.16246e-08 contains an object of type line.","Tex":[],"LaTex":[]}

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2

    0.7864    0.6177

関連するトピック