Modifying the Tolerance (TolX) inside the Matlab function file (fzero.m).
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Matt J
el 19 de Jul. de 2018
But, the program keeps using the default TolX !!
What evidence of that do you see?
Respuestas (3)
Aquatris
el 19 de Jul. de 2018
Editada: Aquatris
el 19 de Jul. de 2018
Feed "optnew" to the fzero function, not "options". You are not changing the setting of options with that command.
3 comentarios
Aquatris
el 19 de Jul. de 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
el 20 de Jul. de 2018
Editada: Matt J
el 20 de Jul. de 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
Steven Lord
el 19 de Jul. de 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);
0 comentarios
Matt J
el 19 de Jul. de 2018
Editada: Matt J
el 19 de Jul. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Optimization en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!