How many terms of series are required to get accuracy upto three decimal point. That is series sum correct upto 3.141
9 views (last 30 days)
Show older comments
ajeet sahu
on 18 Oct 2021
Commented: Sulaymon Eshkabilov
on 22 Oct 2021
Image of the problem
Here is what I written but I'm getting answer 1000 which is wrong.This is really challenging for me to get correct answer.
sum = 0;
i = 0;
true = pi;
%3 decimal place accuracy criteria
while abs(sum-true) >= 0.001
i = i+1;
sum = sum + 4*(-1)^(i-1)*1/(2*i-1);
end
fprintf('terms required are: %d',i)
5 Comments
Accepted Answer
John D'Errico
on 20 Oct 2021
Edited: John D'Errico
on 20 Oct 2021
@ajeet sahu - what you do not seem to understand is that the script you have shown IS performing the correct iterations, The problem is with the convergence test. But more importantly, you will need to decide what it means to YOU for the convergence test to terminate. Until YOU state a clear definition of what it means for you to get the convergence you want, then code cannot be written without a clear definition.
I''ve plotted below what the successive approximations do, by looking at a specific digit here:
I've plotted that second digit here. As you can see, it seems to be around iteration 625 or so that 4 becomes stable, but it was osillating between 3 and 4 from roughly iteration 120. So the very firsst time a 4 appears after 3.1, is roughly iteration 120.
Now, look at the next digit. When does it stabilize?
Here we see that a 1 in that place begins to appear around iteration 1700, but that third digit is now oscilating between 1 and 2 for a long time. It is close to iteration 2500 before we get a stable digit 1 at that point. The problem is, if we look at pi itself, 3.142 is actually CLOSER to the true value of pi than is 3.141. So can we say that 3.141 has converged? This is the problem with your tolerance test. Your convergence test is looking to see if the absolute error in the approximation to pi is within a specific tolerance, and stopping as soon as that happens.
I could also plot the next digit, as found by that approximation. etc. But you need to decide what convergence means to you. And probably, you need to decide what it means to your teacher, because that is who will be grading your solution.
So next, consider the absolute error, thus your approximation minus pi. When does that error, which is oscillating in sign at EVERY iteration, become ssufficiently small in absolute value at some point?
As you can see from this last figure, I had to use a log scale on the y axis, because the numbers become small. But if you are looking only at the difference from ground truth, at what point would you determine the series has finally converged?
We cannot give you a convergence test until you know yourself, and you explain in clear words, what it means for that approximation to have converged. Typically, is is simply absolute convergence that is used, so one might use the absolute difference between truth and the approximation, and when that difference is first less than some tolerance value. But that does NOT gaurantee that a specific digit is correct. As you see from the plots I have shown, any given digit will oscillate for quite a while before it settles into a fixed value.
So your problem is within your own mind, and only there. Until you explain what it means for convergence, and until you explain what you think your teacher means for that to happen, nobody can help you.
3 Comments
More Answers (1)
Sulaymon Eshkabilov
on 18 Oct 2021
Indeed, it is working ok. Here is a test with 0.0001 decimal accuracy. There is one minor point that needs to be fixed withing fprintf():
sum = 0;
i = 0;
true = pi;
% 4 decimal place accuracy criteria
while abs(sum-true) >= 0.0001
i = i+1;
sum = sum + 4*(-1)^(i-1)*1/(2*i-1);
end
fprintf('terms required are: %d \n',i)
fprintf('The found SUM = %f and PI = %f \n', sum, pi)
fprintf('The difference is %f \n', abs(sum-true))
2 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!