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

16 Ansichten (letzte 30 Tage)
Ahmad Alalyani
Ahmad Alalyani am 19 Jul. 2018
Bearbeitet: Matt J am 20 Jul. 2018
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 Kommentare
Matt J
Matt J am 19 Jul. 2018
But, the program keeps using the default TolX !!
What evidence of that do you see?
Ahmad Alalyani
Ahmad Alalyani am 19 Jul. 2018
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.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Aquatris
Aquatris am 19 Jul. 2018
Bearbeitet: Aquatris am 19 Jul. 2018
Feed "optnew" to the fzero function, not "options". You are not changing the setting of options with that command.
  3 Kommentare
Aquatris
Aquatris am 19 Jul. 2018
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 am 20 Jul. 2018
Bearbeitet: Matt J am 20 Jul. 2018
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

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 19 Jul. 2018
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 am 19 Jul. 2018
Bearbeitet: Matt J am 19 Jul. 2018
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.

Kategorien

Mehr zu Behavior and Psychophysics finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by