How can I use iteration to compute a continued fraction for pi in Matlab?

In summary, pi is a continued fraction. The 1st term is 3+1/9. The nth term is the sum of the previous terms. The algorithm to compute this to n terms uses Matlab.
  • #1
mayeeta
5
0

Homework Statement



Via iteration. A continued fraction for pi is:

MainEq1.gif


Write an algorithm to compute this to n terms using Matlab.

1st term would be 3+1/9



The Attempt at a Solution



p = '1';
for k = 1:n
p=3+(-1+2^n)/(3+p);
end
p

I'm very new to programming, and I can only think of this.
 
Physics news on Phys.org
  • #2
mayeeta said:

Homework Statement



Via iteration. A continued fraction for pi is:

MainEq1.gif


Write an algorithm to compute this to n terms using Matlab.

1st term would be 3+1/9



The Attempt at a Solution



p = '1';
for k = 1:n
p=3+(-1+2^n)/(3+p);
end
p

I'm very new to programming, and I can only think of this.
Have you checked it at all?

For the very first term, when n= 1, this is 3+ (-1+2)/(3+ 1)= 3+ 1/4, not 3+ 1/9.

If it were me, I think I would run the loop backwards, starting from n and going down to 1 because each fraction is used in the previous one.

Also you might want to check exactly what the "nth term" is. Do you just ignore the "new fraction" and just use denominator "6" for the last term?
 
Last edited by a moderator:
  • #3
I tried to do it backward, and I got this

p = ((2*n+1)^2)/6;
for k = n:-1:1
p = 6+(2*n-1)^2/p;
end
f=p+3

How to fix it? I think it's closer to the right answer.
 
  • #4
This all just may be very coincidental, but from this continued fraction is it possible to show [tex]\sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}[/tex] from this? It just seems that it should have a nice connection, with the squares and the 6's in there and all.
 
  • #5
mayeeta said:

Homework Statement



Via iteration. A continued fraction for pi is:

MainEq1.gif


Write an algorithm to compute this to n terms using Matlab.

1st term would be 3+1/9



The Attempt at a Solution



p = '1';
for k = 1:n
p=3+(-1+2^n)/(3+p);
end
p

I'm very new to programming, and I can only think of this.

mayeeta said:
I tried to do it backward, and I got this

p = ((2*n+1)^2)/6;
for k = n:-1:1
p = 6+(2*n-1)^2/p;
end
f=p+3

How to fix it? I think it's closer to the right answer.

The limits seem a bit awkward. Mmmm, let's call the terms 1 2 3 4 5 6 which involve the squares of 1 3 5 7 9 11 respectively. This means that you can generalize it as n and 2n-1 to make things a bit more clear.

Now the last term is:

[tex]A=\frac{(2n-1)^2}{6}[/tex]

Then the runner i goes from n-1 to 1 and determines the general intermediate result:

[tex]B=\frac{(2i-1)^2}{6+A}[/tex]

Off course you need to set this value back to A for the next i-loop. You get finally in pseudo-code:

integer n,i
real A,B
input n
set A=(2n-1)^2/6
for i=n-1 downto 1 do
begin loop
calc B=(2i-1)^2/(6+A)
set A=B
end loop
output 3+A

Hope this makes it a little bit more clear.
 

Related to How can I use iteration to compute a continued fraction for pi in Matlab?

1. What is a continued fraction for pi?

A continued fraction for pi is a mathematical expression that represents the irrational number pi (π) as a sequence of ratios between integers.

2. How is the continued fraction for pi calculated?

The continued fraction for pi is calculated by repeatedly taking the integer part of pi and then subtracting it from the original value. This process is continued until the resulting decimal becomes 0, or a predetermined precision is reached.

3. What is the significance of the continued fraction for pi?

The continued fraction for pi has been studied for centuries and has been found to have several interesting properties. It is a representation of pi that converges more quickly than other expressions, making it useful for approximations. It also has connections to other mathematical concepts, such as the Golden Ratio and the Farey sequence.

4. How accurate is the continued fraction for pi?

The accuracy of the continued fraction for pi depends on the number of iterations used in the calculation. The more iterations, the closer the resulting value will be to the true value of pi. However, due to the infinite nature of pi, the continued fraction will always be an approximation.

5. Is the continued fraction for pi used in real-world applications?

The continued fraction for pi is not commonly used in practical applications, as it is primarily a mathematical curiosity. However, it has been used in some fields of research, such as number theory and cryptography, for its unique properties and connections to other mathematical concepts.

Similar threads

  • Precalculus Mathematics Homework Help
Replies
3
Views
1K
  • Precalculus Mathematics Homework Help
Replies
5
Views
989
  • Precalculus Mathematics Homework Help
Replies
14
Views
2K
  • Precalculus Mathematics Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
692
  • Precalculus Mathematics Homework Help
Replies
5
Views
1K
  • Precalculus Mathematics Homework Help
Replies
8
Views
1K
  • Precalculus Mathematics Homework Help
Replies
13
Views
2K
  • Precalculus Mathematics Homework Help
Replies
7
Views
1K
Replies
4
Views
1K
Back
Top