How to set resolution for the numerical calculations in MATLAB
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Qingbin
el 23 de En. de 2014
Comentada: Walter Roberson
el 25 de En. de 2014
My objection is to find the roots of a polynomial as precise as possible. Can I use format long (16 digits) to set the resolution? Can this resolution be set higher than this?
1 comentario
Roger Stafford
el 23 de En. de 2014
Editada: Azzi Abdelmalek
el 24 de En. de 2014
As John has indicated, there is a fundamental difference between the precision used in computation and that which is displayed. Using the 'format' command or getting output from 'sprintf' or 'fprintf' affect only the precision displayed and have nothing to do with the precision of computation. The 'double' type number has a fixed computational precision of about 16 significant digits (53 bits), while the 'single' type has a precision of 7 or so significant digits (24 bits). There is no way to alter these. However, matlab has available the Symbolic Toolbox wherein computational precision can be set at whatever number of digits are desired, though of course computation will proceed at a slower pace. Also John has available in the file exchange some functions that allow much greater computational precision. Look at:
Respuesta aceptada
Walter Roberson
el 23 de En. de 2014
You would need to use Symbolic Toolbox, or one of John D'Errico's variable-precision packages in the File Exchange.
0 comentarios
Más respuestas (1)
Azzi Abdelmalek
el 23 de En. de 2014
sol=roots([1 3 1])
sol1=sprintf('%.20f,',sol)
6 comentarios
Walter Roberson
el 25 de En. de 2014
Are you using floating point constants at the MATLAB level at any step? For example if you had
syms r
A = pi*r^2 + 2.3
then it would be the floating point approximation of pi that would be used, rather than the irrational number, and it would be the floating point approximation of 2.3 that would be used rather than 23/10.
If you do have any floating point numbers, then to avoid floating point round-off you should convert them to symbolic rationals. Do that by quoting each number and enclosing it with sym(), such as
A = sym('pi') * r^2 + sym('2.3')
Remember to do this for exponents as well, such as x^0.5 should become x^sym('0.5') or better x^sym('1/2') (or clearer still sqrt(x) ) I would need to test to be sure that sym('2.3') did exactly what was desired, but unfortunately I do not have that toolbox.
Once all the floating point numbers (or expressions which could return non-integers) have been sym()'d, then re-run the calculation; there should not be any floating point garbage.
Ver también
Categorías
Más información sobre Conversion Between Symbolic and Numeric 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!