How Can I Store and Print Specific Decimal Digits of Pi Using MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter phil.st
  • Start date Start date
  • Tags Tags
    Pi
Click For Summary
SUMMARY

The discussion focuses on storing and printing specific decimal digits of Pi using MATLAB, specifically from the 9901st to the 10000th digit. The user employs the Brent-Salamin algorithm within a custom function called agm_pi(d) to compute Pi to a specified number of decimal places. However, they encounter limitations due to MATLAB's default double-precision floating-point representation, which restricts precision to about 16 significant figures. The conversation highlights the need for alternative approaches to handle high-precision calculations beyond this limit.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of the Brent-Salamin algorithm for Pi calculation
  • Knowledge of variable-precision arithmetic (VPA) in MATLAB
  • Awareness of floating-point representation and its limitations
NEXT STEPS
  • Research the vpa function in MATLAB for variable-precision arithmetic
  • Explore alternative algorithms for high-precision Pi calculation, such as the Gauss-Legendre algorithm
  • Learn about MATLAB's array handling capabilities for large vectors
  • Investigate libraries or toolboxes that extend MATLAB's numerical precision
USEFUL FOR

This discussion is beneficial for MATLAB programmers, mathematicians, and anyone interested in high-precision numerical computations, particularly those working with mathematical constants like Pi.

phil.st
Messages
6
Reaction score
0
Hi! I'm using MATLAB and I want to store or print some particular decimal digits of pi, from 9901 to 10000. I'm using the algorithm below (Brent-Salamin algorithm) to print the first 10000 digits but I can't find out a way to save and print only the decimal digits from 9901 to 10000. What function can I use? Any ideas?

Code:
 function P = agm_pi(d)
% AGM_PI  Arithmetic-geometric mean for pi.
% Brent-Salamin algorithm.
% agm_pi(d) produces d decimal digits.
% See Cleve's Corner, "Computing Pi",
% http://www.mathworks.com/company/ ...
%    newsletters/news_notes/2011/ 

% Copyright 2011 MathWorks, Inc.
digits(d)
a = vpa(1,d);
b = 1/sqrt(vpa(2,d));
s = 1/vpa(4,d);
p = 1;
n = ceil(log2(d));
for k = 1:n
   c = (a+b)/2;
   b = sqrt(a*b);
   s = s - p*(c-a)^2;
   p = 2*p;
   a = c;
end
P = a^2/s;

http://en.wikipedia.org/wiki/Gauss–Legendre_algorithm
 
Last edited:
Physics news on Phys.org
Well, MATLAB by default represents numbers as double-precision floating points:
http://www.mathworks.com/help/techdoc/matlab_prog/f2-12135.html

That means that you only have 53 bits for your fraction (in decimal terms, about 16 significant figures):
http://en.wikipedia.org/wiki/Double_precision

You can see where the problem arises when you try to calculate beyond 16 decimal places using the implementation provided (and no, you can't try to do something sneaky like multiplying the answer by 10^16!)

I had a similar problem as a homework assignment in undergrad once (and I suspect that this is something similar)--the above insight provided the critical hint that I needed to come up with a solution, and may do the same for you.

HINT: how big can you make a vector (1 x n, or n x1 array) in MATLAB?
 
Last edited by a moderator:

Similar threads

  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
5
Views
8K
Replies
1
Views
9K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 9 ·
Replies
9
Views
8K