Write Matlab program that calculates an estimate of the value of PI. In mathemat
ID: 3537298 • Letter: W
Question
Write Matlab program that calculates an estimate of the value of PI.
In mathematics, PI is approximated by the following formula:
PI = 4 %u2013 4/3 + 4/5 %u2013 4/7 + 4/9 - 4/11 + 4/13 - 4/15 + 4/17 . . .
The more terms you use, the closer to PI you get (The above equation shows 9 terms). For example, if you use 5 terms to calculate PI, your formula would be as follows:
PI = 4 - 4/3 + 4/5 - 4/7 + 4/9 = 4 %u2013 1.333333333333 + 0.8 - 0.571428 + 0.4444444
= 3.33968254.
What is your job?
Write a program that prompts the user to enter the number of terms which will be used to calculate PI. Also, your program should keep asking the user to re-calculate PI as long as you wish. Your program should get as close as it can to PI= 3.141592653589793.
Here is an example of input/output: %uF0E0 Enter number of terms for PI calculation: 5
Your results are: Actual PI = 3.141592653589793 Calculated PI = 3.33968254
Would you like to try another PI calculation? (yes/no) yes Enter number of terms for PI calculation: 500000
Your results are: Actual PI = 3.141592653589793 Calculated PI = 3.141590654
Would you like to try another PI calculation? (yes/no) yes Enter number of terms for PI calculation: 50000000
Your results are: Actual PI = 3.141592653589793 Calculated PI = 3.141592634
Would you like to try another PI calculation? (yes/no) no
Thank you, I am satisfied with this result.
Note: Your results might be just a little different from the above as far as the precision.
Explanation / Answer
resume='yes';
while( strcmpi(resume,'yes') )
t=input('Enter number of terms for PI calculation :');
res=0;
for i=1:t
res=res + ((-1)^(i+1))*(4/(2*i-1));
end
fprintf('Yout Results are: ');
fprintf('Actual PI: %.15f ',pi);
fprintf('Calculated PI: %.15f ',res);
resume = input('Would you like to try another PI calculation? (yes/no)','s');
end
disp('Thank you, I am satisfied with this result.');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.