Write a function [sn, n] = mySumPi(tol), which outputs 𝑆n and n for the smallest n such that |𝑆n − pi| < tol

5 Comments

No. It is not correct as code to compute an approximation for pi.
Hint: Does it form a sum in any way? No.
Did you need to sum the terms of the approximation? Yes.
In fact, you seem to be forming a PRODUCT of terms. Multiplication and addition are not the same thing. The Sigma in your formula indicates that you need a sum.
Lots of ways to do the problem. Look at cumsum(). Look at what the following does:
n=1:100;
sn=8*cumsum(1./(4*n-3)./(4*n-1));
i have to write a code which outpous n and sn..
@David where do i have to write this?
what i have to change?
Your equation does not match the one you are trying to code. You will need to initialize sn and n before starting your while loopo.

Sign in to comment.

Answers (1)

Ok, you are at least now getting close. Your code is still convoluted. And you have the wrong terms you create.
Look carefully at the term you need to create. It should apparently look like
1/((4*k-3)*(4*k-1))
You can multiply by 8, to cater to the 8 out front in the sum. But what you wrote is not that. Close. But not that. It still had sn in it, from the previous term.
As well, why are you computing count, but then returning n, which is just 1 less than count? Strange. Oh, I guess you are using n as the summation index, but it is not the number of terms you needed in the sum? But it is the number of terms.
Finally, you never actually initialized the approximation in any way at zero. Always initialize a sum variable that will accumulate a result. Just don't use a variable named sum.
And, oh. Learn to use semi-colons on your lines.
function [piapprox,k] = mySumPi(tol)
%[piapprox,k] = mySumPi(tol)
%lim as n approaches infinity is pi
% k is the index of the first term such that abs(piapprox-pi) <tol
k = 0;
piapprox = 0;
while abs(piapprox-pi) >= tol
k = k + 1;
piterm = 8./((4*k-3)*(4*k-1));
piapprox = piapprox + piterm;
end
end
With a tolerance of 1e-2, I get
[piapprox,count] = mySumPi(1e-2)
piapprox
piapprox =
3.13159290355855
k
k =
50
The series you are using is not very rapidly convergent. Better than a sharp stick in the eye, but not incredibly much better.

4 Comments

thank you very much but it says that n and sn must be the outputs.
I would never write it with a for loop. At best, I would only ever use a while loop, as I showed. Honestly, since this is a homework problem, I would not write it at all, since this is a relatively poorly convergent series to approximate pi, and I long since forgot any desire to do homework.
And trying to solve the problem without a loop, well, you COULD use cumsum. But that would force you to generate many more terms than you might need, summing them, only to find that you generated far too many terms, or that possibly, you did not generate a sufficient number of terms. Either case would leave you unhappy, forcing you to essentially put a loop around your solution. BAD PROGRAMMING! By trying to avoid a loop, you then need to make your solution more complex, involving a loop anyway.
So use of a non-looping solution is actually a terribly poor way to solve the problem.
Don't look for "elegant", non-looping solutions if there is no need to do so. You gain nothing out of it, your code gains nothing. The simple while loop I wrote is trivial to read, trivial to write, and efficient to execute.

Sign in to comment.

Categories

Find more on Elementary Math in Help Center and File Exchange

Asked:

on 22 Feb 2020

Commented:

on 15 Mar 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!