Brute Force for finding optimal value?

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

采纳的回答

Matt J
Matt J 2022-3-28
编辑:Matt J 2022-3-28
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 个评论
Torsten
Torsten 2022-3-28
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 2022-3-28
Sorry for the typing mistake. Kindly do needful.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-3-28
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 个评论
Chetan Fadnis
Chetan Fadnis 2022-3-28
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 2022-3-28
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by