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

3 Ansichten (letzte 30 Tage)
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

Antworten (3)

KSSV
KSSV am 25 Jan. 2017
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 Kommentare
Guillaume
Guillaume am 25 Jan. 2017
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 am 25 Jan. 2017
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

Melden Sie sich an, um zu kommentieren.


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

Adam
Adam am 25 Jan. 2017
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.

Kategorien

Mehr zu Numeric Types 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!

Translated by