How to set resolution for the numerical calculations in MATLAB

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 commentaire

Roger Stafford
Roger Stafford le 23 Jan 2014
Modifié(e) : Azzi Abdelmalek le 24 Jan 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:

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 23 Jan 2014
You would need to use Symbolic Toolbox, or one of John D'Errico's variable-precision packages in the File Exchange.

Plus de réponses (1)

sol=roots([1 3 1])
sol1=sprintf('%.20f,',sol)

6 commentaires

It seems that sprintf function only determines the number of decimals for the display, right?
What is the resolution for this roots function? Can this resolution be increased? Thanks
John D'Errico
John D'Errico le 23 Jan 2014
Modifié(e) : John D'Errico le 23 Jan 2014
Roots uses double precision (so about 16 digits) like all arithmetic in MATLAB. This is not something you can increase. Yes, sprintf only allows you to print a lesser number of digits. Those extra digits you could get out of sprintf are floating point trash, meaningless digits, so this answer by Azzi is of no "significance".
Just a quick question to follow up: In the calculation of some symbolic expressions, I ended up having the number: 913958311492758060278303784163082240 as the coefficient of one term. Does this number contain floating point trash?
Maybe. It is
(2)^21 * (5) * (7)^4 * (13) * (17)^2 * (103) * (653)^2 * (839) * (857)^2 * (357031)
which has no obvious pattern that might arise from multiplying factors.
What polynomial are you finding the root of?
I am trying to find the roots of two multinomials, say f(T1,T2)=0 and g(T1,T2)=0. f(T1,T2)=0 is calculated from the Routh array of one system and g(T1,T2)=0 is obtained the same way after some further manipulations. The number above is observed as the coefficient of one entry when I was calculating the determinant of the sylvester matrix of the two multninomials eliminating T2.
Sorry for the complexity. But this is what my objective is.
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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by