Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
  5 Kommentare
David Hill
David Hill am 22 Feb. 2020
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 22 Feb. 2020
Bearbeitet: John D'Errico am 22 Feb. 2020
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 Kommentare
John D'Errico
John D'Errico am 15 Mär. 2021
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by