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

Answer to Problem 29E
Solution:
The script file is,
clc
clear all
close all
% MATLAB code to calcualte the value of pi by different options.
%script file.
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 choose one of the choices')
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 script file should be placed in the same folder.
The script file is,
% MATLAB code to calcualte the value of pi by Machinformula.
%script file.
function machinformula
pivalues = 4*(4*atan(1/5)-atan(1/239));
%define the variable pivalues.
fprintf('the value of pi using Machin''s formula is%.4f\n', pivalues)
%print the value of pi.
end
% end of function
%The script file should be placed in the same folder.
The script file is,
% MATLAB code to get the value of integer.
%script file.
function out = valuen
out = input('A positive integer for n is entered:');
%the value of positive integer is entered.
while out˜= int32(out)||out<=0
out = input('not valid! A posiytive integer for n is entered:');
end
end
% end of function
%The script file should be placed in the same folder.
The script file is,
% MATLAB code to calcualte the value of pi by leibniz's formula for the specific terms.
%script file.
function leibn
out = valuen;
numerat = -4.*cumprod(-ones(1, out));
%define the variable numerat as numerator.
denomina = 1:2:2*out;
%define the variable denomina as denominator.
pievalues = sum((numerat)./(denomina));
fprintf('the approximation of Leibniz for pi with %d terms is %.4f\n', out, pievalues);
%print the value of pi.
end
% end of function
%The script file should be placed in the same folder.
The script 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.
The main script file is,
% MATLAB code to calcualte the value of pi.
%script file.
select = -1;
while select ˜= 4
select = pivalues;
switch select
%select the different options to get the value of pi.
case 1
machinformula
case 2
leibn
case 3
leibgood
end
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 by different options.
%script 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 choose one of the choices')
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 script file should be placed in the same folder.
% MATLAB code to calcualte the value of pi by Machinformula.
%script file.
function machinformula
pivalues = 4*(4*atan(1/5)-atan(1/239));
%define the variable pivalues.
fprintf('the value of pi using Machin''s formula is%.4f\n', pivalues)
%print the value of pi.
end
% end of function
%The script file should be placed in the same folder.
% MATLAB code to get the value of integer.
%script file.
function out = valuen
out = input('A positive integer for n is entered:');
%the value of positive integer is entered.
while out˜= int32(out)||out<=0
out = input('not valid! A posiytive integer for n is entered:');
end
end
% end of function
%The script file should be placed in the same folder.
% MATLAB code to calcualte the value of pi by leibniz's formula for the specific terms.
%script file.
function leibn
out = valuen;
numerat = -4.*cumprod(-ones(1, out));
%define the variable numerat as numerator.
denomina = 1:2:2*out;
%define the variable denomina as denominator.
pievalues = sum((numerat)./(denomina));
fprintf('the approximation of Leibniz for pi with %d terms is %.4f\n', out, pievalues);
%print the value of pi.
end
% end of function
%The script 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.
% MATLAB code to calcualte the value of pi.
%script file.
select = -1;
while select ˜= 4
select = pivalues;
switch select
%select the different options to get the value of pi.
case 1
machinformula
case 2
leibn
case 3
leibgood
end
end
% end of function
%The script file should be placed in the same folder.
Save the MATLAB scripts with names, pivalues.m, machinformula.m, valuen.m, leibn.m, leibgood.m and main.m in the current folder. Execute the script by typing the script name at the command window to generate result.
Result:
The results is,
Therefore, the result and script files are stated above.
Want to see more full solutions like this?
Chapter 6 Solutions
MATLAB: A Practical Introduction to Programming and Problem Solving
- Apply STATA commands & submit the output for each question only when indicated below i. Generate the log of birthweight and family income of children. Name these new variables Ibwght & Ifaminc. Include the output of this code. ii. Apply the command sum with the detail option to the variable faminc. Note: you should find the 25th percentile value, the 50th percentile and the 75th percentile value of faminc from the output - you will need it to answer the next question Include the output of this code. iii. iv. Use the output from part ii of this question to Generate a variable called "high_faminc" that takes a value 1 if faminc is less than or equal to the 25th percentile, it takes the value 2 if faminc is greater than 25th percentile but less than or equal to the 50th percentile, it takes the value 3 if faminc is greater than 50th percentile but less than or equal to the 75th percentile, it takes the value 4 if faminc is greater than the 75th percentile. Include the outcome of this code…arrow_forwardsolve this on paperarrow_forwardApply STATA commands & submit the output for each question only when indicated below i. Apply the command egen to create a variable called "wyd" which is the rowtotal function on variables bwght & faminc. ii. Apply the list command for the first 10 observations to show that the code in part i worked. Include the outcome of this code iii. Apply the egen command to create a new variable called "bwghtsum" using the sum function on variable bwght by the variable high_faminc (Note: need to apply the bysort' statement) iv. Apply the "by high_faminc" statement to find the V. descriptive statistics of bwght and bwghtsum Include the output of this code. Why is there a difference between the standard deviations of bwght and bwghtsum from part iv of this question?arrow_forward
- According to a health information website, the distribution of adults’ diastolic blood pressure (in millimeters of mercury, mmHg) can be modeled by a normal distribution with mean 70 mmHg and standard deviation 20 mmHg. b. Above what diastolic pressure would classify someone in the highest 1% of blood pressures? Show all calculations used.arrow_forwardWrite STATA codes which will generate the outcomes in the questions & submit the output for each question only when indicated below i. ii. iii. iv. V. Write a code which will allow STATA to go to your favorite folder to access your files. Load the birthweight1.dta dataset from your favorite folder and save it under a different filename to protect data integrity. Call the new dataset babywt.dta (make sure to use the replace option). Verify that it contains 2,998 observations and 8 variables. Include the output of this code. Are there missing observations for variable(s) for the variables called bwght, faminc, cigs? How would you know? (You may use more than one code to show your answer(s)) Include the output of your code (s). Write the definitions of these variables: bwght, faminc, male, white, motheduc,cigs; which of these variables are categorical? [Hint: use the labels of the variables & the browse command] Who is this dataset about? Who can use this dataset to answer what kind of…arrow_forwardApply STATA commands & submit the output for each question only when indicated below İ. ii. iii. iv. V. Apply the command summarize on variables bwght and faminc. What is the average birthweight of babies and family income of the respondents? Include the output of this code. Apply the tab command on the variable called male. How many of the babies and what share of babies are male? Include the output of this code. Find the summary statistics (i.e. use the sum command) of the variables bwght and faminc if the babies are white. Include the output of this code. Find the summary statistics (i.e. use the sum command) of the variables bwght and faminc if the babies are male but not white. Include the output of this code. Using your answers to previous subparts of this question: What is the difference between the average birthweight of a baby who is male and a baby who is male but not white? What can you say anything about the difference in family income of the babies that are male and male…arrow_forward
- A public health researcher is studying the impacts of nudge marketing techniques on shoppers vegetablesarrow_forwardThe director of admissions at Kinzua University in Nova Scotia estimated the distribution of student admissions for the fall semester on the basis of past experience. Admissions Probability 1,100 0.5 1,400 0.4 1,300 0.1 Click here for the Excel Data File Required: What is the expected number of admissions for the fall semester? Compute the variance and the standard deviation of the number of admissions. Note: Round your standard deviation to 2 decimal places.arrow_forwardA pollster randomly selected four of 10 available people. Required: How many different groups of 4 are possible? What is the probability that a person is a member of a group? Note: Round your answer to 3 decimal places.arrow_forward
- Wind Mountain is an archaeological study area located in southwestern New Mexico. Potsherds are broken pieces of prehistoric Native American clay vessels. One type of painted ceramic vessel is called Mimbres classic black-on-white. At three different sites the number of such sherds was counted in local dwelling excavations. Test given. Site I Site II Site III 63 19 60 43 34 21 23 49 51 48 11 15 16 46 26 20 31 Find .arrow_forwardRothamsted Experimental Station (England) has studied wheat production since 1852. Each year many small plots of equal size but different soil/fertilizer conditions are planted with wheat. At the end of the growing season, the yield (in pounds) of the wheat on the plot is measured. Suppose for a random sample of years, one plot gave the following annual wheat production (in pounds): 4.46 4.21 4.40 4.81 2.81 2.90 4.93 3.54 4.16 4.48 3.26 4.74 4.97 4.02 4.91 2.59 Use a calculator to verify that the sample variance for this plot is . Another random sample of years for a second plot gave the following annual wheat production (in pounds): 3.89 3.81 3.95 4.07 4.01 3.73 4.02 3.78 3.72 3.96 3.62 3.76 4.02 3.73 3.94 4.03 Use a calculator to verify that the sample variance for this plot is . Suppose that we test the claim using that the population variance of annual wheat production for the first plot is larger…arrow_forwardIt is thought that prehistoric Native Americans did not take their best tools, pottery, and household items when they visited higher elevations for their summer camps. It is hypothesized that archaeological sites tend to lose their cultural identity and specific cultural affiliation as the elevation of the site increases. Let x be the elevation (in thousands of feet) for an archaeological site in the southwestern United States. Let y be the percentage of unidentified artifacts (no specific cultural affiliation) at a given elevation. Suppose that the following data were obtained for a collection of archaeological sites in New Mexico: x 5.50 6.00 6.75 7.00 7.75 y 37 38 92 70 99 Find the equation of the least squares line . Round a and b to three decimal places.arrow_forward
- 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




