Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a function to calculate an estimate of pi for a specified number of sign

ID: 3890265 • Letter: D

Question

Develop a function to calculate an estimate of pi for a specified number of significant digits . Your function should output both the estimate, and the number of summation terms used to calculate the estimate.

OLD CODE:

function [pi_estimate, error_absolute] = CL3_Maclaurin(N)

%% Input

% N: maximum value of n (refer to computer lab slides)

%

%% Output

% pi_estimate result of Maclaurin series expansion

% error_absolute error made by the estimate (absolute value)

%% Write your code here.

sum = 0; % Starts at 0

for i = 0:N % Start at 0 and go until N

sum = sum + ((-1)^(i))*(1/(2*i+1));

end

pi_estimate = 4*sum;

error_absolute = abs(pi_estimate - pi);

end

Explanation / Answer

function [estimate, N_terms] = PiEstimate( numsig)
% Input
% numsig: Required number of significant digits (positive integer, scalar)
% Output
% estimate result of sum series expansion
% N_terms number of sum terms used in the calculation.
n=0;
x=0;
cnt = 0;
k=4*((-1)^n/(2*n+1));
while abs(k)>1e-6
n= n+1;
x=x+k;
cnt++;
k=4*((-1)^n/(2*n+1));
end
x
estimate = k;
N_terms = cnt;