Using fminsearch on a Function with Multiple Outputs

12 ビュー (過去 30 日間)
Tomppx
Tomppx 2017 年 7 月 6 日
コメント済み: Walter Roberson 2022 年 3 月 15 日
Hi,
I have a function called calculateValues, which is stored in a seperate .m file, and takes two inputs ( x and a), while producing 3 outputs ( out1, out2, and out3):
[out1, out2, out3] = calculateValues(x,a);
I would now like to use MatLab's fminsearch to minimise the third output (out3) of my function calculateValues with respect to the variable x (leaving a as just a constant, i.e. not varied as part of the optimisation). Therefore I wanted to do something like the following:
out3min = fminsearch(@(x)calculateValues(x,a),x0);
However, it seems like the default output to be minimised is out1. I guess I need to force the anonymous function @(x)calculateValues(x,a) to return only out3. Does anyone have any suggestions how it can be done? Maybe a way to nest anonymous functions to achieve this?
Many Thanks
  2 件のコメント
Ben Cook
Ben Cook 2022 年 3 月 15 日
I have a similar issue, with the difference that I would like to know what out1 and out2 are.
The best answer (and what I currently do) is to redo the final calculation, but this seems a bit "nasty" surely there is a more elegant solution?
Walter Roberson
Walter Roberson 2022 年 3 月 15 日
You could memoize() -- and you could have the function detect when nargout is 3 and return the values then.
For example,
memoized_objective = memoize(@real_objective);
memoized_objective.CacheSize = 50;
objective = @(x) memo_driver(x, memoized_objective);
[bestx, fval] = fmincon(objective, ....)
[out1, out2, out3] = memo_driver(bestx);
function varargout = memo_driver(x, memoized_objective)
[out1, out2, out3] = memoized_objective(x);
if nargout == 1
varargout{1} = out3;
else
varargout = {out1, out2, out3};
end
end

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 7 月 6 日
It is not possible to use an anonymous function to select a particular output from a function. You need to use an actual function.
function result = lastoutput(fun, varargin)
n = nargout(fun);
outs = cell(1,n);
[outs{:}] = fun(varargin{:});
result = outs{end};
And now you can
out3min = fminsearch( @(x)lastoutput(@calculateValues,x,a), x0);
  2 件のコメント
Tomppx
Tomppx 2017 年 7 月 7 日
Hi Walter, thanks for your answer. That is indeed the best way I found to do it - I wrote something similar to simply select out3, as you suggested, where the function dummyFunction is stored in a separate .m file:
function out3 = dummyFunction(x,a)
[~,~,out3] = calculateValues(x,a);
end
Is there at least a way to define this function in-line in my main file (as opposed to having ot have an extra external file) ?
Thanks again for the response.
Sean de Wolski
Sean de Wolski 2017 年 7 月 7 日
Yes, you can have a subfunction of a parent function or script (subfunctions of scripts new in 16b)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by