
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
- m Proctor ments sources Dak is investigating how long their phone's battery lasts (in hours) for various brightness levels (on a scale of 0-100). A part of the regression analysis done on Ti Calculator and in Excel for their data are displayed below. Ti Calculator Click this button to reveal the Ti Calculator Output. Click again to collapse it. Ti Calculator y = ax+b B0 and p 0 t = -3.02431335 p = 0.0233 df - 6 a =-0.0380555845 b=5.88809202 s = 0.436054238 20.603868129 r = -0.777089524 Resources Excel Type here to search Click this button to reveal the Excel Output. Click again to collapse it. SUMMARY OUTPUT Multiple R 0.777089524 R Square 0.603868129 Adjusted R Square 0.537846150 Standard Error 0.436054238 Observations 8 ANOVA df SS MS F Significance F Regression 1 1.73914021 Residual 6 1.14085979 1.73914021 0.190143298 9.1465 0.0233 Total 7 2.88000000 Coefficients Standard Error Intercept Brightness 5.88809202 -0.0380555845 t Stat 7.62593816 -3.02431335 P-value 0.0003 0.0233 Use…arrow_forwardce Problems x + canvas.pdx.edu/courses/106252/assignments/1102408?module_item_id=4856656 m Proctor ments THIS IS THE second question of exdin where you have to Snow compere work to get run points. If you use TI calculator functions, you may write your work along with functions and values you I used and answers on a paper or type on an empty Word document or Excel file. If you use Ti calculator app on your computer or phone, you may provide screen shots of those on a word document or Excel file. Clearly provide the function along with values for the work. If you use Excel, you may use the Excel calculator or Excel as a calculator on your computer to provide complete work and the final answer on it. Please provide all the answers to the following questions on the same Excel sheet. If you use formula, you may write all your steps with numbers on a paper and submit a photo of it. Otherwise, you may type all those steps on a Word document or an Excel file. Do NOT round in the middle steps…arrow_forwardSuppose the model for certain data is a parabola y = Bo+B1x+B2x² with observations (1, 2.2), (2, 6.9), (3, 16.1), (4, 28.7), (5, 46.1). Describe the design matrix, the observation vector, and the parameter vector. Using these write down the system of equations to be approximated Xẞ = y, the parts of the normal equations XTX and XTy and write down the normal equations. Solve and determine the residual vector €. You may use a calculator for the computations but show the steps as described above.arrow_forward
- . This problem will yield a standard formula given in elementary statistics for a least squares line, making use of the normal equations. (a) Given pairs of data points (x1, Y1), (x2, Y2), ..., (xn, Yn) consider approximating lines of the form y = mx+b. The error e; for the ith pair is the distance between y; and the height (y value) of the line at xi. This is ei = Yi — (mxi + b). If we consider the equations b + x;m = Yi for i n in the variables b and m we can = 1,2, = think of this as a system of equations Ax = 6 where A 1 x1 x2 = : [m] Хп Y1 Уп numbers. Here, note that the variables are m and b and the xi, Yi are given The least squares approximation for this system (which gives the intercept b and slope m of the best least squares line for the data) is the solution to the normal equations AT Ax = ATb. Determine ATA (a 2×2 matrix) and AT (a 2×1 matrix). The entries will be sums of terms involving the x; and y₁. Write these, first using Σ notation and then simplify the notation using…arrow_forward= a) Recall that the formula for the projection p of vector & onto vector a is p = ±ªã. The plane through the origin in R³ given by ax+by+cz = 0 for real numbers a, b, c, is the set of all points (viewed as vectors) orthogonal to the normal n = (a, b, c) to the plane. To find the distance from a point w (xo, Yo, zo) to the plane ax+by+cz = 0 we can find the projection of w onto the normal ñ and then find the length of this projection. Do this to derive a generic formula for the distance in terms of xo, Yo, Zo, a, b, c. First find the length squared and then take a root. b) To find the distance from a point w (xo, Yo, zo) to a plane ax + by + cz = d, not necessarily not through the origin, one approach is to shift all z coordinates down by d. That is, replace (x, y, z) by (x, y, z — d). The relationship between the plane and w is unchanged so we can find the distance by finding the distance between (xo, Yo, zo - d) and the plane ax + by+cz = 0 through the origin. Do this to derive a…arrow_forwardUse Gram-Schmidt orthogonalization to find an orthonormal set of vectors with the same span as = 1 2 -0-08-0 a2 = 3 a3 = -2 Find also a QR factorization A = QR with the columns of Q the orthonormal set found above and R upper triangular and invertible and A with columns ā₁, ā2, ā3.arrow_forward
- (a) Let Q be an orthonormal matrix. That is, the columns of Q have unit length and are pairwise orthogonal. Prove that the length of Qã is equal to the length of for any column vector x. Note that you can do this by showing that the lengths squared are equal. (b) Prove that if A and B are symmetric and πT Ax = x² Bã for all & then A = B. Make use of coordinate vectors ē;, which are 0 except for a 1 in position i. Explain why, for matrix A, entries can be found by arj = Aej. First show that diagonal = = entries aii bir. Then consider what (ē¿ + ē¿)ªA(ē¿ + ē¿) in terms of entries of A (and similarly for B) and make use of symmetry aij = aji and similarly for B. (c) Prove the converse: If for all x, ||Q|| ||||| then Q is orthonormal. One ap- proach is to use part (b) to show that QTQ = I. Recall that QTQ is symmetric (and trivially I is symmetric). Recall also that, written in matrix notation, vector length (squared) is given by ||7||2 = (this is the dot product written in matrix…arrow_forward(a) What is wrong with the following statement and proof? A is invertible if and only if ATA is invertible. 'Proof': Since det(A) = det(AT), det(ATA) = det(A)². Either both are 0 (and both A and ATA are not invertible) or both are nonzero (and both A and ATA are invertible). (b) Prove that Null(A) C Null(ATA). That is, prove that if Ax = 0 then AT Ax = 0. (c) Prove that Null(ATA) C Null(A). That is, prove that if AT Añ = 0 then Aã = Ổ. Recall that the length (squared) of a vector v is V, which written in matrix multiplication notation is Tv. (d) Parts (b) and (c) imply that Null(A) = • Nul(ATA). Use this to prove that the columns of A are linearly independent if and only if ATA has an inverse.arrow_forwardGive a 99% confidence interval, for μ1 M2 given the following information. n₁ = 20, 1 = 2.31, s₁ = 0.47 n2 = 40, 2 = 2.21, 82 = 0.55 + Use Technology Rounded to 2 decimal places. Hint Question Help: Video Submit Question ch Η UP T SINK O DELL F2 F3 F4 F5 F6 F7 F8 144 ►11 4arrow_forward
- Please could you provide solutions to the following questions. Thanksarrow_forwardPlease could you provide solutions to the following questions. Thanksarrow_forwardor Beer and blood alcohol content: Many people believe that gender, weight, drinking habits, and many other factors are much more important in predicting blood alcohol content (BAC) than simply considering the number of drinks a person consumed. Here we examine data from sixteen student volunteers at Ohio State University who each drank a randomly assigned number of cans of beer. These students were evenly divided between men and women, and they differed in weight and drinking habits. Thirty minutes later, a police officer measured their blood alcohol content (BAC) in grams of alcohol per deciliter of blood (Malkevitc and Lesser, 2008). The scatterplot and regression table summarize the findings. to search BAC (grams per deciliter) 0.15- 0.10- 0.05- Part of Ti Calculator Output Ti Calculator y = ax+b Be and p = 0 t = 7.48 P 0.0000 df = 14 a = 0.0180 b = -0.0127 Part of Excel Output 2 4 0 0 6 Cans of beer (Intercept) beers Estimate -0.0127 0.0180 Std. Error t value 0.0126 P(>|t|) -1.00…arrow_forward
- Algebra & Trigonometry with Analytic GeometryAlgebraISBN:9781133382119Author:SwokowskiPublisher:Cengage
Mathematics 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 Littell
Trigonometry (MindTap Course List)TrigonometryISBN:9781305652224Author:Charles P. McKeague, Mark D. TurnerPublisher:Cengage Learning
Glencoe Algebra 1, Student Edition, 9780079039897...AlgebraISBN:9780079039897Author:CarterPublisher:McGraw Hill




