How to save a variable from an anonymous function?

4 views (last 30 days)
I am using the function fmincon to minimize the function f:
[X,f]=fmincon(@(X) obj_function(X,P,Pd,intervalotiempo),X0,A,B,Aeq,Beq,LB,UB,@(X)nonlcon(X,Potnominal,intervalotiempo,Pd));
fmincon requires the use of anonymous funtions, therefore the variables that are used in nonlcon are not stored after runing the program.
I would like to know if it is possible to save (get it as an output) one varible from the function nonlcon.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Jul 2019
There is no mechanism for returning values from nonlcon.
You can any of the usual techniques for saving data from a function. For example you could make nonlcon a nested function with a shared variable.
Note that the objective function may be invoked multiple times without a nonlcon invocation, and that nonlcon may get invoked multiple times without invoking the objective function, including at locations that the objective function is not called for. You should not count on the calls to the two functions corresponding.

More Answers (1)

Stephan
Stephan on 19 Jul 2019
for nonlcon it's not necessarily an anonymous function that is needed. you can also use a "normal" function, so that you can save the needed values or display them.
  3 Comments
Maria Sanz
Maria Sanz on 23 Jul 2019
Edited: Maria Sanz on 23 Jul 2019
And how can I rewrite: @(X)nonlcon(X,Potnominal,intervalotiempo,Pd) in a way that an anonymous function is not needed?
I need that X varies during the execution of fmincon.
Walter Roberson
Walter Roberson on 20 Sep 2019
function driver
various things here
Potnominal = value;
intervalotiempo = value;
Pd = value;
stored_intermediate_results = {};
[x, fval] = fmincon(@objective_function, A, b, Aeq, beq, lb, ub, @nonlcon, options);
save('AppropriateName.mat', 'x', 'fval', 'stored_intermediate_results');
function [c, ceq] = nonlcon(X)
%here Potnominal, intervalotiempo, Pd, stored_intermediate_results are all shared variables
temp1 = some expression in X and Potnominal and intervalotiempo and Pd
temp2 = some expression in X and Potnominal and intervalotiempo and Pd
c = some expression in X and Potnominal and intervalotiempo and Pd and temp1 and temp2
ceq = some expression in X and Potnominal and intervalotiempo and Pd and temp1 and temp2
stored_intermediate_results(end+1) = {X, temp1, temp2};
end
end
No anonymous function. But it would not have mattered, because you could instead use
function driver
various things here
Potnominal = value;
intervalotiempo = value;
Pd = value;
stored_intermediate_results = {};
[x, fval] = fmincon(@objective_function, A, b, Aeq, beq, lb, ub, @(X)nonlcon(X,Potnominal,intervalotiempo,Pd), options);
save('AppropriateName.mat', 'x', 'fval', 'stored_intermediate_results');
function [c, ceq] = nonlcon(X,Potnominal,intervalotiempo,Pd)
%here Potnominal, intervalotiempo, Pd are all parameters
%here stored_intermediate_results is a shared variable
temp1 = some expression in X and Potnominal and intervalotiempo and Pd
temp2 = some expression in X and Potnominal and intervalotiempo and Pd
c = some expression in X and Potnominal and intervalotiempo and Pd and temp1 and temp2
ceq = some expression in X and Potnominal and intervalotiempo and Pd and temp1 and temp2
stored_intermediate_results(end+1) = {X, temp1, temp2};
end
end
There is a small execution time difference between these two, with the parameter version being slightly faster than the shared variable version.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!