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

2 views (last 30 days)
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 !

Accepted Answer

Tuong Nguyen Minh
Tuong Nguyen Minh on 20 Sep 2021
Edited: Tuong Nguyen Minh on 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);

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!