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

  • Thread starter phil.st
  • Start date
  • Tags
    Pi
In summary, the conversation discusses using MATLAB to store or print decimal digits of pi. The Brent-Salamin algorithm is used to print the first 10000 digits, but the challenge is to save and print only the decimal digits from 9901 to 10000. The individual suggests using the AGM_PI function and provides some helpful resources on number representation in MATLAB. They also give a hint about using vectors to solve the problem.
  • #1
phil.st
6
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
  • #2
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:

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

1. What is pi?

Pi is a mathematical constant that represents the ratio of a circle's circumference to its diameter. It is approximately 3.14159, but it is an infinite number with no repeating pattern.

2. How many digits of pi are known?

As of 2021, over 31 trillion digits of pi have been calculated. However, for most practical uses, only a few digits after the decimal point are necessary.

3. How can I print particular digits of pi?

To print a specific digit of pi, you can use a computer program or calculator that has pi stored as a constant. Alternatively, you can use a formula or algorithm to calculate the digit manually.

4. Can I print pi to an infinite number of digits?

No, it is physically impossible to print an infinite number of digits. However, with advanced computing power, we can calculate and print an extremely large number of digits of pi.

5. Are there any patterns in the digits of pi?

So far, no repeating pattern has been found in the digits of pi. It is believed to be a completely random and non-repeating number, making it a fascinating topic of study in mathematics and computer science.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
528
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
6K
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
  • Programming and Computer Science
Replies
5
Views
3K
Back
Top