How to evaluate a multivariable function at different value of the variable effectively ?

3 vues (au cours des 30 derniers jours)
I am having a function f() this function is complicated function and its varialbles are epxi, beta, a1_t2, a2_t3, and xai. All of these variable are from 0 to 1. However, because some NaN problem, I decided to change it a little bit that is all the variable are from 0.1 to 0.9.
epxi = [0.1:0.1:0.9];
beta = [0.1:0.1:0.9];
a1_t2 = [0.1:0.1:0.9];
a2_t3 = [0.1:0.1:0.9];
xai = [0.1:0.1:0.9];
I want to evaluate my function f at all posible combination of epxi, beta, a1_t2, a2_t3, and xai to find out the max value effectively ( I have a multi-cores computer) but dont know how to do this.
Currently, my implemenation is quite naive and bad:
for c1 = 1:length(epxi)
for c2 = 1:length(beta)
for c3 = 1:length(a1_t2)
for c4 = 1:length(a2_t3)
for c5 = 1:length(xai)
temp = f(epxi(c1),beta(c2),a1_t2(c3),a2_t3(c4),xai(c5);
result = [result temp];
end
end
end
end
end
Particularly, f() here is the function of interest. I store the evaluation to an array that keep expanding. The expanding array is the variable result.
Note that this function f is not a symbolic function.
I intended to use the built-in matlab function max on the result vector but I understand that this implementation is not efficient. I guess that some pre-allocation of the variable result would be more efficient but fail to do so.
Please help me with this !
Thank you for your enthusiasm !

Réponse acceptée

Tuong Nguyen Minh
Tuong Nguyen Minh le 20 Sep 2021
Modifié(e) : Tuong Nguyen Minh le 20 Sep 2021
I have finally found a way to do it by using the built-in combvec function
all_combination= combvec(epxi,beta,a1_t2,a2_t3,xai)';
function_value_array = zeros(1,length(epxi)*length(beta)*length(a1_t2)*length(a2_t3)*length(xai));
parfor jj = 1:length(function_value_array)
function_value_array(jj) = TotalSTP_Exhaustive(all_combination(jj,1),all_combination(jj,2),all_combination(jj,3),all_combination(jj,4),all_combination(jj,5));
end
y = max(function_value_array);

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by