
Concept explainers
To write:
A menu-driven program to investigate the constant

Answer to Problem 28E
Solution:
The script file is,
% MATLAB code to calcualte the value of pi.
%script file.
select = pivalues;
while select ˜= 4
switch select
%select the different options to get the value of pi.
case 1
machinformula
%print the result by usin Machin's formula.
case 2
leibn
%get approximate by using Leibniz.
case 3
leibgood
%approximate pi value using Leibniz untill good approximation.
end
select = pivalues;
end
% end of
%The script file should be placed in the same folder.
The function file is,
% MATLAB code to calcualte the value of pi by different options.
%function file.
function select = pivalues
select = menu('select a option for pi', 'Machin''s formula', 'Leibniz''s formula:n-terms','Leibniz''s formula:good approximation','Exit');
%select the different options in menu.
while select == 0
disp('not valid! please select one of the options')
select = menu('select a option for pi', 'Machin''s formula', 'Leibniz''s formula:n-terms','Leibniz''s formula:good approximation','Exit');
end
end
% end of function
%The function file should be placed in the same folder.
The function file is,
% MATLAB code to calcualte the value of pi by Machinformula.
%function file.
function machinformula
formula = (4*atan(1/5)-atan(1/239));
%define the variable pivalues.
fprintf('Using the MATLAB constant, pi = %.6f\n', pi)
fprintf('the value of pi using Machin''s formula, pi = %.6f\n', 4*formula)
%print the value of pi.
end
% end of function
%The function file should be placed in the same folder.
The function file is,
% MATLAB code to calcualte the value of pi by leibniz's formula for the specific terms.
%function file.
function leibn
fprintf('Approximate value of pi by using Leibniz'' formula\n')
n = askforn;
approxvaluepi = 0;
denominator = -1;
signterm = -1;
for i = 1:n
denominator = denominator + 2;
signterm = -signterm;
approxvaluepi = approxvaluepi + signterm * (4/denominator);
end
fprintf('An approximation of pi with n = %d is %.2f\n', n, approxvaluepi)
end
function out = askforn
inputnum = input('A positive integer for n is entered: ');
num2 = int32(inputnum);
while num2 ˜= inputnum || num2 < 0
inputnum = input('Not Valid! Enter a positive integer:');
num2 = int32(inputnum);
end
out = inputnum;
end
% end of function
%The function file should be placed in the same folder.
The function file is,
% MATLAB code to calcualte the value of pi by leibniz's formula till the good apprroxiamtion is found..
%script file
function leibgood
error = 0.01;
N = 1;
S = 2;
runsum = 0;
difference = 1;
while error < difference
term = (-1)^S*4/N;
temp = runsum;
runsum = runsum + term;
difference = abs(temp-runsum);
N = N+2;
S = S+1;
end
fprintf('An approximation of pi using Leibniz ''series within %.2f is %.2f\n', error, runsum)
%print the value of pi.
end
% end of function
%The script file should be placed in the same folder.
Explanation of Solution
Machin’s formula is given as,
Leibniz’s formula is given as,
The approximation till the fourth term is given as,
MATLAB Code:
% MATLAB code to calcualte the value of pi.
%script file.
select = pivalues;
while select ˜= 4
switch select
%select the different options to get the value of pi.
case 1
machinformula
%print the result by usin Machin's formula.
case 2
leibn
%get approximate by using Leibniz.
case 3
leibgood
%approximate pi value using Leibniz untill good approximation.
end
select = pivalues;
end
% end of function
%The script file should be placed in the same folder.
% MATLAB code to calcualte the value of pi by different options.
%function file.
function select = pivalues
select = menu('select a option for pi', 'Machin''s formula', 'Leibniz''s formula:n-terms','Leibniz''s formula:good approximation','Exit');
%select the different options in menu.
while select == 0
disp('not valid! please select one of the options')
select = menu('select a option for pi', 'Machin''s formula', 'Leibniz''s formula:n-terms','Leibniz''s formula:good approximation','Exit');
end
end
% end of function
%The function file should be placed in the same folder.
% MATLAB code to calcualte the value of pi by Machinformula.
%function file.
function machinformula
formula = (4*atan(1/5)-atan(1/239));
%define the variable pivalues.
fprintf('Using the MATLAB constant, pi = %.6f\n', pi)
fprintf('the value of pi using Machin''s formula, pi = %.6f\n', 4*formula)
%print the value of pi.
end
% end of function
%The function file should be placed in the same folder.
% MATLAB code to calcualte the value of pi by leibniz's formula for the specific terms.
%function file.
function leibn
fprintf('Approximate value of pi by using Leibniz'' formula\n')
n = askforn;
approxvaluepi = 0;
denominator = -1;
signterm = -1;
for i = 1:n
denominator = denominator + 2;
signterm = -signterm;
approxvaluepi = approxvaluepi + signterm * (4/denominator);
end
fprintf('An approximation of pi with n = %d is %.2f\n', n, approxvaluepi)
end
function out = askforn
inputnum = input('A positive integer for n is entered: ');
num2 = int32(inputnum);
while num2 ˜= inputnum || num2 < 0
inputnum = input('Not Valid! Enter a positive integer:');
num2 = int32(inputnum);
end
out = inputnum;
end
% end of function
%The function file should be placed in the same folder.
% MATLAB code to calcualte the value of pi by leibniz's formula till the good apprroxiamtion is found..
%script file
function leibgood
error = 0.01;
N = 1;
S = 2;
runsum = 0;
difference = 1;
while error < difference
term = (-1)^S*4/N;
temp = runsum;
runsum = runsum + term;
difference = abs(temp-runsum);
N = N+2;
S = S+1;
end
fprintf('An approximation of pi using Leibniz ''series within %.2f is %.2f\n', error, runsum)
%print the value of pi.
end
% end of function
%The script file should be placed in the same folder.
Save the MATLAB script with name, main.m, and the function files with names machinformula.m, leibn.m, pivalues.m and leibgood.m in the current folder. Execute the program by typing the script name at the command window to generate result.
Result:
The results is,
Therefore, the result is stated above.
Want to see more full solutions like this?
Chapter 6 Solutions
Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
- You want to obtain a sample to estimate the proportion of a population that possess a particular genetic marker. Based on previous evidence, you believe approximately p∗=11% of the population have the genetic marker. You would like to be 90% confident that your estimate is within 0.5% of the true population proportion. How large of a sample size is required?n = (Wrong: 10,603) Do not round mid-calculation. However, you may use a critical value accurate to three decimal places.arrow_forward2. [20] Let {X1,..., Xn} be a random sample from Ber(p), where p = (0, 1). Consider two estimators of the parameter p: 1 p=X_and_p= n+2 (x+1). For each of p and p, find the bias and MSE.arrow_forward1. [20] The joint PDF of RVs X and Y is given by xe-(z+y), r>0, y > 0, fx,y(x, y) = 0, otherwise. (a) Find P(0X≤1, 1arrow_forward4. [20] Let {X1,..., X} be a random sample from a continuous distribution with PDF f(x; 0) = { Axe 5 0, x > 0, otherwise. where > 0 is an unknown parameter. Let {x1,...,xn} be an observed sample. (a) Find the value of c in the PDF. (b) Find the likelihood function of 0. (c) Find the MLE, Ô, of 0. (d) Find the bias and MSE of 0.arrow_forward3. [20] Let {X1,..., Xn} be a random sample from a binomial distribution Bin(30, p), where p (0, 1) is unknown. Let {x1,...,xn} be an observed sample. (a) Find the likelihood function of p. (b) Find the MLE, p, of p. (c) Find the bias and MSE of p.arrow_forwardGiven the sample space: ΩΞ = {a,b,c,d,e,f} and events: {a,b,e,f} A = {a, b, c, d}, B = {c, d, e, f}, and C = {a, b, e, f} For parts a-c: determine the outcomes in each of the provided sets. Use proper set notation. a. (ACB) C (AN (BUC) C) U (AN (BUC)) AC UBC UCC b. C. d. If the outcomes in 2 are equally likely, calculate P(AN BNC).arrow_forwardSuppose a sample of O-rings was obtained and the wall thickness (in inches) of each was recorded. Use a normal probability plot to assess whether the sample data could have come from a population that is normally distributed. Click here to view the table of critical values for normal probability plots. Click here to view page 1 of the standard normal distribution table. Click here to view page 2 of the standard normal distribution table. 0.191 0.186 0.201 0.2005 0.203 0.210 0.234 0.248 0.260 0.273 0.281 0.290 0.305 0.310 0.308 0.311 Using the correlation coefficient of the normal probability plot, is it reasonable to conclude that the population is normally distributed? Select the correct choice below and fill in the answer boxes within your choice. (Round to three decimal places as needed.) ○ A. Yes. The correlation between the expected z-scores and the observed data, , exceeds the critical value, . Therefore, it is reasonable to conclude that the data come from a normal population. ○…arrow_forwardding question ypothesis at a=0.01 and at a = 37. Consider the following hypotheses: 20 Ho: μ=12 HA: μ12 Find the p-value for this hypothesis test based on the following sample information. a. x=11; s= 3.2; n = 36 b. x = 13; s=3.2; n = 36 C. c. d. x = 11; s= 2.8; n=36 x = 11; s= 2.8; n = 49arrow_forward13. A pharmaceutical company has developed a new drug for depression. There is a concern, however, that the drug also raises the blood pressure of its users. A researcher wants to conduct a test to validate this claim. Would the manager of the pharmaceutical company be more concerned about a Type I error or a Type II error? Explain.arrow_forwardFind the z score that corresponds to the given area 30% below z.arrow_forwardFind the following probability P(z<-.24)arrow_forward3. Explain why the following statements are not correct. a. "With my methodological approach, I can reduce the Type I error with the given sample information without changing the Type II error." b. "I have already decided how much of the Type I error I am going to allow. A bigger sample will not change either the Type I or Type II error." C. "I can reduce the Type II error by making it difficult to reject the null hypothesis." d. "By making it easy to reject the null hypothesis, I am reducing the Type I error."arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- Algebra & Trigonometry with Analytic GeometryAlgebraISBN:9781133382119Author:SwokowskiPublisher:CengageMathematics For Machine TechnologyAdvanced MathISBN:9781337798310Author:Peterson, John.Publisher:Cengage Learning,Elementary AlgebraAlgebraISBN:9780998625713Author:Lynn Marecek, MaryAnne Anthony-SmithPublisher:OpenStax - Rice University
- Algebra: Structure And Method, Book 1AlgebraISBN:9780395977224Author:Richard G. Brown, Mary P. Dolciani, Robert H. Sorgenfrey, William L. ColePublisher:McDougal LittellTrigonometry (MindTap Course List)TrigonometryISBN:9781305652224Author:Charles P. McKeague, Mark D. TurnerPublisher:Cengage LearningGlencoe Algebra 1, Student Edition, 9780079039897...AlgebraISBN:9780079039897Author:CarterPublisher:McGraw Hill




