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!