Brute Force for finding optimal value?

39 views (last 30 days)
Sir, I have a optimization parameter say, x in range [-pi, pi]. I have to find optimal value of x such that it maximizes the function f(x).
I want to incorporate brute force on it. How to write the code? Also can we use fmincon? Is it brute-force ?

Accepted Answer

Matt J
Matt J on 28 Mar 2022
Edited: Matt J on 28 Mar 2022
fmincon is not brute force, but it would be overkill for a single-variable problem. You should use fminbnd instead.
How to do it by brute force depends on whether f() is vectorized. If it is, you could just do,
N=1000; %number of samples to brute force search
xsamples=linspace(-pi,pi,N);
fsamples=f(xsamples);
[fmax,imax]=max(fsamples);
xmax=xsamples(imax);
If f() is not vectorized, then fsample would instead be computed according to,
fsamples=arrayfun(@f,xsamples);
  4 Comments
Torsten
Torsten on 28 Mar 2022
I have a function Say, f(x)=sin(x). and I need 10 values, i.e. x=1:1:10.
and x lie in range [-pi,pi]
If x is 1:1:10, x lies in the range 1:10. Sorry, but it's impossible to understand what you intend to do.
Chetan Fadnis
Chetan Fadnis on 28 Mar 2022
Sorry for the typing mistake. Kindly do needful.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 28 Mar 2022
Brute force means to try every possible solution, at least until an acceptable solution is found.
Now suppose you have the function
f = @(x) 1 - (x==0.000086789148348975)
By examination we know that it has a minima at exactly one point. We can also see that knowing the value at any other point does not help us find the minimum. If we are not able to examine the source (or if it is too complicated to reason over) then we would have to systematically try every representable number in the range -pi to +pi.
Unfortunately that is more than 8*10^18 possible points. There is no practical way to try all of those points.
You should give up on the idea of using brute force for general optimization. It can sometimes be used when the possible inputs are quantized. For example if the situation was such that you only needed to check 10^9 points then doing so might not be fast, but it might be realistic to do overnight.
  2 Comments
Chetan Fadnis
Chetan Fadnis on 28 Mar 2022
I have a function Say, f(x)=sin(x). and I need 10 values, i.e. x=1:1:10.
and x lie in range [-pi,pi]. The final result after applying brute force will be of order 1*10, and all x values to be maximum. How to incorporate it, using brute force?
Regards and thanks in anticipation.
Walter Roberson
Walter Roberson on 28 Mar 2022
number_of_values_to_test = 2 * typecast(pi, 'uint64') + 1
number_of_values_to_test = uint64 9228513313104091697
"Brute force" would require testing that many different possibilities in the general case.
There are other cases. For example it might happen to be known that no two roots of an equation were less than 1e-6 apart, then you could divide the range up into segments of (1e-6)*(1-eps) each and do a zero-finding search over each segment. That too could be considered brute force.

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox 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!