Problem 953. Pi Estimate 1
Solution Stats
Problem Comments
-
17 Comments
please update the link to the document, cant find it anymore
The link is broken, but you can solve this problem with the Leibniz formula for π.
The principle is described here: https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
@Jordan Wilkerson change the number format and round to significant digits shown in test code
@Paul Derwin
Oh right, duh. I should've thought of that haha. That worked! I deleted my last comment since it basically was a solution after all. Thanks for your help!
Leibniz formula, precision 1e-6.
please update the link.
The description has been updated per the link provided in a prior comment.
function [estimate] = pi_est1(nMax)
estimate=0;
for i=1:(nMax-1)
m=2*i+1;
if mod(i,2)~=0
estimate=(1/m)+estimate;
else
estimate=-(1/m)+estimate;
end
end
estimate=(1-estimate)*4;
estimate=round(estimate,6);
end
I believe the task statement should be modified by specifying that the estimate should be rounded. Otherwise, the test suite can be edited accordingly.
The problem description has been updated to specify that rounding is required.
What an idiotic checking algorithm. I spent 10 minutes just trying to get the software to think I had solved the problem when I had already gotten it right multiple times earlier. Make sure you officially round, and not just print it with digits (which is smarter because the variable retains all of its significant digits), otherwise you'll find your time wasted like mine was.
And, all for 10 points. How stupid. Make a better checking algorithm or at least give us more points for having to put up with this BS
good
good
I'll have to say that the checking algorithm is very poorly written. Using isequal instead of an absolute difference with a permitted tolerance check is straight out flawed for me. And after repeating the calculations with both Python and MATLAB. I get an estimate of 3.2323.. for a series of 10 elements (nMax = 10). I wonder how the problem setters arrived at an estimate of 3.04... something. I can't remember the exact value that was used for that test.
So in running this summation through my ti-nspire for 10 iterations, I get an estimate of 3.232323... However, test 1 asserts that this should actually be 3.041840. The code I wrote also returns 3.232323, because it uses the Leibniz formula to estimate pi using the specified number of iterations "nMax". Either I am completely missing something or the author is unaware of what values a correct solution should actually be returning.
Like Sam, my code does not match the 'correct solutions.' As some previous comments suggested, I set my format to long and rounded to six digits.
nMax = 10, my estimate is 3.232316000000000
nMax = 1000, my estimate is 3.142592000000000
nMax = 1e6, my estimate is 3.141594000000000
Solution Comments
Show commentsProblem Recent Solvers1168
Suggested Problems
-
Find the numeric mean of the prime numbers in a matrix.
8980 Solvers
-
745 Solvers
-
Who has power to do everything in this world?
449 Solvers
-
1526 Solvers
-
778 Solvers
More from this Author4
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!