MATLAB: How to minimise function with many outputs

fmincon

I have an anonymous function which produces multiple outputs. How do I use fmincon to minimise with respect to the first output?

Best Answer

Where you currently have
fmincon(HandleToAnonymousFunction, ......)
use
fmincon(@(X) HandleToAnonymousFunction(X), .....)
This will have the effect of discarding all except the first output.
I am assuming here that when you say that it produces multiple outputs, that you mean that the current function does something like deal() to emit multiple output variables, or else is a simple handle to a function that has multiple potential outputs such as min: @min would be an example of an anonymous function that can return multiple outputs.
If what you mean is instead that your function returns a vector or array of values instead of a scalar, then the approach would be
First = @(V) V(1);
fmincon(@(X) First(HandleToAnonymousFunction(X)), .....)