How to set resolution for the numerical calculations in MATLAB
조회 수: 19 (최근 30일)
이전 댓글 표시
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
Roger Stafford
2014년 1월 23일
편집: Azzi Abdelmalek
2014년 1월 24일
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:
채택된 답변
Walter Roberson
2014년 1월 23일
You would need to use Symbolic Toolbox, or one of John D'Errico's variable-precision packages in the File Exchange.
댓글 수: 0
추가 답변 (1개)
Azzi Abdelmalek
2014년 1월 23일
sol=roots([1 3 1])
sol1=sprintf('%.20f,',sol)
댓글 수: 6
Walter Roberson
2014년 1월 25일
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!