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
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.
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; F=0; while n<100 add=4*(((-1)^n)/(2*n+1)); F = F + add; error_approximatePercent = (abs(add)/F)*100; error_approximateRelative = abs(add)/F; %when using the precent error numsigP = 2-log10(2*error_approximatePercent); %when using the relative error numsigR = -log10(2*error_approximateRelative); n= n+1; %relative and precent error give the same result. Be careful on the %formula disp([n,numsigP,numsigR]) end N=n;
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
