Passing extra parameters back through an optimisation function
조회 수: 9 (최근 30일)
이전 댓글 표시
Hi there, i have a bit of a problem... i'll try to explain: i have to optimise z in this case (i use ... as placeholders for the not relevant parts). so i started with this:
[z_opt] = fmincon(@(z)my_function(z,a,b),...);
then the function would be:
function my_results = my_function(z,a,b)
r1 = really_time_consuming_function_1(z,a);
r2 = really_time_consuming_function_2(z,a);
my_result = some_other_function(r1,r2,b);
so now i get my optimised z_opt and since i need both r1 and r2 in the main program as well i have to calculate them again after the optimisation:
r1 = really_time_consuming_function_1(z_opt,a);
r2 = really_time_consuming_function_2(z_opt,a);
i know, i could define global variables to pass r1 and r2 back to the main program, but i think there has to be a better way (without the use of global variables). so is there a way to pass back more than one result, but still just optimise the first one in the vector? (something like this...)
my_result = [some_other_function(r1,r2,b),r1,r2];
as far as i have seen, the "passing extra parameters" help just is about passing them through the optimisation function in the to-be-optimised-function, but not the other way round. thanks for any help,
manuel
댓글 수: 0
채택된 답변
Matt Kindig
2012년 4월 3일
One way is to wrap the call to fmincon in an outer function, and have the really_time_consuming_function's as a sub-function of this outer function, so that they share the same workspace. This way you can output variables r1 and r2 back to the main program. Something like
function [z_opt, r1, r2]=mainfcn(z,a,b, ...)
r1 = []; r2 = []; z_opt = []; %initialize vars
z_opt = fmincon(@my_function,...)
function my_result = my_function(z)
%a and b are accessible from the main function workspace
r1 = really_time_consuming_function_1(z_opt,a);
r2 = really_time_consuming_function_2(z_opt,a);
end
end
추가 답변 (2개)
Oleg Komarov
2012년 4월 3일
If I interpret it correctly, you obtain z_opt and within my_function(z,a,b) you want to skip teh following part:
r1 = really_time_consuming_function_1(z,a);
r2 = really_time_consuming_function_2(z,a);
and do directly
my_result = some_other_function(r1,r2,b);
where r1 and r2 are the 'optimal' ones. However, with this approach you skip the optimization so I guess your objective is something else.
Guessing, you simply want to return the optimized r1 and r2 in order to avoid calling
r1 = really_time_consuming_function_1(z_opt,a);
r2 = really_time_consuming_function_2(z_opt,a);
Then write the function to return multiple outputs
function [my_results,r1,r2] = my_function(z,a,b)
...
댓글 수: 0
m_si
2012년 4월 3일
댓글 수: 1
Oleg Komarov
2012년 4월 3일
[z_opt,r1,r2] = fmincon(@(z)my_function(z,a,b),...);
Should do the trick with the suggested change in my asnwer.
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel Computing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!