**Problems**

You can find answers to problems marked with an asterisk at the end of the text.

Section 4.1

1. The volume V and surface area A of a sphere of radius r are given by

a. Develop a pseudocode description of a program to compute V and A for 0 S r S 3 m, and to plot V versus A.

b. Write and run the program described in part Q.

2. The roots of the quadratic equation QX2 +bx + c = 0 are given by

-b± Jb2 -4QC x= 2Q

Q. Develop a pseudocode description of a program to compute both

roots given the values of Q, b, and c. Be sure to identify the real and

imaginary parts.

b. Write the program described in part Q and test it for the following

cases:

I. Q = 2, b = 10, c = 12

2. Q = 3, b = 24, c = 48

3. Q = 4, b = 24, c = 100

3. It is desired to compute the sum of the first ten terms of the series

14k3 – 20k2 + 5k, k = 1,2.3,

Q. Develop a pseudocode description of the required program.

b. Write and run the program described in part Q.

– Section4.2

4.* Suppose that x = 6. Find the results of the following operations by hand

and use MATLAB to check your results.

Q. z (x<10)

b. z (x==10)

c. Z (x>=4)

d. z = (x-=7)

5.* Find the results of the following operations by hand and use MATLAB to

check your results.

a. z 6>3+B.

b. z 6+3>B

c. z = 4> (2 +9 )

d. z,= (4<7) +3

e. z = 4<7+3

f z = (4<7)*5

g. z = 4«7*5)

h. z = 2/5>=5

6.* Suppose that x = [ 10 , – 2, 6, 5 , – 3] and y = [ 9, – 3, 2,

5, -1 ] . Find the results of the following operations by hand and use

MATLAB to check your results.

a. z = (x<6)

b. z (x<=y)

c. z = (x==y)

d. z = (x-=y)

7. For the arrays x and y given below, use MATLAB to find all the elements

in x that are greater than the corresponding elements in y.

x = [-3, 0, 0, 2, 6, B] y = [-5, -2, 0, 3, 4, 10]

8. The array price given below contains the price in dollars of a certain

stock over 10 days. Use MATLAB to determine how many days the price

was above $20.

price = [19, 18, 22, 21, 25, 19, 17, 21, 27, 29]

9: The arrays price_A and price_B given below contain the price in

dollars of two stocks over 10 days. Use MATLAB to determine how many

days the price of stock A was above the price of stock B.

price_A [19, 1B, 22, 21, 25, 19, 17, 21, 27, 29]

price_B = [22, 17, 20, 19,24,18,16,25, 2B, 27]

10. The arrays price_A, price_B, and price_C given below contain the

price in dollars of three stocks over 10 days. .

a. Use MATLAB to determine how many days the price of stock A was

above both the price of stock B and the price of stock C.

b. Use MATLAB to determine how many days the price of stock A was

above either the price of stock B or the price of stock C.

c. Use MATLAB to determine how many days the price of stock A was

above either the price of stock B or the price of stock C, but not both.

price_A [19, 1B, 22, 21, 25, 19, 17, 21, 27, 29]

price_B (22, 17, 20, 19, 24, 1B, 16, 25, 28, 27]

price_C = [17,13,22,23,19,17,20,21,24, 2B

Section 4.3

11.* Suppose that x = [-3, 0, 0, 2, 5, 8] andy = [-5, -2,

0, 3·, 4, 10].Find the results of the following operations by hand

and use MATLAB to check your results.

a. z y-c-x

b. z x&y

c. z = xly

d. z xor(x,y)

12. The height and speed of a projectile (such as a thrown ball) launched with

a speed of vo at an angle A to the horizontal are given by

where g is the acceleration due to gravity. The projectile will strike the

ground when h(t) = 0, which gives the time to hit thit = 2(vo/ g) sin A.

Suppose that A = 30°, Vo = 40 mis, and g = 9.81mls2. Use the

MATLAB relational and logical operators to find the times when

a. The height is no less than 15 m.

b. The height i’f no less than 15 m and the speed is simultaneously no

greater than 36 mls.

c. The height is less than 5 m or the speed is greater than 35 mls.

13.* The price, in dollars, of a certain stock over a 10-day period is given in the

following array.

price = [19, 18, 22, 21, 25, 19, 17, 21, 27, 29]

Suppose you owned 1000 shares at the start of the IO-day period, and you

bought 100 shares every day the price was below $20 and sold 100 shares

every day the price was above $25. Use MATLAB to compute (a) the

amount you spent in buying shares, (b) the amount you received from the

sale of shares, (c) the total number of shares you own after the 10th day,

and (d) the net increase in the worth of your portfolio.

14. Let e1 and e2 be logical expressions. DeMorgan’s laws for logical

expressions state that

NOT(e.l AND e2) implies that (NOT el) OR (NOT e2)

and

NOT(el OR e2) implies that (NOT el) AND (NOT e2)

Use these laws to find an equivalent expression for each of the following

expressions and use : 1ATLAB to verify the equivalence.

4 -(Iz < 10)&(x>=6))

h I (y — 2) I (.. > 5))

,15. Are these following expressions equivalent? Use MATLAB to check your

answer for specific values of a, b, c, and d.

a. 1. (a==b)&((b=:=c)l(a==c))

2. (a==b)1 (‘(b==c) & (a==c) )

b. 1. (a-cb) & ((a>c) I (ac-d) )

2. (a-cb) & (ac-e) I ( (a-cb) & (ac-d ) )

Section 4.4

16. Rewrite the following statements to ‘use only one if statement.

17. Write a program that accepts a numerical value x from 0 to 100 as input

and computes and displays the corresponding letter grade given by the

following table.

A x ~ 90

B. 80:::: x:::: 89

C 70::::x:::: 79

D 60::::x:::: 69

F x < 60

a. Use nested if statements in your program (do not use elsei f).

b. Use only elseif clauses in your program.

18. Write a program that accepts a year and determines whether or not the year

is a leap year. Use the mod function. The output should be the variable

extra_day, which should be 1 if the year is a leap year and 0 otherwise.

The rules for determining leap years in the Gregorian calendar are:

1. All years evenly divisible by 400 are leap years.

2. Years evenly divisible by 100 but not by 400 are not leap years.

3. Years divisible by 4 but not by 100 are leap years.

4. All other years are not leap years.

For example, the years 1800, 1900, 2100, 2300, and 2500 are not leap

years, but 2400 is a leap year.

19.. Figure P19a shows a mass-spring model of the type used to design

packaging systems and vehicle suspensions, for example. The springs

exert a force that is proportional to their compression, and the

proportionality constant is the spring constant k. The two side springs

provide additional resistance if the weight W is too heavy for the center

spring. When the weight W is gently placed, it moves througha distance x

before coming to rest. From statics, the weight force must balance the

spring forces at this new position. Thus .

W=k!x

W = k!x + 2k2(x – d)

if x < d

if x 2: d

These relations can be used to generate the plot of W versus x, shown in

Figure P 19b. . •

Q. Create a function file that computes the distance x, using the input

parameters W, klo k2, and d. Test your function for the following two

cases, using the values k, = 104 N/m; k2 = 1Sx 104 N/m; d =0.1 m.

W = SOON

W = 2000N

b. Use your function to plot x versus W for 0 ::: W ::: 3000 N for the

values of k!, k2, andd given in part Q.

Section 4.5

20. The (x , y) coordinates of a certain object as a function of time t are given b .

x(t) = 5t – 10 y(t) = 25tZ – 120t + 144

for 0 =:: t ~ 4. Write a program to determine the time at which the object

is the closest to the origin at (0, 0). Determine also the minimum distance.

Do this two ways:

Q. By using a for loop.

b. By not using a for loop.

21. Consider the array A.

Write a program that computes the array B by computing the natural

logarithm of all the elements of A whose value is no less than 1, and adding

20 to each element that is equal to or greater than 1. Do this two ways:

Q. By using a for loop with conditional statements.

b. By using a logical array as a mask.

·:1-

We want to analyze the mass-spring system discussed in Problem 19 for

the case in which the weight W is dropped onto the’platform attached to

the center spring. If the weight is dropped from a height h above the

platform, we can find the maximum spring compression x by equating

the weight’s gravitational potential energy W(h + x) with the potential

energy stored in the springs. Thus

a. Create a function file that computes the maximum compression x due

-to the falling weight. The function’s input parameters are kJ, ka. d, W

and h. Test your function for the following two cases, using the values

k, = 104 N/m; k2 = 1.5 X 104 N/m; and d = 0.1 m.

W = lOON, h = 0.5 m

W = 2000 N, h = 0.5 m

b. Use your function file to generate a plot of x versus h for 0 ::: h ::: 2 m.

Use W = 100 N and the preceding values for k!, k2, and d..

23. Electrical resistors are said to be connected “in series” if the same current

passes through each and “in parallel” if the same voltage is applied across

each. If in series, they areequivalent to a single resistor whose resistance

is given by

Write an M-file that prompts the user for the type of connection (series or

parallel) and the number of resistors n and then computes the equivalent

resistance.

24. a. An ideal diode blocks the flow of current in the direction opposite that

of the diode’s arrow symbol. It can be used to make a half-wave

rectifier as shown in Figure P24a. For the ideal diode, the voltage VL

across the load RL is given by

where time t is in seconds. Write a MATLAB program to plot the

voltage vL versus t for 0 ::: t ::: 10.

b. A more accurate model of the diode’s behavior is given by the offset

diode model, which accounts for the offset voltage inherent in

semiconductor diodes. The offset model contains an ideal diode

and a battery whose voltage equals the offset voltage (which is

approximately 0.6 V for silicon diodes) [Rizzoni, 1996]. The

half-wave rectifier using this model is shown in Figure P24b. For

this circuit,

Using the same supply voltage given in part a, plot the voltage

VL versus t for 0 ::: t ::: 10; ther compare the results with the plot

obtained in part a.

. 25.* Engineers in industry must continually look for ways to make their

designs and operations more efficient. One tool for doing so is

optimization, which uses ~ mathem~tical description of the design or

operation ~osel~c.tt.hfJ(be~>v~alues of certain va:iables. Many sophisticated

rtathematlcal t~~J.sh:a.Xeb·een developed for this purpose, and some are

in the MATLAB.OP~Wlizationtoolbox. However, problems that have a

limited number of p6ssi.b{~variable values can use MATLAB loop

structures to ~ear{~.,’fo~the optimum solution. This problem and the next

two are examples o~fu~ltivariable optimization that can be done with the

basic MATLAB,program.

A company wants.to locate a distribution center that will serve six of

its major customers in.a 30″x 30 mi area. The locations of the customers

relative to the southwest comer of the area are given in the following

table in terms of (x, y) coordinates (the x direction is east; the y direction

is north) (see Figure ~25).,;Also given is the volume in tons per week that

must be delivered from the distribution center to each customer. The

weekly delivery cost ci’ for.customer i depends on the volume Vi and the

distance d, ,from the distribution center. For simplicity we will assume

that this distance is the straight-line distance. (This assumes that

the road-network is dense.) The weekly cost is given by c, = O.5dj Vj;

i = 1, … , 6. Find the location of the distribution center (to the nearest

mile) that minimizes the total weekly cost to service allsix

customers.

26. A company has the choice of producing up to four different products with

its machinery, which consists of lathes, grinders, and milling machines.

The number of hours on each machine required to produce a product is

given in the following table, along with the number of hours available

per week on each type of machine. Assume that the company can sell

everything it produces. The profit per item for each product appears in the

last line of the table

a. Determine how many units of each product the company should make

to maximize its total profit and then compute this profit. Remember,

the company cannot make fractional units, so your answer must be

in integers. (Hint: First estimate the upper limits on the number of

products that can be produced without exceeding the available

capacity.)

b. How sensitive is your answer? How much does the profit decrease if

you make one more or one less item than the optimum?

27. A certain company makes televisions, stereo units, and speakers. Its parts

inventory includes chassis, picture tubes, speaker cones, power supplies,

and electronics. The inventory, required components, and profit for each

product appear in the following table. Determine how many of each

product to make in order to maximize the profit.

28.* Use a loop in MATLAB to determine how long it will take to accumulate

$1,000,000 in a bank account if you deposit $10,000 initially and $10,000

at the end of each year; the account pays 6 percent annual interest.

29. A weight W is supported by two cables anchored a distance D apart (see

Figure P29). The cable length LAB is given, but the length LAC is to be

selected. Each cable can support a maximum tension force equal to W. For

the weight to remain stationary, the total horizontal force and total vertical

force must each be zero. This principle gives the equations

– TA B cos 0 +TAC cos ¢ = 0

TAB sinO + TAC sin¢ = W

For the given values D = 6 ft, LAB = 3 ft, and W = 2000 lb, use a loop in

MATLAB to find LAC min,the shortest length LAC we can use without TAB

or TAC exceeding 2000 lb. Note that the largest LAC can be is 6.7 ft (which

corresponds to e = 90°). Plot the tension forces TAB and TAc on the same

graph versus LAC for LAC min ::::LAC:::: 6.7.

30. * In the structure in Figure P30a, six wires support three beams. Wires 1 and

2 can support no more than 1200 N each, wires 3 and 4 can support no

more than 400 N each, and wires 5 and 6 no more than 200 N each. Three

equal weights Ware attached at the points shown. Assuming that the

structure is stationary and that the weights of the wires and the beams are

very small compared to W, the principles of statics applied to a particular

beam stare that the sum of vertical forces is zero and that the sum of .

moments about any point is also zero. Applying these principles to each

beam using the free-body diagrams shown in Figure P30b, we obtain the

following equations. Let the tension force in wire i be Ti. For beam 1

For beam 2

T3+T4=W+Ts

– W – 2Ts +3T4 =0

For beam 3

Ts+T6=W

-W+3T6=O

Find the maximum value of the weight W the structure can support.

Remember that.the wires cannot support compression, so T; must be

nonnegative.

31. The equations describing the circuit shown in Figure P31 are:

-VI + Rlid’~ R4i; =–i

-R4i4 + R2i2 + R5i5 = 0

-R5i5 + R3i3+ V2 = 0

il = i2 + i4

i2 = i3 + is

Q. The given values of the resistances and the voltage VI are RI =5,

R2 = 100, R3 = 200, R4 = ISO, R5 = 250kQ, and VI = 100 V.•

(Note that 1 kQ = 1000 Q.) Suppose that each resistance is rated to

carry a current of no more than 1mA (= 0.001 A). Determine the

allowable range of positive values for the voltage V2.

b. Suppose we want to investigate how the resistance R3 limits the

allowable range for V2. Obtain a plot of the allowable limit on V2 as a

function of R3 for 150 ~ R3 ~ 250kQ.

32. Many applications require us to know the temperature distribution in an

object. For example, ~h~in~formation is important for controlling the

material properties; such,’as’hardness, when cooling an object formed from

molten metal. In a heat transferio’urse, the following description of the

temperature distribution in a lfar;I’rettangular metal plate is often derived.

The temperature is held constant at TI on three sides, and at T2 on the

fourth side (see Figure P32). The temperature T(x, y) as a function of the

xy coordinates shown is given by

Use the following data: TI = 70°F, T2 = 200°F, and W = L = 2 ft.

Q. The terms in the preceding series become smaller in magnitude as n

increases. Write a MATLAB program to verify this fact for

n = 1, … , 19 for the center of the plate (x = y = 1).

b. Using x = y = 1, write a MATLAB program to determine how many

terms are required in the series to produce a temperature calculation

that is accurate to within 1 percent. (That is, for what value of n will

the addition of the next term in the series produce a change in T of

less than I percent.) Use your physical insight to determine whether

this answer gives the correct temperature at the center of the plate.

c. Modify the program from part b to compute the temperatures in the

plate; use a spacing of 0.2 for both x .and y.

33. Consider the following script file. Fill in the lines of the following table

with the values that would be displayed immediately after the whi 1e

statement if you ran the script file. Write in the values the variables have

each time the while statement is executed. You might need more or fewer

lines in the table. Then type in the file, and run it to check your answers.

k = l;b = -2;x = -l;y = -2;

while k <= 3

k, b, x , y

y = x”2 – 3;

ify<b,

b = y;

end

x

k

end

x + 1;

k + 1;

34. Assume that the human player makes the first move against the computer

in a game of Tic-Tac-Toe, which has a 3 x 3 grid. Write a MATLAB

function that lets the computer respond to that move. The function’s input

argument should be the cell location of the human player’s move. The

function’s output should be the cell location of the computer’s first move.

Label the ceIls as I, 2, 3 across the top row; 4, 5, 6 across the middle row,

and 7, 8, 9 across the bottom row. ‘

Section 4.6

35. The following table gives the approximate values of the static coefficient

of friction J1- for various materials.

To start a weight W moving on a horizontal surface, you must push with

a force F, where F = J1- W. Write a MATLAB program that uses the

swi tch structure to compute the force F. The program should accept

as input the value of Wand the type of materials.

36. The height and speed of a projectile (such as a thrown balI) launched with

a speed of Vo at an angle A to the horizontal are given by

where g is the acceleration due to gravity. The prpjectile wiIl strike the

ground when h(t) = 0, which gives the time to hit thi! = 2(vo/ g) sin A.

Use the switch structure to write a MATLAB program to compute

either the maximum 111;:;’;> . -‘~chedby the projectile, the total horizontal

distance traveled, or the t;’Tleto hit. The program should accept as input

37.

the user’s choice of which quantity to compute and the values of Vo, A,

and g. Test the program for the case where Vo = 40 mis, A = 30°, ana

g = 9.81 mls2. .

Use the switch structure to write a MATLAB program to compute how

much money accumulates in a savings account in one year. The program

should accept the following input: the initial amount of money deposited

in the account; the frequency of interest compounding (monthly, quarterly, •

.semiannually, or annually); and the interest rate. Run your program for a

$1000 initial deposit for each case; use a 5 percent interest rate. Compare

the amounts of money that accumulate for each case. /

..Engineers often need to estimate the pressures and volumes of j gas in a

container. The van der Waals equation is often used for this purpose. It is

where the term b is a correction for the volume of the molecules, and the

term ajy2 is a correction for molecular attractions. The gas constant is R,

the absolute temperature is T, and the gas specific volume is Y. The value

of R is the same for all gases; it is R =0.08206 L-atm/mol-K. The values

of a and b depend on the type of gas. Some values are given in the

following table. Write a user-defined function using the swi tch structure

that computes the pressure P on the basis of the van der Waals equation.

The function’s input arguments should be T, Y, and a string variable

containing the name of a gas listed in the table. Test your function for

chlorine (Ch) for T = 300 K and Y = 20 Limo\.

39. Using the program developed in Problem 18, write a program that uses the

switch structure to compute the number of days in a year up to a given

date, given the year, the month, and the day of the month.

Section 4.8

40. Consider the college enrollment model discussed in Example 4.8-2.

Suppose the college wants to limit freshmen admissions to 120 percent

of the current sophomore class and limit sophomore transfers to

10 percent of the current freshman class. Rewrite and run the program

given in the example to examine the effects of these policies over a

10-year period. Plot the results

41. Suppose you project that you will be able to deposit the following monthly

amounts into a savings account for a period of five years. The account

initially has no money in it.

At the end of each year in which the account balance is at least $3000,

you withdraw $2000 to buy a certificate of deposit (CD), which pays

6 percent interest compounded annually.

Write a MATLAB program to compute how much money will

accumulate in five years in the account and in any CDs you buy. Run the

program for two ‘different savings interest rates: 4 percent and 5 percent.

42. * A certain company manufactures and sells golf carts. At the end of each

. week, the company transfers the carts produced that week into storage

(inventory). All carts that are sold are taken from the inventory. A simple

model of this process is

I(k + I) = P(k) + I(k) – S(k)

where

P(k) = the number of carts produced in week k

l(k) = the number of carts in inventory in week k

S(k) = the number of carts sold in week k

The projected weekly sales for 10 weeks are

Suppose the weekly production is based on the previous week’s sales so

that P(k) = S(k – I). Assume that the first week’s production is 50 carts;

that is, P(l) = 50. Write a MATLAB program to compute and plot the

number of carts in inventory for each of the 10 weeks or until the

inventory drops below zero. Run the program for two cases: a. an initial

inventory of 50 carts so that 1(1) = 50, and b. an initial inventory of

30 carts so that I (I) = 30.

43. Redo Problem 42 with the restriction that the next week’s production is set

to zero if the inventory exceeds 40 carts.