Sarah Tate
Sarah Tate

Reputation: 13

Approximating pi using Recursion in Matlab

I am supposed to approximate pi by using the continued fraction below in the link using recursion in Matlab. I am new to recursion and am unsure how to keep getting the fraction to repeat itself.

http://www.geom.uiuc.edu/~huberty/math5337/groupe/expresspi.html

This is what I have tried so far but am getting no where near the correct answer.

function approximation = recyclePi(limit)

approximation = rpHelper(limit, -1);

approximation = round(approximation, 10);

end

function out = rpHelper(limit, base)

if limit < 1

    out = base - 1;

else 


    base = base + 2;

    sq = base.^2;

    out = 2 + sq / rphelper(limit - 1, base)

%     out = rpHelper(limit - 1, base);
end


end

Please help!

Upvotes: 0

Views: 277

Answers (1)

Las
Las

Reputation: 68

Your code looks ok. Assuming that "the correct answer" is the output of pi you have to do the following:

p=recyclePi(100)
pi=(p-1).^(-1)*4

-1 because the fraction starts with 1+... ^-1,*4 because the continued fraction gives you 4/pi and not pi

If this isnt your problem, please clarify your question.

Upvotes: 1

Related Questions