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

  • Thread starter Thread starter mayeeta
  • Start date Start date
  • Tags Tags
    Fraction Pi
Click For Summary

Homework Help Overview

The discussion revolves around computing a continued fraction for pi using iteration in Matlab. Participants are exploring the formulation of the continued fraction and how to implement it programmatically.

Discussion Character

  • Exploratory, Mathematical reasoning, Problem interpretation

Approaches and Questions Raised

  • One participant presents an initial attempt at an algorithm but questions the correctness of the first term in the continued fraction. Another participant suggests reversing the iteration order and checks the definition of the "nth term." A third participant shares a modified approach and seeks feedback on its accuracy. Additionally, there is a discussion about potential connections between the continued fraction and the series for \(\sum_{n=1}^{\infty} \frac{1}{n^2}\).

Discussion Status

The discussion is active, with participants providing various attempts and suggestions for improvement. There is no explicit consensus, but multiple interpretations and approaches are being explored, indicating a productive exchange of ideas.

Contextual Notes

Participants express uncertainty about the correct formulation of terms in the continued fraction and the implications of their iterative approaches. There is also mention of the need to clarify the definition of the "nth term" and how it relates to the overall computation.

mayeeta
Messages
5
Reaction score
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
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:
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.
 
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.
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
10
Views
63K
Replies
13
Views
3K