How to call objective function in outputfcn or plotfcn during optimization?

I want to minimize a complicated objective function. It returns 1. objective function value and 2. some fit data (like the fitted line from a ls regression). I want to use the 'OutputFcn' or 'PlotFcn' argument to draw the fitted line given the paramters at the current iteration. Unfortunately the fields in optimValues do not contain the obj_function, so I don't know how to do it. Can anyone help?
As a MWE consider the code below which does the least squares regression numerically.
%% Random Data
x = 1:.1:20;
x = x';
e = randn(numel(x),1)*10;
y = 1 + 3*x + e;
%% Optimization
eparam = fminunc(@(param) obj_fun(param, x, y), [0 0]);
%% Plot I want drawn at every iteration
[sum_of_squared_residuals,y_pred] = obj_fun(eparam,x,y);
plot(x,y)
hold on
plot(x, y_pred)
%% Objective function
function [sum_of_squared_residuals,y_pred] = obj_fun(param,x,y)
intrcpt = param(1);
slope = param(2);
y_pred = intrcpt + slope*x;
squared_residuals = (y-y_pred).^2;
sum_of_squared_residuals = sum(squared_residuals);
end

 採用された回答

Matt J
Matt J 2021 年 5 月 3 日

0 投票

Unfortunately the fields in optimValues do not contain the obj_function
It does. It's the fval field.

6 件のコメント

Michael Stollenwerk
Michael Stollenwerk 2021 年 5 月 3 日
This is just the obj_function(current_point) and not the obj_function handle, which I would need to access the second output argument of obj_function.
Matt J
Matt J 2021 年 5 月 3 日
編集済み: Matt J 2021 年 5 月 3 日
Where does your output function reside? obj_fun can be called from any function in the same file, including from the output function, if it s defined there.
Michael Stollenwerk
Michael Stollenwerk 2021 年 5 月 3 日
編集済み: Michael Stollenwerk 2021 年 5 月 3 日
The output function is in my own my_fmincon file. The obj_fun is defined in an m-file which calls my_fmincon. I checked the workspace via debugger in the output function. obj_fun is unfortunately not there.
Matt J
Matt J 2021 年 5 月 3 日
編集済み: Matt J 2021 年 5 月 3 日
In my_fmincon, you can define the output function like so,
fmincon(objf_fun, _____,'OutputFcn',@(a1,a2,a3) outfun(a1,a2,a3, obj_fun))
function stop = outfun(x,optimValues,state, obj_fun)
....
end
You can also make outfun a nested function ithin my_fmincon, in which case it will have access to anything in my_fmincon's main workspace.
Michael Stollenwerk
Michael Stollenwerk 2021 年 5 月 3 日
Perfect, thanks! This works.
Matt J
Matt J 2021 年 5 月 3 日
You're welcome, but please Accept-click the answer to indicate so.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOptimization についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by