Brute Force for finding optimal value?
30 次查看(过去 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 ?
0 个评论
采纳的回答
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
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.
更多回答(1 个)
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 个评论
Walter Roberson
2022-3-28
number_of_values_to_test = 2 * typecast(pi, 'uint64') + 1
"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 Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!