Keyword: シンプレックス法, 関数の最小化
概要
本サンプルはシンプレックス法による関数の最小化を行うサンプルプログラムです。 本サンプルはNelder-Meadシンプレックス法により以下に示される関数の最小値を求めて出力します。本サンプルではx1とx2の初期値を(−1.0,1.0)として計算しています。
※本サンプルはnAG Toolbox for MATLAB®が提供する関数 e04cb() のExampleコードです。実行にはMATLAB®本体(他社製品)とnAG Toolbox for MATLAB®が必要です。
本サンプル及び関数の詳細情報は e04cb のマニュアルページをご参照ください。
Mファイル e04cb_funct.m
function [fc, user] = funct(n, xc, user) fc = exp(xc(1))*(4*xc(1)*(xc(1)+xc(2))+2*xc(2)*(xc(2) +1)+1);
- このMファイルでは関数 funct を定義しています。この関数は特定の点における関数 F の値を求めます。
Mファイル e04cb_monit.m
function [user] = monit(fmin, fmax, sim, n, ncall, serror, vratio, user)
if (user(1) ~= 0)
fprintf('\nThere have been %d function calls\n', ncall);
fprintf('The smallest function value is %10.4f\n', fmin);
fprintf('The simplex is\n');
disp(sim);
fprintf('The standard deviation in function values at the vertices
of the simplex is %10.4f\n', serror);
fprintf('The linearized volume ratio of the current simplex to the
starting one is %10.4f\n', vratio);
end
- このMファイルでは関数 monit を定義しています。関数 monit はパラメータ選択の値の出力に使用されます。
入力データ
function [fc, user] = funct(n, xc, user)
fc = exp(xc(1))*(4*xc(1)*(xc(1)+xc(2))+2*xc(2)*(xc(2) +1)+1);
function [user] = monit(fmin, fmax, sim, n, ncall, serror, vratio, user)
if (user(1) ~= 0)
fprintf('\nThere have been %d function calls\n', ncall);
fprintf('The smallest function value is %10.4f\n', fmin);
fprintf('The simplex is\n');
disp(sim);
fprintf('The standard deviation in function values at the vertices of the simplex is %10.4f\n', serror);
fprintf('The linearized volume ratio of the current simplex to the starting one is %10.4f\n', vratio);
end
x = [-1; 1];
tolf = sqrt(x02aj);
tolx = sqrt(tolf);
maxcal = int32(100);
user = [int32(0)];
[xOut, f, user, ifail] = e04cb(x, tolf, tolx, 'e04cb_funct', 'e04cb_monit', maxcal, 'user', user)
- x は最小値の位置の推定を指定しています。
- tolf は関数値の誤差の許容値を指定しています。
- tolx は空間値の誤差の許容値を指定しています。
- maxcal は関数評価の最大値を指定しています。
- user には本関数を呼び出すuser(MATLABオブジェクト)を指定しています。
- 最後に本関数を呼び出す構文を指定しています。
出力結果
xOut =
0.5000
-0.9999
f =
1.7885e-08
user =
0
ifail =
0
- xOut は最小の関数値に対応するxの値を示しています。
- f は最小の関数値を示しています。
- user は本関数を呼び出すuser(MATLABオブジェクト)を示しています。
- ifail は関数がエラーを検知しなければ"0"を出力します。
