1

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?

John
  • 21
  • 4
  • This: http://stackoverflow.com/a/1344794/3139711 could be an option, but if this is more elegant than your intent to define a new function is open to dispute... – knedlsepp Dec 27 '14 at 00:54
  • I'd seen that post before, but like you said, it's rather similar to defining a new function (just more generally applicable). I was hoping for more of a one line solution, but perhaps that's just not possible. – John Dec 29 '14 at 06:12

1 Answers1

0

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.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
  • thanks for the reply! It seems you're looking at optimising a single-output function for a particular input, whereas I'm trying to optimise a multiple-output function, but for just one output. i.e. I have function [out1,out2] = myfunc(x) and I want to find the value of x that minimises out2. Thanks again! – John Dec 24 '14 at 08:17