what if I need more decimal digits in calculated results ? ( e.g. 50 or 80 decimal digits )

2 次查看(过去 30 天)
What should I do to increase the decimal digits ? FOR EXAMPLE :
clear all
clc
x=(-0.1);
y=(0);
fprintf(' i x \n')
fprintf(' --- ------------- \n')
for i=1:5;
fx=(x(i))^3-(x(i))^2+2;
fxx=3*(x(i))^2-2*(x(i));
y(i)=x(i)-(fx/fxx);
fy=(y(i))^3-(y(i))^2+2;
fyy=3*(y(i))^2-2*(y(i));
x(i+1)=x(i)+([fy-fx]/fyy);
end
for i=1:length(x)
fprintf('%2i %18.15f\n',i,x(i))
end
THE RESULTS ARE
i x
--- -------------
1 -0.100000000000000
2 -3.119140528362807
3 -1.474069695697443
4 -1.017351956185236
5 -1.000001047082565
6 -1.000000000000000

回答(3 个)

KSSV
KSSV 2017-1-25
fprintf('%2i %18.15f\n',i,x(i))
In the fprintf format, increase the number of decimal points. At present it prints 15 points, you change 15 to your desired number. But why you want 50 or 80 decimal points?
  3 个评论
Guillaume
Guillaume 2017-1-25
This is a very misleading answer! You can't just keep increasing the number of digits in fprintf and hope it's going to give you more valid digits. Certainly putting 50 or 80 is utterly pointless. The maximum number of precision digits of IEEE double is around 17 digits.
To get more precision you need a different storage format than double. Hence vpa.
khalid boureeny
khalid boureeny 2017-1-25
Hi,Guillaume ... thanks for your help ... I'm beginner in matlab ... My problem is that I don't know where to put the ( vpa ) in my program ... OR I don't know how to modify my program code to do so ... where should I put vpa function ???
x=(-pi); y=(0); fprintf(' i x \n') fprintf(' --- ------------- \n') for i=1:5; fx=(x(i))^3-(x(i))^2+2; fxx=3*(x(i))^2-2*(x(i)); y(i)=x(i)-(fx/fxx); fy=(y(i))^3-(y(i))^2+2; fyy=3*(y(i))^2-2*(y(i)); x(i+1)=x(i)+([fy-fx]/fyy); % this is my method
end
for i=1:length(x) fprintf('%2i %18.30f\n',i,x(i)) end
thanks again , I really need an answer for my question .. khalid_boureeny@yahoo.com

请先登录,再进行评论。


Jan
Jan 2017-1-25
编辑:Jan 2017-1-25
See https://www.mathworks.com/help/symbolic/vpa.html : "Variable-precision arithmetic", exactly what you need.

Adam
Adam 2017-1-25
You probably have to use the Symbolic Math Toolbox for more accuracy though I don't have this so am not familiar with its usage. Numbers get truncated when represented as doubles as there are only a certain amount of bits used and these represent generally no more than about 17 decimal places. It doesn't matter how many you ask for in a fprintf statement as they won't be there.
Using the Symbolic Math Toolbox my understanding is that equations remain in symbolic form rather than their results getting truncated in doubles so that you can maintain the precision. How you then print the results from that though I'm not sure as I've never done it. I assume it isn't difficult though.
But if you don't have Symbolic Toolbox you are stuck with ~15-17 decimal places. Usually that is enough for most work, though not all.

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by