Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

MATLAB for Engineers 5th Edition

Moore Solutions Manual


Visit to download the full and correct content document: https://testbankdeal.com/dow
nload/matlab-for-engineers-5th-edition-moore-solutions-manual/
Matlab for Engineers, 5th Edition
Chapter 7 Homework Solutions

clear,clc, close all

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 = input('Enter a value of 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]

x = input('Enter a matrix -- don''t forget [ and ] ')


max(x)

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 = input('Enter the area of the base of a cone ')

area = 10.00

h = input('Enter the height of a cone ')

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 = input('Enter your first name, inside single quotes ')

first_name = Holly

last_name = input('Enter your last name, inside single quotes ')

last_name = Moore

disp(['Your name is: ',first_name,' ',last_name])

Your name is: Holly Moore

or... use the 's' modifier

first_name=input('Enter your first name ','s');


last_name = input('Enter your last name ','s');
disp(['Your name is: ', first_name,' ',last_name])

Your name is: Holly 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.

age = input('Enter your age ');


disp(['your age is: ',num2str(age)])

your age is: 29

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.

x = input('Enter an array of numbers ')

x = 8.00 7.00 4.00 10.00 3.00

num = length(x);
disp(['You entered ',num2str(num),' numbers'])

You entered 5 numbers

fprintf

Problem 7.8
Repeat Problem 7.7, and use fprintf to report your results.

fprintf('You entered %3.0f numbers \n',num)

You entered 5 numbers

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:

· Create a vector of angle values from 0 to 2πin increments of π/10.

· Calculate the sine of each of the angles, and group your results into an

· array that includes the angle and the sine.

· 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');

A table of angles and corresponding sine values

disp('theta Sin(theta)');

theta Sin(theta)

fprintf('%5.2f %5.2f \n',result)

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:

· Use disp to create a title and column headings.

· Use fprintf to display the numerical information.


· Because the length represented in angstroms is so big, represent your result in scientific notation,
showing two values after the decimal point. This corresponds to three significant figures (one before and
two after the decimal point).

inches = 1:10;
angstroms = inches*2.54*10^8;
disp('Inches Are to big to measure in Angstroms')

Inches Are to big to measure in Angstroms

disp('Inches Angstroms')

Inches Angstroms

fprintf('%5.0f %7.2e \n',[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

1 Japanese Yen = 0.00979 US Dollar

1 US Dollar (USD) = 102.02 Japanese Yen (JPY)

yen = 5:5:(5 + 24*5);


dollars = yen*0.00979;
result = [yen;dollars]

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

disp('Conversion from Yen to Dollars - August 1, 2016')

Conversion from Yen to Dollars - August 1, 2016


disp('Yen Dollars')

Yen Dollars

fprintf('%5.0f %8.4f \n',result)

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:

1 Euro = 1.11696 US Dollar

1 US Dollar (USD) = 0.89449 Euro (EUR)

euro = 1:2:(1+29*2);
dollar = euro*1.11696;
disp('Euros Dollars')

Euros Dollars

fprintf('%5.0f %8.2f \n',[euro; dollar])

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:

1 British Pound = 1.3221 US Dollar

1 US Dollar (USD) = 0.75574 British Pound (GBP)

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')

Conversion table $ to various currencies

disp('dollars euros pounds yen')

dollars euros pounds yen

fprintf('%5.0f %8.3f %8.3f %8.3f \n',result)

1 0.895 0.756 102.020


2 1.791 1.511 204.040
3 2.686 2.267 306.060
4 3.581 3.023 408.080
5 4.476 3.779 510.100
6 5.372 4.534 612.120
7 6.267 5.290 714.140
8 7.162 6.046 816.160
9 8.058 6.802 918.180
10 8.953 7.557 1020.200

Problems using the table function


Problem 7.13
Consider the patient data contained in the table.

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.

c) Create column vectors for age, height and weight.

d) Display the information using the table function.

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))

last first age height weight


__________ ________ _____ ______ ______
'Smith' 'Fred' 6.00 47.00 82.00
'Jones' 'Kathy' 22.00 66.00 140.00
'Webb' 'Milton' 92.00 62.00 110.00
'Anderson' 'John' 45.00 72.00 190.00

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

fprintf('%5.0f %8.2f \n', [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.

start = input('Enter the starting temperature ');


incr = input('Enter the increment ');
stop = start +incr*24;
C = start:incr:stop;
R = (C + 273)*9/5;
disp('Temperature Conversion')

Temperature Conversion

disp(' C R')

C R

fprintf('%5.0f %8.2f \n',[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 = input('Enter the starting temperature ')

start = 0

incr = input('Enter the increment ')

incr = 20.00

num = input('Enter the number of lines ')

num = 6.00

stop = start + incr*(num-1);


C = start:incr:stop;
F = 9/5*C + 32;
disp('Temperature Conversion')

Temperature Conversion

disp(' C F')

C F

fprintf('%5.0f %8.2f \n',[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:

1 kW = 3412.14 Btu/h =737.56 ft lbf/s

1 hp = 550 ft lbf/s =2544.5 Btu/h

(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.

incr = input('Enter the increment ')


incr = 2.50

start = 0;
stop = 15;
kW = start:incr:stop;
hP = 3412.14/2544.5 * kW;
disp('Power Conversion')

Power Conversion

disp(' kW hP')

kW hP

fprintf('%8.1f %8.2f \n', [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) Repeat part a using the table function.

% 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 = input('Enter the final value ')

stop = 50.00
ftlbs = 0:incr:stop;
Btuhr = 550/2544.5 * ftlbs;
disp(' Power Conversion')

Power Conversion

disp(' ft lbf/sec Btu/hr')

ft lbf/sec Btu/hr

fprintf('%8.0f %8.2f \n', [ftlbs;Btuhr])

0 0.00
10 2.16
20 4.32
30 6.48
40 8.65
50 10.81

(d) Repeat part c using the table function.

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 = input('Enter the starting value ')

start = 0

stop = input('Enter the final value ')

stop = 100.00

num = input('Enter the number of entries ')

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

disp(' kW Btu/hr hP ft lbf/s')

kW Btu/hr hP ft lbf/s

fprintf('%8.0f %8.2f %8.2f %8.2f \n', [kW;Btu_per_hour; hP;ft_lbf_per_sec ])

0 0.00 0.00 0.00


20 68242.80 26.82 14751.20
40 136485.60 53.64 29502.40
60 204728.40 80.46 44253.60
80 272971.20 107.28 59004.80
100 341214.00 134.10 73756.00

(f) Repeat part d using the table function.

disp(' Power Conversion')

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))

Kilowatts Btu_per_hr HorsePower ftlbf_per_s


_________ __________ __________ ___________
0.00 0.00 0.00 0.00
20.00 68242.80 26.82 14751.20
40.00 136485.60 53.64 29502.40
60.00 204728.40 80.46 44253.60
80.00 272971.20 107.28 59004.80
100.00 341214.00 134.10 73756.00

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

Plot the height of the rocket from 0 to 30 seconds, and

· 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')

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'])

The maximum height reached by the rocket is approximately 1290.379 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')

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)

The distance between the points is 1.03 units

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.

One way to compare two vehicles is to find the “cost to own.”

Cost to own =Purchase cost + Upkeep +Gasoline cost

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.

gas_cost=input('Estimate the price per gallon of gasoline ');


miles=0:100:400000;
cost_to_own_camry=23070 + miles/25 * gas_cost;
cost_to_own_camry_hybrid=26790 + miles/43 * gas_cost;
plot(miles,cost_to_own_camry,miles,cost_to_own_camry_hybrid)
title('Comparison of Std. Camry and Hybrid Models')
xlabel('Distance driven, miles')
ylabel('Cost to own')
[break_even, cost]=ginput(1)

break_even = 94470.05
cost = 32004.37

a=sprintf('The break even milage is %7.0f \n',break_even)

a =
The break even milage is 94470

gtext(a)

You might also like