Multiple outputs in fmincon

Hi everyone.
I'd like to know if it is somehow possible to use fmincon with a function that has more than one output. I know that from the official description of fmincon we should employ a function which has a scalar as its only output.
To be clear, I need to optimize only one of the function outputs, but at the same time I need to save some intermediate results that would be otherwise stucked inside the function.
Thanks so much in advance,
Roberto

4 commentaires

Adam
Adam le 28 Juin 2018
As far as I understand it the function you pass to fmincon is used within its own code so any 2nd output argument would just get lost anyway as fmincon itself gives no capability to return extra output arguments.
If your function passed in were part of a class then you could store intermediate results in the class object as it runs, but I'm not aware how you could do it otherwise.
Roberto Galimberti
Roberto Galimberti le 28 Juin 2018
I don't think I've understood the class thing. Anyway, someone told me that if I use global variables they can escape the function environment. Is that true?
Adam
Adam le 28 Juin 2018
Modifié(e) : Adam le 28 Juin 2018
Yes, global variables do ignore scope, though they are not advisable to use.
Creating a class can be quite simple for a purpose like this, e.g.
classdef MyClass < handle
properties
a = 0;
b = [];
end
methods
function myScalar = myFunc( obj, input1, input2,... )
% Do whatever fmincon wants and set myScalar for it
obj.a = obj.a + 7;
obj.b = [ obj.b rand ];
end
end
end
Then in your current code create an object and the function handle, to the function on your object
myObj = MyClass( );
f = @myObj.myFunc( input1, input2 )
Note, here I just give an example with two pre-defined inputs arguments. You would just use a function signature with whatever arguments you give your current function, with the additional obj at the beginning in the function definition in the class, as mentioned below.
Then pass it to fmincon as you would your current function handle.
Now, even without having use classes this is relatively simple. Your function will look just like it does now, but with a few changes:
  1. It needs to take a first argument that represents the object of the class (I always call this 'obj', but call it whatever you want).
  2. Any variables that you want to keep hold of across calls to the function when it is run in fmincon you can add as properties (here I added 'a' and 'b', but obviously name them sensibly) and then you can update these in any way you want, as shown by my example above. You just need to remember to put the 'obj.' at the front of the property and then it behaves just like a regular variable.
Then when fmincon has finished, you still have your object, myObj and you can then use its properties as
myObj.a
myObj.b
and they will have stored whatever you told them too as your function was running.
The syntax of my example may not be 100% correct as I have just typed it straight in here off the top of my head without testing it, but the general idea works fine and there is plenty of documentation for the usage of classes in a simple way like this.
Note that accessing class properties using the obj. syntax can be costly (in time) though if your function is being called a massive amount of times.
Roberto Galimberti
Roberto Galimberti le 5 Juil 2018
Thank you! I'll see if it works for my purpose!

Connectez-vous pour commenter.

Réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by