Problem 1: (5 Points) Write a function with the header: function [piEstimate, pi
ID: 3728157 • Letter: P
Question
Problem 1: (5 Points) Write a function with the header: function [piEstimate, piError] myFor Pínlte rations) = which uses o for-loop and the Archimedes method below to estimate the value of pi lyou I. Let A= 1 and N = 6 2. Repeat the following niterations times a. Replace N by 2N b. Replace A by 12-sqrt(4- AA2) 14(1/2) C. Let L=NA/2 d. Let U = L/sqrt(1-AA2/2) e. Let piEstimate = (U + L)/2 f. Let piError= (U-L)/2 3. Return >> [piEst, piErr myori (2) 3.160015969288298 piErr 0.027387356007061Explanation / Answer
Hello there,
PFB matlab function for the required functionality along with the output of test runs.
function [piEstimate, nIterations] = myWhilePi(tol)
A = 1;
N = 6;
nIterations = 1; % initializing no of iterations and error
piError = tol+1;
while(piError > tol)
N = 2*N;
A = (2-sqrt(4 - A^2))^(1/2);
L = N*A/2;
U = L/sqrt(1-A^2/2);
piEstimate = (U+L)/2;
piError = (U-L)/2;
nIterations = nIterations + 1;
end
end
-------------------------------------------------
TestRuns:
---------------------------------------------------
> [piEst, nIter] = myWhilePi(.1)
piEst = 3.1600
nIter = 3
> [piEst, nIter] = myWhilePi(.01)
piEst = 3.1461
nIter = 4
> [piEst, nIter] = myWhilePi(.0001)
piEst = 3.1416
nIter = 8
> [piEst, nIter] = myWhilePi(1e-16)
piEst = 0
nIter = 29