The number n is a mathematical constant that represents the
The number n is a mathematical constant that represents the ratio of a circle\'s circumference to its diameter. The approximate value is 3.14159. For large values of n, pi can be estimated using the formula: R_n = 1 - 1/3 + 1/5 - 1/7 - ... = sigma_n =1^infinity (-1)^n +1/2n - 1 pi/4 pi_n = 4R_n Write a function called pi_estimate that takes one input, n, the upper limit on the summation and returns the estimate of pi, calculated to n terms. Using this function, write a script, pi_main, that continues to call pi_estimate for values of n starting at 1 until the value returned is within .00015 of MATLAB\'s value for pi. Issue a well formatted message indicating the number of terms and the estimate of pi. 2n A \" 1 4 n
Solution
The function definition for the pi estimate is as under:-
prompt = \"Enter the upper limit value\"; % Asking for the user input.
n = input(\'prompt\');
function c = pi_estimate(n) % Function definition
for i = 1:n % Looping over till the upper limit
a = power(-1,i+1); % Calculating the numerator expression separately
b = 2*i+1;
c = c+(a/b); % Sum of the entire series
end
