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

14 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Behavior and Psychophysics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by