# How can I do Exhaustive search in matlab?

58 views (last 30 days)
Adi Nor on 21 Nov 2019
Commented: QUANG LE on 6 Oct 2020
I have a function that depends on three variables x,y,z. I want to find the optimal values of these variables using exhaustive search that can maximize this function, subject to x+y+z<=1. How can I do this in Matlab?

Walter Roberson on 21 Nov 2019
xvalues = [list all possible x values]
yvalues = [list all possible y values]
zvalues = [list all possible z values];
[X, Y, Z] = ndgrid(xvalues, yvalues, zvalues);
mask = X + Y + Z <=1;
f = x.^3 - log(y+z) + sin(z); %compute everything. %use appropriate function
[bestf, bestidx] = min(f); %or max(f) depending what you are trying to optimize
bestx = x(bestidx);
besty = y(bestidx);
bestz = z(bestidx);
QUANG LE on 6 Oct 2020
Thanks so much, it works.

### Categories

Find more on Direct Search in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by