Modifying the Tolerance (TolX) inside the Matlab function file (fzero.m).

8 ビュー (過去 30 日間)
Ahmad Alalyani
Ahmad Alalyani 2018 年 7 月 19 日
編集済み: Matt J 2018 年 7 月 20 日
I wrote the code as:
f = @(x) sin(x);
a = -0.01;
b = 0.05;
x = [a b];
options = optimset;
optnew = optimset(options,'TolX',1e-12);
[b,fval,exitflag,output]=fzero(f,x,options);
But, the program keeps using the default TolX !! How can I modify the default Tolerance (TolX) to 1e-12 or to any other value?
  2 件のコメント
Matt J
Matt J 2018 年 7 月 19 日
But, the program keeps using the default TolX !!
What evidence of that do you see?
Ahmad Alalyani
Ahmad Alalyani 2018 年 7 月 19 日
When I change 1e-12 to another value such as 1e-14, I keep get the same answer. I also get the same answer by using the default TolX.

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

回答 (3 件)

Aquatris
Aquatris 2018 年 7 月 19 日
編集済み: Aquatris 2018 年 7 月 19 日
Feed "optnew" to the fzero function, not "options". You are not changing the setting of options with that command.
  3 件のコメント
Aquatris
Aquatris 2018 年 7 月 19 日
For this particular function, it does not affect the results a lot. However, for other functions, it might. For instance, if TolX is increased to 1e-2 for this function, the result is significantly different. I think the OP wanted to make sure he/she/they are able to change the parameter as wanted.
f = @(x) sin(x);
a = -0.01;
b = 0.05;
x = [a b];
options = optimset;
optnew = optimset(options,'TolX',1e-12);
[b,fval,exitflag,output]=fzero(f,x,optnew);b
b =
1.0293e-22
optnew = optimset(options,'TolX',1e-2);
[b,fval,exitflag,output]=fzero(f,x,optnew);b
b =
0.0100
Matt J
Matt J 2018 年 7 月 20 日
編集済み: Matt J 2018 年 7 月 20 日
I think the OP wanted to make sure he/she/they are able to change the parameter as wanted.
That is clearly what the OP wanted. My point though, was that the OP would have had the same worry even if the original code had been correct, because comparing the results from different TolX is a misleading test.
For this particular function, it does not affect the results a lot.
It doesn't affect it at all. The results are identical:
optnew = optimset(options,'TolX',1e-12);
b0=fzero(f,x);
b1=fzero(f,x,optnew);
isequal(b0,b1)
ans =
logical
1

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


Steven Lord
Steven Lord 2018 年 7 月 19 日
The relevant section of your code is:
options = optimset;
optnew = optimset(options,'TolX',1e-12);
[b,fval,exitflag,output]=fzero(f,x,options);
You're passing in the original options structure as the third input to fzero, not the modified copy of that original options structure created by the second line of that code segment.
Change the last line to the following and see if it respects the tolerance:
[b,fval,exitflag,output]=fzero(f,x,optnew);

Matt J
Matt J 2018 年 7 月 19 日
編集済み: Matt J 2018 年 7 月 19 日
Apart from what Steve and Aquatris mentioned, there is no guarantee that changing TolX will change the results (in fact, I get no change in the result when I fix the code in your example). It is only one of the stopping criteria that can cause the search to terminate.

カテゴリ

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