How do I program to calculate the estimate of pi using Leibniz but enabling me to select number n?

6 views (last 30 days)
I need to program the estimate for pi using Leibniz summation but I want to choose number of n myself, rather than make an infinite loop. What is pi when n=4, n=13 etc. Can someone please explain how I can do that? I'm struggling

Answers (1)

Star Strider
Star Strider on 19 Jan 2016
You don’t need a loop. You can take advantage of MATLAB’s vectorised operations:
N = 10; % Number Of Terms
pi4 = 1./(1:2:N) .* -(-1).^(1:fix(N/2)); % Series
cs_pi = 4*cumsum(pi4); % Term-Wise Cumulative Sum
s_pi = 4*sum(pi4); % Sum
By setting ‘N’ to as many terms as you want, you can see the series, term-wise cumulative sum, and sum. I multiplied it by 4 to see the approximations to pi. Experiment with it!

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!