Is there an elegant way to call fminsearch
to optimise the n'th output of a function? or would one need to define a new function that returns the n'th output of the original function and apply fminsearch
to this new function?
EDITED FOR CLARIFICATION:
i.e. given:
function [out1, out2] = myfunc(x)
% appropriate code
end
what is the simplest way to find the value of x that minimises out2?
If your function is called
foo
:You can either define a named function:
Or use anonymous functions:
and pass it to fminsearch.
There is another way, which is being (ab)used frequently, using local functions to assign the inputs. I don't recommend it, since it breaks a lot of good software engineering practices.