Can one run fminsearch for a function defined by cases?

1 回表示 (過去 30 日間)
alpedhuez
alpedhuez 2020 年 7 月 28 日
コメント済み: Matt J 2020 年 7 月 28 日
First, I have a function defined by cases like
if x<1 f(x)=x
if x>1 f(x)=sqrt(x)
Second, suppose I want to calculate the maximium of f for 0<x<2 (this problem itself is trivial, but as an example).
Third, can one run fminsearch for such problem?

採用された回答

Matt J
Matt J 2020 年 7 月 28 日
編集済み: Matt J 2020 年 7 月 28 日
Because you are optimizing over a known bounded interval fminbnd is more appropriate:
x_optimal = fminbnd(@fun,0,2)
function out=fun(x)
if x<=1 out=x; else out=sqrt(x); end
out=-out; %because we're maximizing
end
  2 件のコメント
alpedhuez
alpedhuez 2020 年 7 月 28 日
編集済み: alpedhuez 2020 年 7 月 28 日
Thank you. Then what should one do when one maximizes over a finite set of possible x's? Like 0,0.01,0.02,...,1?
Matt J
Matt J 2020 年 7 月 28 日
For that, you would just use max().

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2020 年 7 月 28 日
編集済み: John D'Errico 2020 年 7 月 28 日
Can you? Yes.
Should you? This may sometimes be a poor idea, although it depends on the specific segment functions. Are the segments continuous across the break? Are they differentiable across the break?
Fminsearch does not require a several times continuously differentiable objective. It is even reasonably robust in that respect. But if your function is discontinuous across a break, then expect that it can easily fail. The nastier is your function, the more chance of failure.
What does failure mean here? It does not mean the algorithm will fail due to an error message. It just means fminsearch may not consistently find the optimal solution. It could get more easily stuck in a sub-optimal solution. Note there is NO assurance of convergence to the globally optimal solution for fminsearch (or virtually any optimizer) even on a more well-behaved problem, just that if you make things messy it may fail more easily.
If your problem is a 1-dimensional one, then it is advisable to best break down the problem into sub-intervals, optimizing over each interval, and to then use fminbnd for each interval. This will yield the best possile result. Using fminbnd is good here, because it employs a bracket to search over. You then choose the overall best choice of all solutions returned. In the case of a bracket that extends to -inf or +inf, just choose a reasonably large number, but not excessively large, as that can cause numerical problems.
  1 件のコメント
alpedhuez
alpedhuez 2020 年 7 月 28 日
Yes but the real problem is such that one cannot divide into subintervals of the real line.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOptimization についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by