Professional Documents
Culture Documents
Matlab For Engineers 5th Edition Moore Solutions Manual
Matlab For Engineers 5th Edition Moore Solutions Manual
Input Function
Problem 7.1
Create an M-file that prompts the user to enter a value of x and then calculates the value of sin (x).
x = 3.14
result = sin(x)
result = 0.00
Problem 7.2
Create an M-file that prompts the user to enter a matrix and then use the max function to determine the
largest value entered. Use the following matrix to test your program:
[1, 5, 3, 8, 9, 22]
Problem 7.3
The volume of a cone is
Prompt the user to enter the area of the base and the height of the cone (Figure P7.3). Calculate the
volume of the cone.
area = 10.00
h = 6.00
volume = 1/3*area*h
volume = 20.00
Disp Function
Problem 7.4
One of the first computer programs many students write is called “Hello, World.” The only thing the
program does is print this message to the com puter screen. Write a “Hello, World” program in an M-file,
using the disp function.
disp('Hello World')
Hello World
Problem 7.5
Use two separate input statements to prompt a user to enter his or her first and last names. Use
the disp function to display those names on one line. (You’ll need to combine the names and some
spaces into an array.)
first_name = Holly
last_name = Moore
Problem 7.6
Prompt the user to enter his or her age. Then use the disp function to report the age back to the
command window. If, for example, the user enters 5 when prompted for her age, your display should
read
Your age is 5
This output requires combining both character data (a string) and numeric data in the disp function—
which can be accomplished by using the num2str function.
Problem 7.7
Prompt the user to enter an array of numbers. Use the length function to determine how many values
were entered, and use the disp function to report your results to the command window.
num = length(x);
disp(['You entered ',num2str(num),' numbers'])
fprintf
Problem 7.8
Repeat Problem 7.7, and use fprintf to report your results.
Problem 7.9
Use fprintf to create the multiplication tables from 1 to 13 for the number 6. Your table should look
like this.
1 times 6 is 6
2 times 6 is 12
3 times 6 is 18 etc.
num = 1:6;
mult = num*6;
result =[num;mult];
fprintf('%5.0f times 6 is %3.0f \n',result)
1 times 6 is 6
2 times 6 is 12
3 times 6 is 18
4 times 6 is 24
5 times 6 is 30
6 times 6 is 36
Problem 7.10
Before calculators were readily available (about 1974), students used tables to determine the values of
mathematical functions like sine, cosine, and log. Create such a table for sine, using the following steps:
· Calculate the sine of each of the angles, and group your results into an
· Use disp to create a title for the table and a second disp command to create column headings.
· Use the fprintf function to display the numbers. Display only two values past the decimal point.
theta=0:pi/10:pi*2;
result =[theta;sin(theta)];
disp('A table of angles and corresponding sine values');
disp('theta Sin(theta)');
theta Sin(theta)
0.00 0.00
0.31 0.31
0.63 0.59
0.94 0.81
1.26 0.95
1.57 1.00
1.88 0.95
2.20 0.81
2.51 0.59
2.83 0.31
3.14 0.00
3.46 -0.31
3.77 -0.59
4.08 -0.81
4.40 -0.95
4.71 -1.00
5.03 -0.95
5.34 -0.81
5.65 -0.59
5.97 -0.31
6.28 -0.00
Problem 7.11
Very small dimensions—those on the atomic scale—are often measured in angstroms. An angstrom is
represented by the symbol Å and corresponds to a length of 10−10 m. Create an inches-to-angstroms
conversion table as follows for values of inches from 1 to 10:
inches = 1:10;
angstroms = inches*2.54*10^8;
disp('Inches Are to big to measure in Angstroms')
disp('Inches Angstroms')
Inches Angstroms
1 2.54e+08
2 5.08e+08
3 7.62e+08
4 1.02e+09
5 1.27e+09
6 1.52e+09
7 1.78e+09
8 2.03e+09
9 2.29e+09
10 2.54e+09
Problem 7.12
Use your favorite Internet search engine and World Wide Web browser to identify recent currency
conversions for British pounds sterling, Japanese yen, and the European euro to US dollars. Use
the conversion tables to create the following tables (use the disp and fprintf commands in your
solution, which should include a title, column labels, and formatted output):
(a) Generate a table of conversions from yen to dollars. Start the yen column at 5 and increment by 5
yen. Print 25 lines in the table.
On Monday, August 1st 2016 , the conversion from yen to dollars was
result =
5.00 10.00 15.00 20.00 25.00 30.00 35.00
0.05 0.10 0.15 0.20 0.24 0.29 0.34
Yen Dollars
5 0.0490
10 0.0979
15 0.1469
20 0.1958
25 0.2448
30 0.2937
35 0.3427
40 0.3916
45 0.4406
50 0.4895
55 0.5384
60 0.5874
65 0.6363
70 0.6853
75 0.7342
80 0.7832
85 0.8322
90 0.8811
95 0.9301
100 0.9790
105 1.0279
110 1.0769
115 1.1259
120 1.1748
125 1.2238
(b) Generate a table of conversions from the euros to dollars. Start the euro column at 1 euro and
increment by 2 euros. Print 30 lines in the table.
On Monday, August 1st 2016, the conversion from Euro to Dollars was:
euro = 1:2:(1+29*2);
dollar = euro*1.11696;
disp('Euros Dollars')
Euros Dollars
1 1.12
3 3.35
5 5.58
7 7.82
9 10.05
11 12.29
13 14.52
15 16.75
17 18.99
19 21.22
21 23.46
23 25.69
25 27.92
27 30.16
29 32.39
31 34.63
33 36.86
35 39.09
37 41.33
39 43.56
41 45.80
43 48.03
45 50.26
47 52.50
49 54.73
51 56.96
53 59.20
55 61.43
57 63.67
59 65.90
(c) Generate a table with four columns. The first should contain dollars, the second the equivalent
number of euros, the third the equivalent number of pounds, and the fourth the equivalent number of
yen. Let the dollar column vary from 1 to 10.
On Monday, August 1st 2016 , the conversion for pounds to dollars was:
dollars = 1:10;
euros = dollars/1.11696;
yen = dollars*102.02;
pounds = dollars * 0.75574;
result = [dollars;euros; pounds; yen];
disp('Conversion table $ to various currencies')
a) Create a column vector of last names called last, using curly braces.
b) Create a column vector of first names called first, using curly braces.
last = {'Smith','Jones','Webb','Anderson'}';
first = {'Fred','Kathy','Milton','John'}';
age =[6;22;92;45];
height = [47;66;62;72];
weight =[82;140;110;190];
disp(table(last,first,age,height,weight))
Problems Combining the input, disp, and fprintf and table Commands
Problem 7.14
This problem requires you to generate temperature conversion tables. Use the following equations,
which describe the relationships between temperatures in degrees Fahrenheit (TF), degrees Celsius
(TC), kelvins (TK), and degrees Rankine (TR), respectively:
You will need to rearrange these expressions to solve some of the problems.
(a) Generate a table of conversions from Fahrenheit to Kelvin for values from 0°F to 200°F. Allow the
user to enter the increments in degrees F between lines. Use disp and fprintf to create a table with
a title, column headings, and appropriate spacing.
start = 0;
stop = 200;
incr = input('Enter the temperature increment ');
F=start:incr:stop;
K = (F+459.6)*5/9;
disp('Temperature Conversions')
Temperature Conversions
disp(' F K')
F K
0 255.33
50 283.11
100 310.89
150 338.67
200 366.44
(b) Generate a table of conversions from Celsius to Rankine. Allow the user to enter the starting
temperature and the increment between lines. Print 25 lines in the table. Use disp and fprintf to
create a table with a title, column headings, and appropriate spacing.
Temperature Conversion
disp(' C R')
C R
0 491.40
20 527.40
40 563.40
60 599.40
80 635.40
100 671.40
120 707.40
140 743.40
160 779.40
180 815.40
200 851.40
220 887.40
240 923.40
260 959.40
280 995.40
300 1031.40
320 1067.40
340 1103.40
360 1139.40
380 1175.40
400 1211.40
420 1247.40
440 1283.40
460 1319.40
480 1355.40
(c) Generate a table of conversions from Celsius to Fahrenheit. Allow the user to enter the starting
temperature, the increment between lines, and the number of lines for the table. Use disp and
fprintf to create a table with a title, column headings, and appropriate spacing.
start = 0
incr = 20.00
num = 6.00
Temperature Conversion
disp(' C F')
C F
0 32.00
20 68.00
40 104.00
60 140.00
80 176.00
100 212.00
Problem 7.15
Engineers use both English and SI (Système International d’Unités) units on a regular basis. Some
fields use primarily one or the other, but many combine the two systems. For example, the rate of
energy input to a steam power plant from burning fossil fuels is usually measured in Btu/hour. However,
the electricity produced by the same plant is usually measured in joules/s (watts). Automobile engines,
by contrast, are often rated in horsepower or in ft lbf/s. Here are some conversion factors relating these
different power measurements:
(a) Generate a table of conversions from kW to hp. The table should start at 0 kW and end at 15 kW.
Use the input function to let the user define the increment between table entries. Use disp and
fprintf to create a table with a title, column headings, and appropriate spacing.
start = 0;
stop = 15;
kW = start:incr:stop;
hP = 3412.14/2544.5 * kW;
disp('Power Conversion')
Power Conversion
disp(' kW hP')
kW hP
0.0 0.00
2.5 3.35
5.0 6.70
7.5 10.06
10.0 13.41
12.5 16.76
15.0 20.11
% b
disp(' Power Conversion')
Power Conversion
disp(table(kW',hP','VariableNames',{'Kilowatts','HorsePower'}))
Kilowatts HorsePower
_________ __________
0.00 0.00
2.50 3.35
5.00 6.70
7.50 10.06
10.00 13.41
12.50 16.76
15.00 20.11
(c) Generate a table of conversions from ft lbf/s to Btu/h. The table should start at 0 ft lbf/s but let the
user define the increment between table entries and the final table value. Use disp and fprintf to
create a table with a title, column headings, and appropriate spacing.
start = 0;
incr = input('Enter the increment ')
incr = 10.00
stop = 50.00
ftlbs = 0:incr:stop;
Btuhr = 550/2544.5 * ftlbs;
disp(' Power Conversion')
Power Conversion
ft lbf/sec Btu/hr
0 0.00
10 2.16
20 4.32
30 6.48
40 8.65
50 10.81
disp('Power Conversion')
Power Conversion
disp(table(ftlbs',Btuhr','VariableNames',{'Foot_Pounds','Btu_hr'}))
Foot_Pounds Btu_hr
___________ ______
0.00 0.00
10.00 2.16
20.00 4.32
30.00 6.48
40.00 8.65
50.00 10.81
(e) Generate a table that includes conversions from kW to Btu/h, hp, and ft lbf/s. Let the
user define the initial value of kW, the final value of kW, and the number of entries in
the table. Use disp and fprintf to create a table with a title, column headings, and
appropriate spacing.
start = 0
stop = 100.00
num = 6.00
kW = linspace(start,stop,num);
Btu_per_hour = kW * 3412.14;
ft_lbf_per_sec = 737.56 * kW;
hP = kW * 3412.14 / 2544.5;
disp('Power Conversion')
Power Conversion
kW Btu/hr hP ft lbf/s
Power Conversion
v_names = {'Kilowatts','Btu_per_hr','HorsePower','ftlbf_per_s'};
disp(table(kW', Btu_per_hour', hP',ft_lbf_per_sec','VariableNames',v_names))
ginput
Problem 7.16
At time t = 0, when a rocket’s engine shuts down, the rocket has reached an altitude of 500 m and is
rising at a velocity of 125 m/s. At this point, gravity takes over. The height of the rocket as a function of
time is
· Use the ginput function to estimate the maximum height the rocket reaches and the time when the
rocket hits the ground.
· Use the disp command to report your results to the command window.
t = 0:30;
h = -9.8/2*t.^2 + 125*t + 500;
plot(t,h)
disp('Position the cursor over the maximum height on the graph')
[a,b] = ginput(1)
a = 12.62
b = 1290.38
disp(['The maximum height reached by the rocket is approximately ' num2str(b) ' meters'])
disp(['Position the cursor over the position where the rocket hits the ground'])
Position the cursor over the position where the rocket hits the ground
[a,b] = ginput(1)
a = 29.14
b = -6.41
disp(['The time the rocket hits the ground is ' num2str(a) ' seconds'])
The time the rocket hits the ground is 29.1359 seconds
Problem 7.17
The ginput function is useful for picking distances off a graph. Demonstrate this feature by doing the
following:
· Create a graph of a circle by defining an array of angles from 0 to 2 π, with a spacing of π/100.
· Use the ginput function to pick two points on the circumference of the circle.
· Use hold onto keep the figure from refreshing, and plot a line between the two points you picked.
· Use the data from the points to calculate the length of the line between them. (Hint: Use the
Pythagorean theorem in your calculation.)
theta = 0:0.1:2*pi;
r = ones(1,length(theta));
figure(2)
polar(theta,r)
disp('Use the cursor to enter two locations on the circle')
[a,b] = ginput(2)
a =
0.88
0.88
b =
0.51
-0.52
hold on
plot(a,b)
hold off
dist = sqrt((a(1)-a(2))^2+(b(1) - b(2))^2);
fprintf('The distance between the points is %4.2f units \n',dist)
Problem 7.18
In recent years, the price of gasoline has fluctuated dramatically. Automobile companies have
responded with more fuel-efficient cars, in particular, hybrid models. But will you save money by
purchasing a hybrid such as the Toyota Camry rather than a Camry with a standard engine? The hybrid
vehicles are considerably more expensive, but get better gas mileage. Consider the vehicle prices and
gas efficiencies shown in Table P7.18.
Assume for this exercise that the upkeep costs are the same, so in our comparison we’ll set them equal
to zero.
(a) What do you think the cost of gasoline will be over the next several years? Prompt the user to enter
an estimate of gasoline cost in dollars/gallon.
(b) Find the “cost to own” as a function of the number of miles driven for a pair of vehicles from the
table, based on the fuel price estimate from part (a). Plot your results on an x–y graph. The point where
the two lines cross is the break-even point.
(c) Use the ginput function to pick the break-even point off the graph.
(d) Use sprintf to create a string identifying the break-even point, and use the result to create a text-
box annotation on your graph. Position the text box using the gtext function.
break_even = 94470.05
cost = 32004.37
a =
The break even milage is 94470
gtext(a)