フィルターのクリア

Max number iterations for tolerance

6 ビュー (過去 30 日間)
Jami Turnquist
Jami Turnquist 2020 年 3 月 10 日
回答済み: Matt J 2020 年 3 月 11 日
x0 = [3,4];
function [F,gradient] = MyFunc(x)
F = 10*x(1).^2 + x(2)^2;
gradient = [20*x(1), 2*x(2)];
options = optimset('TolX', 0.01);
x = fminsearch(@MyFunc, x0, options)
end
I'm tryng to find the max number of iterations needed to find a minimum for the above function, using a tolerance of 0.01. For some reaspon this code will not yield results but it will run, just not show anything. Can anyone figure out the error?

回答 (2 件)

Steven Lord
Steven Lord 2020 年 3 月 10 日
Don't call fminsearch with MyFunc as your objective function from inside MyFunc itself. Move the lines where you call optimset and fminsearch outside MyFunc.
Assuming that MyFunc was nested inside its parent function (so it can "see" the definition of x0) and that parent function called MyFunc with an input x with at least two elements, this would:
  • call MyFunc which would call fminsearch which would
  • call MyFunc which would call fminsearch which would
  • call MyFunc which would call fminsearch ...
Eventually MATLAB would reach the recursion limit and throw an error or it would crash. If you've set the recursion limit too high, it potentially could crash the machine.
  1 件のコメント
Jami Turnquist
Jami Turnquist 2020 年 3 月 10 日
Are the x values I obtain the number of iterations it takes?

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


Matt J
Matt J 2020 年 3 月 11 日
Call fminsearch with more output arguments to get information about how many iterations it took.
x0 = [3,4];
options = optimset('TolX', 1e-6);
[x,fval,exitflag, stats] = fminsearch(@MyFunc, x0, options)
function [F,gradient] = MyFunc(x)
F = 10*x(1).^2 + x(2)^2;
gradient = [20*x(1), 2*x(2)];
end
x =
1.0e-06 *
-0.2376 0.1417
fval =
5.8448e-13
exitflag =
1
stats =
struct with fields:
iterations: 64
funcCount: 124
algorithm: 'Nelder-Mead simplex direct search'
message: 'Optimization terminated:↵ the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-06 ↵ and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-04 ↵'

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by