
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
- What would you say about a set of quantitative bivariate data whose linear correlation is -1? What would a scatter diagram of the data look like? (5 points)arrow_forwardBusiness discussarrow_forwardAnalyze the residuals of a linear regression model and select the best response. yes, the residual plot does not show a curve no, the residual plot shows a curve yes, the residual plot shows a curve no, the residual plot does not show a curve I answered, "No, the residual plot shows a curve." (and this was incorrect). I am not sure why I keep getting these wrong when the answer seems obvious. Please help me understand what the yes and no references in the answer.arrow_forward
- a. Find the value of A.b. Find pX(x) and py(y).c. Find pX|y(x|y) and py|X(y|x)d. Are x and y independent? Why or why not?arrow_forwardAnalyze the residuals of a linear regression model and select the best response.Criteria is simple evaluation of possible indications of an exponential model vs. linear model) no, the residual plot does not show a curve yes, the residual plot does not show a curve yes, the residual plot shows a curve no, the residual plot shows a curve I selected: yes, the residual plot shows a curve and it is INCORRECT. Can u help me understand why?arrow_forwardYou have been hired as an intern to run analyses on the data and report the results back to Sarah; the five questions that Sarah needs you to address are given below. please do it step by step on excel Does there appear to be a positive or negative relationship between price and screen size? Use a scatter plot to examine the relationship. Determine and interpret the correlation coefficient between the two variables. In your interpretation, discuss the direction of the relationship (positive, negative, or zero relationship). Also discuss the strength of the relationship. Estimate the relationship between screen size and price using a simple linear regression model and interpret the estimated coefficients. (In your interpretation, tell the dollar amount by which price will change for each unit of increase in screen size). Include the manufacturer dummy variable (Samsung=1, 0 otherwise) and estimate the relationship between screen size, price and manufacturer dummy as a multiple…arrow_forward
- Here is data with as the response variable. x y54.4 19.124.9 99.334.5 9.476.6 0.359.4 4.554.4 0.139.2 56.354 15.773.8 9-156.1 319.2Make a scatter plot of this data. Which point is an outlier? Enter as an ordered pair, e.g., (x,y). (x,y)= Find the regression equation for the data set without the outlier. Enter the equation of the form mx+b rounded to three decimal places. y_wo= Find the regression equation for the data set with the outlier. Enter the equation of the form mx+b rounded to three decimal places. y_w=arrow_forwardYou have been hired as an intern to run analyses on the data and report the results back to Sarah; the five questions that Sarah needs you to address are given below. please do it step by step Does there appear to be a positive or negative relationship between price and screen size? Use a scatter plot to examine the relationship. Determine and interpret the correlation coefficient between the two variables. In your interpretation, discuss the direction of the relationship (positive, negative, or zero relationship). Also discuss the strength of the relationship. Estimate the relationship between screen size and price using a simple linear regression model and interpret the estimated coefficients. (In your interpretation, tell the dollar amount by which price will change for each unit of increase in screen size). Include the manufacturer dummy variable (Samsung=1, 0 otherwise) and estimate the relationship between screen size, price and manufacturer dummy as a multiple linear…arrow_forwardExercises: Find all the whole number solutions of the congruence equation. 1. 3x 8 mod 11 2. 2x+3= 8 mod 12 3. 3x+12= 7 mod 10 4. 4x+6= 5 mod 8 5. 5x+3= 8 mod 12arrow_forward
- Scenario Sales of products by color follow a peculiar, but predictable, pattern that determines how many units will sell in any given year. This pattern is shown below Product Color 1995 1996 1997 Red 28 42 21 1998 23 1999 29 2000 2001 2002 Unit Sales 2003 2004 15 8 4 2 1 2005 2006 discontinued Green 26 39 20 22 28 14 7 4 2 White 43 65 33 36 45 23 12 Brown 58 87 44 48 60 Yellow 37 56 28 31 Black 28 42 21 Orange 19 29 Purple Total 28 42 21 49 68 78 95 123 176 181 164 127 24 179 Questions A) Which color will sell the most units in 2007? B) Which color will sell the most units combined in the 2007 to 2009 period? Please show all your analysis, leave formulas in cells, and specify any assumptions you make.arrow_forwardOne hundred students were surveyed about their preference between dogs and cats. The following two-way table displays data for the sample of students who responded to the survey. Preference Male Female TOTAL Prefers dogs \[36\] \[20\] \[56\] Prefers cats \[10\] \[26\] \[36\] No preference \[2\] \[6\] \[8\] TOTAL \[48\] \[52\] \[100\] problem 1 Find the probability that a randomly selected student prefers dogs.Enter your answer as a fraction or decimal. \[P\left(\text{prefers dogs}\right)=\] Incorrect Check Hide explanation Preference Male Female TOTAL Prefers dogs \[\blueD{36}\] \[\blueD{20}\] \[\blueE{56}\] Prefers cats \[10\] \[26\] \[36\] No preference \[2\] \[6\] \[8\] TOTAL \[48\] \[52\] \[100\] There were \[\blueE{56}\] students in the sample who preferred dogs out of \[100\] total students.arrow_forwardBusiness discussarrow_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




