3.1 創(chuàng)建和使用輸出函數(shù)
????????下面給出了輸出函數(shù)的一個(gè)簡單示例,該輸出函數(shù)繪制優(yōu)化函數(shù)生成的點(diǎn)。
function stop = outfun(x, optimValues, state)
stop = false;
hold on;
plot(x(1),x(2),'.');
drawnow
????????在解算以下優(yōu)化問題時(shí),可以使用此輸出函數(shù)繪制 fminsearch
生成的點(diǎn)
????????為此,
????????1 創(chuàng)建一個(gè)包含前述代碼的文件,并將其作為
outfun.m
保存在 MATLAB 路徑的文件夾中。
????????2 將
options
結(jié)構(gòu)體的
Outputfcn
字段的值設(shè)置為
outfun
的函數(shù)句柄。
options = optimset(
'OutputFcn'
, @outfun);
????????3
輸入以下命令:
hold
on
objfun=@(x) exp(x(1))*(4*x(1)^2+2*x(2)^2+x(1)*x(2)+2*x(2));
[x fval] = fminsearch(objfun, [-1 1], options)
hold
off
????????這些命令返回解
x =
0.1290 -0.5323
fval =
-0.5689
????????并顯示以下關(guān)于 fminsearch
生成的點(diǎn)的繪圖:
3.2 輸出函數(shù)的結(jié)構(gòu)體
????????輸出函數(shù)的函數(shù)定義行采用以下格式:
stop = outfun(x, optimValues, state)
????????其中
????????? stop
是一個(gè)標(biāo)志,根據(jù)優(yōu)化例程是停止還是繼續(xù),該標(biāo)志為
true
或
false。。
????????? x
是算法在當(dāng)前迭代中計(jì)算的點(diǎn)。
????????? optimValues 是包含當(dāng)前迭代中的數(shù)據(jù)的結(jié)構(gòu)體。
????????? state
是算法的當(dāng)前狀態(tài)。。
????????優(yōu)化函數(shù)在每次迭代中將輸入?yún)?shù)的值傳遞給 outfun
。
3.3 嵌套輸出函數(shù)的示例
????????示例不需要輸出函數(shù)在每次迭代后保留數(shù)據(jù)。如果不需要保存各次迭代之間的數(shù)據(jù),可以將輸出函數(shù)編寫為函數(shù)文件,并從命令行直接調(diào)用優(yōu)化函數(shù)。但是,要使輸出函數(shù)在每次迭代后記錄數(shù)據(jù),請編寫一個(gè)用于實(shí)現(xiàn)以下目的的文件:
????????? 以嵌套函數(shù)的形式包括輸出函數(shù) - 有關(guān)詳細(xì)信息,請參閱 MATLAB 編程基礎(chǔ)中的“嵌套函數(shù)”。
????????? 調(diào)用優(yōu)化函數(shù)。
????????在以下示例中,函數(shù)文件還包含目標(biāo)函數(shù)作為局部函數(shù)。您也可以將目標(biāo)函數(shù)編寫為單獨(dú)的文件或匿名函數(shù)。
????????嵌套函數(shù)可以訪問其所在的文件中的變量。因此,此方法使輸出函數(shù)能夠在每次迭代后保留變量。
????????以下示例使用輸出函數(shù)記錄以下求解中的 fminsearch
迭代:

????????輸出函數(shù)以矩陣(稱為 history)的形式返回點(diǎn)序列。要運(yùn)行此示例,請執(zhí)行下列步驟:
????????1使用 MATLAB 編輯器打開一個(gè)新文件。
????????2 將以下代碼復(fù)制并粘貼到此文件。
function
[x fval history] = myproblem(x0)
history = [];
options = optimset(
'OutputFcn'
, @myoutput);
[x fval] = fminsearch(@objfun, x0,options);
function
stop = myoutput(x,optimvalues,state);
stop = false;
if
isequal(state,
'iter'
)
history = [history; x];
end
end
function
z = objfun(x)
z = exp(x(1))*(4*x(1)^2+2*x(2)^2+x(1)*x(2)+2*x(2));
end
end
????????3 將文件作為
myproblem.m
保存到 MATLAB 路徑上的某個(gè)文件夾中。
????????4 在 MATLAB 提示符下,輸入
????????[x fval history] = myproblem([-1 1]);
????????函數(shù) fminsearch
返回最佳點(diǎn)
x
及 x 處的目標(biāo)函數(shù)值
fval
。
x,fval
x =
0.1290 -0.5323
fval =
-0.5689
????????此外,輸出函數(shù) myoutput
向 MATLAB 工作區(qū)返回矩陣
history,該矩陣包含算法在每次迭代中生成的點(diǎn)。
history
的前四行為
history(1:4,:)
ans =
-1.0000 1.0000
-1.0000 1.0000
-1.0750 0.9000
-1.0125 0.8500
????????history 最后一行的點(diǎn)與最佳點(diǎn)
x
相同。
history(end,:)
ans =
0.1290 -0.5323
objfun(history(end,:))
ans =
-0.5689
3.4 optimValues 中的字段
????????下表列出了由優(yōu)化函數(shù) fminbnd
、
fminsearch
和
fzero
提供的
optimValues 結(jié)構(gòu)體的字段。表的“命令行顯示標(biāo)題”列中列出了將
options
的
Display
參數(shù)設(shè)置為
'iter'
時(shí)顯示的標(biāo)題。
3.5 算法的狀態(tài)
????????下表列出了 state
的可能值:
?
文章來源:http://www.zghlxwxcb.cn/news/detail-669937.html
????????下面的代碼演示輸出函數(shù)如何使用 state
的值來確定要在當(dāng)前迭代中執(zhí)行的任務(wù)。
switch
state
case
'init'
% Setup for plots or dialog boxes
case
'iter'
% Make updates to plots or dialog boxes as needed
case
'interrupt'
% Check conditions to see whether optimization
% should quit
case
'done'
% Cleanup of plots, dialog boxes, or final plot
end
3.6 Stop 標(biāo)簽
????????輸出參數(shù) stop
是
true
或
false
的標(biāo)簽。此標(biāo)志通知優(yōu)化函數(shù)優(yōu)化是停止 (
true
) 還是繼續(xù) (
false)。下面的示例演示了使用
stop
標(biāo)簽的典型方法。
????????1)根據(jù) optimValues 中的數(shù)據(jù)停止優(yōu)化
????????輸出函數(shù)可以根據(jù) optimValues 中的當(dāng)前數(shù)據(jù)在任何迭代中停止優(yōu)化。例如,下面的代碼在目標(biāo)函數(shù)值小于
5
時(shí)將
stop
設(shè)置為
true
:
function
stop = myoutput(x, optimValues, state)
stop = false;
% Check if objective function is less than 5.
if
optimValues.fval < 5
stop = true;
end
? ? ? ? 2)基于對話框輸入停止優(yōu)化
????????在設(shè)計(jì) UI 來執(zhí)行優(yōu)化時(shí),可以采用控件(例如停止按鈕)使輸出函數(shù)停止優(yōu)化。以下代碼顯示如何執(zhí)行此回調(diào)。代碼假定
停止
按鈕回調(diào)將值
true
存儲在名為
hObject
的
handles
結(jié)構(gòu)體的
optimstop 字段中,而該結(jié)構(gòu)體又存儲在
appdata
中。
function
stop = myoutput(x, optimValues, state)
stop = false;
% Check if user has requested to stop the optimization.
stop = getappdata(hObject,
'optimstop'
);
4.優(yōu)化求解器繪制函數(shù)
????????options 結(jié)構(gòu)體的 PlotFcns 字段指定優(yōu)化函數(shù)在每次迭代時(shí)調(diào)用的一個(gè)或多個(gè)函數(shù),用于繪制各種進(jìn)度測度。傳遞函數(shù)句柄或函數(shù)句柄的元胞數(shù)組。繪圖函數(shù)的結(jié)構(gòu)體與輸出函數(shù)的結(jié)構(gòu)體相同。
????????PlotFcns 選項(xiàng)可以與下列 MATLAB 優(yōu)化函數(shù)配合使用:
????????? fminbnd
????????? fminsearch
????????? fzero
????????這些優(yōu)化函數(shù)的預(yù)定義繪圖函數(shù)包括:
????????? @optimplotx
繪制當(dāng)前點(diǎn)
????????? @optimplotfval
繪制函數(shù)值
????????? @optimplotfunccount
繪制函數(shù)計(jì)數(shù)(不適用于
fzero
)
????????要查看或修改預(yù)定義的繪圖函數(shù),請使用 MATLAB 編輯器打開函數(shù)文件。例如,要查看函數(shù)文件以便繪制當(dāng)前點(diǎn),請輸入:
edit optimplotx.m
? ? ? ? 例如,想要查看使用 fminsearch
與繪圖函數(shù)
@optimplotfval
求最小值的進(jìn)度:
????????1
為目標(biāo)函數(shù)編寫一個(gè)文件。在本示例中,使用:
function
f = onehump(x)
r = x(1)^2 + x(2)^2;
s = exp(-r);
f = x(1)*s+r/20;
????????2
設(shè)置 options 以便使用繪圖函數(shù):
options = optimset(
'PlotFcns'
,@optimplotfval);
????????3
從 [2,1] 起調(diào)用
fminsearch
:
[x ffinal] = fminsearch(@onehump,[2,1],options)
????????4
MATLAB 返回以下內(nèi)容:
x =
-0.6691 0.0000
ffinal =
-0.4052
文章來源地址http://www.zghlxwxcb.cn/news/detail-669937.html
到了這里,關(guān)于matlab使用教程(22)—非線性優(yōu)化函數(shù)的設(shè)置的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!