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
AI Thread Summary
The discussion focuses on creating an iterative algorithm in Matlab to compute a continued fraction for pi, starting with the first term as 3 + 1/9. A user initially proposed a solution but received feedback that the first term calculation was incorrect, suggesting a backward iteration approach for better accuracy. Another participant provided a refined method, emphasizing the need to generalize the terms and correctly set up the loop for calculations. The final proposed algorithm involves calculating terms in reverse, updating values iteratively, and ultimately outputting the result as 3 + A. The conversation highlights the importance of understanding the structure of continued fractions and iterative programming in Matlab.
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 \sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6} 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:

A=\frac{(2n-1)^2}{6}

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

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

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.
 
I tried to combine those 2 formulas but it didn't work. I tried using another case where there are 2 red balls and 2 blue balls only so when combining the formula I got ##\frac{(4-1)!}{2!2!}=\frac{3}{2}## which does not make sense. Is there any formula to calculate cyclic permutation of identical objects or I have to do it by listing all the possibilities? Thanks
Essentially I just have this problem that I'm stuck on, on a sheet about complex numbers: Show that, for ##|r|<1,## $$1+r\cos(x)+r^2\cos(2x)+r^3\cos(3x)...=\frac{1-r\cos(x)}{1-2r\cos(x)+r^2}$$ My first thought was to express it as a geometric series, where the real part of the sum of the series would be the series you see above: $$1+re^{ix}+r^2e^{2ix}+r^3e^{3ix}...$$ The sum of this series is just: $$\frac{(re^{ix})^n-1}{re^{ix} - 1}$$ I'm having some trouble trying to figure out what to...
Back
Top