How to set resolution for the numerical calculations in MATLAB
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Qingbin
am 23 Jan. 2014
Kommentiert: Walter Roberson
am 25 Jan. 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 Kommentar
Roger Stafford
am 23 Jan. 2014
Bearbeitet: Azzi Abdelmalek
am 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:
Akzeptierte Antwort
Walter Roberson
am 23 Jan. 2014
You would need to use Symbolic Toolbox, or one of John D'Errico's variable-precision packages in the File Exchange.
0 Kommentare
Weitere Antworten (1)
Azzi Abdelmalek
am 23 Jan. 2014
sol=roots([1 3 1])
sol1=sprintf('%.20f,',sol)
6 Kommentare
Walter Roberson
am 25 Jan. 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.
Siehe auch
Kategorien
Mehr zu Conversion Between Symbolic and Numeric finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!