Passing extra parameters back through an optimisation function
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Matt Kindig
le 3 Avr 2012
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
Plus de réponses (2)
Oleg Komarov
le 3 Avr 2012
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 commentaires
m_si
le 3 Avr 2012
1 commentaire
Oleg Komarov
le 3 Avr 2012
[z_opt,r1,r2] = fmincon(@(z)my_function(z,a,b),...);
Should do the trick with the suggested change in my asnwer.
Voir également
Catégories
En savoir plus sur Parallel Computing Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!