Write a function with the header function [piEstimate, piError] = myForPi (n!ter
ID: 3728017 • Letter: W
Question
Write a function with the header function [piEstimate, piError] = myForPi (n!terations) which uses a forloop and the Archimedes method below to estimate the value of pi (you must use this approach). I. Let A = 1 and N=6 2. Repeat the following niterations times a. Replace N by 2N b. Replace A by [2-sart[4-AA2} ] 1 /2) C. Let L NA/2 d. Let U = L/sqrt(1-A^2/2) e. Let pEstimate = (U + L)/2 f. Let piError=U-L)/2 3. Return Test Functions >> [piEst, piErr] = myForPi (2) 3.160015969288298 piErr 0.027387356007061 >> [piEst, piErr] = myForPi (4) piEst 3.142715953186386 piErr = 0.001684002295856 >> [piEst, piErr] = myForPi (10) 3.141592927281175Explanation / Answer
Solution:
code:
function [ piEstimate, piError ] = myForPi( nIterations )
A=1; %This is the first step
N=6;
%The Archimedes principle is applied here
for i=1:nIterations
N = 2*N;
A = sqrt(2 - sqrt(4 - A*A));
L = (N*A)/2;
U = L/(sqrt(1 - (A*A)/2));
piEstimate = (U+L)/2;
piError = (U-L)/2;
end
Output:
>> [piEst, piError] = myForPi(2)
piEst =
3.1600
piError =
0.0274
>> [piEst, piError] = myForPi(4)
piEst =
3.1427
piError =
0.0017
>> [piEst, piError] = myForPi(10)
piEst =
3.1416
piError =
4.1069e-07
>>
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.