fminsearch to optimise n'th output in Matlab

87 views Asked by At

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?

1

There are 1 answers

1
Andrey Rubshtein On

If your function is called foo:

function foo(i,...,x)

end

You can either define a named function:

function foo_x(x)
   foo(...,x);
end

Or use anonymous functions:

@(x) foo(...,x)

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.