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

3 次查看(过去 30 天)
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 !

采纳的回答

Tuong Nguyen Minh
Tuong Nguyen Minh 2021-9-20
编辑:Tuong Nguyen Minh 2021-9-20
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by