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
SUMMARY

This discussion focuses on computing a continued fraction for pi using iteration in Matlab. The initial approach provided by a user was incorrect, as it miscalculated the first term. A corrected algorithm was proposed, which runs the loop backwards from n to 1, utilizing the formula p = ((2*n+1)^2)/6. The final output is derived from the iterative process, yielding a more accurate approximation of pi.

PREREQUISITES
  • Understanding of continued fractions and their mathematical properties.
  • Familiarity with Matlab programming and syntax.
  • Knowledge of iterative algorithms and their implementation.
  • Basic concepts of mathematical series and limits.
NEXT STEPS
  • Learn about Matlab loops and control structures for better algorithm design.
  • Explore the mathematical derivation of continued fractions for other constants.
  • Investigate the relationship between continued fractions and series convergence.
  • Study optimization techniques for iterative algorithms in Matlab.
USEFUL FOR

Students learning programming in Matlab, mathematicians interested in continued fractions, and anyone looking to improve their algorithmic problem-solving skills.

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.
 

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
1K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
10
Views
63K
Replies
13
Views
3K