How to save a variable from an anonymous function?

조회 수: 4 (최근 30일)
Maria Sanz
Maria Sanz 2019년 7월 19일
댓글: Walter Roberson 2019년 9월 20일
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.

채택된 답변

Walter Roberson
Walter Roberson 2019년 7월 23일
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.

추가 답변 (1개)

Stephan
Stephan 2019년 7월 19일
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
Maria Sanz
Maria Sanz 2019년 7월 23일
편집: Maria Sanz 2019년 7월 23일
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 2019년 9월 20일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver-Based Nonlinear Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by