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
- Algebra & Trigonometry with Analytic GeometryAlgebraISBN:9781133382119Author:SwokowskiPublisher:CengageMathematics For Machine TechnologyAdvanced MathISBN:9781337798310Author:Peterson, John.Publisher:Cengage Learning,