Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in...

36
Solutions to Problems in Chapter Four Test Your Understanding Problems T4.2-1 The session is x = [5,-3,18,4];y = [-9,13,7,4]; z = ~y>x z= 0100 z = x&y z= 1111 z = x|y z= 1111 z = xor(x,y) z= 0000 T4.2-2 The script file is identical to that used in Example 4.2-1 except for the line u= find(~(h<4|v>17));. v0 = 20;g = 9.81;A = 40*pi/180; t_hit = 2*v0*sin(A)/g; t = [0:t_hit/100:t_hit]; h = v0*t*sin(A)-0.5*g*t.^2; v = sqrt(v0^2-2*v0*g*sin(A)*t+g^2*t.^2); u = find(~(h<4|v>17)); t_1 = (u(1)-1)*(t_hit/100) t_2 = u(length(u)-1)*(t_hit/100) The results are t 1 =0.5766 and t 2 =2.0443. Thus h< 4 or v> 17 for t<t 1 and for t>t 2 . T4.3-1 x = 13; if x>10 y = log(x) if y>= 3 z = 4*y elseif y >2.5 z = 2*y 4-1

Transcript of Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in...

Page 1: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

Solutions to Problems in Chapter Four

Test Your Understanding Problems

T4.2-1 The session is

�x = [5,-3,18,4];y = [-9,13,7,4];�z = ~y>xz =

0 1 0 0�z = x&yz =

1 1 1 1�z = x|yz =

1 1 1 1�z = xor(x,y)z =

0 0 0 0

T4.2-2 The script file is identical to that used in Example 4.2-1 except for the line u =find(~(h<4|v>17));.

v0 = 20;g = 9.81;A = 40*pi/180;t_hit = 2*v0*sin(A)/g;t = [0:t_hit/100:t_hit];h = v0*t*sin(A)-0.5*g*t.^2;v = sqrt(v0^2-2*v0*g*sin(A)*t+g^2*t.^2);u = find(~(h<4|v>17));t_1 = (u(1)-1)*(t_hit/100)t_2 = u(length(u)-1)*(t_hit/100)

The results are t1 = 0.5766 and t2 = 2.0443. Thus h < 4 or v > 17 for t < t1 and for t > t2.

T4.3-1

x = 13;if x>10

y = log(x)if y>= 3

z = 4*yelseif y >2.5

z = 2*y

4-1

Page 2: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

elsez = 0

endelse

y = 5*xz = 7*x

end

T4.3-2 Note that the asin(x) function returns the correct value (in radians) only if x isin the first or fourth quadrant. Note also that it is possible to give incompatible values forq and x. For example, q cannot equal 1 if x < 0. The following script file protects againstthis error.

if x> = 0&x<1if q==1

y = asin(x)*180/pi;elseif q==2

y = asin(x)*180/pi + 270;endif q==3|q==4

disp(′Incompatible values of x and q′)else

disp(y)end

elseif x<0&x>-1if q==4

y = asin(x)*180/pi;else

y = asin(x)*180/pi -90;endif q==1|q==2

disp(′Incompatible values of x and q′)else

disp(y)end

elsedisp(′The magnitude of x is greater than 1′)

end

4-2

Page 3: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

T4.4-1 The script file is

m = 0;for q = 0:6:18

m = m+1;n = 0;for r = 4:4:12

n = n+1;A(m,n) = r+q;

endend

T4.4-2 The script file is

[m,n] = size(A);for c = 1:n

x = 0;for r = 1:m

x = x + A(r,c);end

sum_A(c) = x;enddisp(sum_A)

T4.4-3 The script file is

x = 49;k = 1;while x>0

y = sqrt(x)k = k+1;x = 50 - k^2;

end

T4.4-4 The script file is

error = 0;x = 0;while error < 1

x = x + .01;approx = 1 + x + x^2/2 + x^3/6;error = 100*(exp(x) - approx)/exp(x);

4-3

Page 4: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

enddisp(x)

T4.5-1 The script file is

angle = input(′Enter an angle in degrees.′)switch angle

case 45disp(′Angle is in first quadrant′)

case -45disp(′Angle is in second quadrant′)

case 135disp(′Angle is in third quadrant′)

case -135disp(′Angle is in fourth quadrant′)

otherwisedisp(′Quadrant is unknown.′)

end

T4.7-1 The 0.75 in the matrix should be replaced with 0.70.

T4.7-2 Initial values of a and d are needed in case the statements following the elsestatement are executed (these statements are a(k) = a(k-1), d(k) = d(k-1)).

End-of-Chapter Problems

1. The answers are (a) z = 1, (b) z = 0, (c) z = 1, (d) z = 1

2. The answers are (a) z = 0, (b) z = 1, (c) z = 0,(d) z = 4, (e) z = 1, (f) z = 5,(g) z = 1, (h) z = 0

3. The answers are (a) z = [ 0,1,0,1,1], (b) z = [ 0,0,0,1,1 ],(c) z = [0,0,0,1,0], (d) z = [1,1,1,0,1],

4. The session is

�x = [-3,0,0,2,6,8];�y = [-5,-2,0,3,4,10];�n = find(x>y)n =

1 2 5

Thus the first, second, and fifth elements of x are greater than the corresponding elementsin y.

4-4

Page 5: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

5. The session is

�price = [19,18,22,21,25,19,17,21,27,29];�length(find(price>20))ans =

6

Thus the price was over $20 on six days.

6. The session is

�price_A = [19,18,22,21,25,19,17,21,27,29];�price_B = [22,17,20,19,24,18,16,25,28,27];�length(find(price_A>price_B))ans =

7

7. (a) The session is

�price_A = [19,18,22,21,25,19,17,21,27,29];�price_B = [22,17,20,19,24,18,16,25,28,27];�price_C = [17,13,22,23,19,17,20,21,24,28];�length(find(price_A>price_B&price_A>price_C))ans =

4

Thus the price of stock A was above both B and C on four days.(b) Replace the fourth line in the above session with

�length(find(price_A>price_B|price_A>price_C))ans =

9

The answer is nine days.(c) Replace the fourth line in the session in part (a) with

�length(find(xor(price_A>price_B,price_A>price_C)))ans =

5

The answer is five days.

8. The answers are (a) z = [1,1,1,0,0,0], (b) z = [1,0,0,1,1,1]

4-5

Page 6: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

0 0.5 1 1.5 2 2.5 3 3.5 4 4.50

5

10

15

20

25

30

35

40

Time (sec)

Height (m)

Speed (m/s)

Figure : for Problem 9.

(c) z = [1,1,0,1,1,1], (d) z = [0,1,0,0,0,0]

9. It is helpful to plot the height and speed versus time before answering the questions,especially part (c). The plot is shown in the figure.

The rest of the following script file can be used to compute the answers with moreprecision than can be obtained by reading the plot.

v0 = 40;g = 9.81;A = 30*pi/180;t_hit = 2*v0*sin(A)/g;t = [0:t_hit/100:t_hit];h = v0*t*sin(A)-0.5*g*t.^2;v = sqrt(v0^2-2*v0*g*sin(A)*t+g^2*t.^2);plot(t,h,t,v),xlabel(′Time (sec)′), gtext(′Height′),gtext(′Speed′),grid%% Part (a).ua = find(h>=15);t1a = (ua(1)-1)*(t_hit/100)t2a = ua(length(ua)-1)*(t_hit/100)%% Part (b).ub = find(h>=15&v<=36);

4-6

Page 7: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

t1b = (ub(1)-1)*(t_hit/100)t2b = ub(length(ub)-1)*(t_hit/100)%% Part (c).uc = find(~(h<5|v>35));t1c = (uc(1)-1)*(t_hit/100)t2c = uc(length(uc)-1)*(t_hit/100)

When run, this file produces the results: t1a = 1.0194, t2a = 3.0581, t1b = 1.0601,t2b = 3.0173, t1c = 1.5494, t2c = 2.5280. Thus the height is no less than 15 metersfor 1.0194 ≤ t ≤ 3.0581 seconds. The height is no less than 15 meters and the speed issimultaneously no greater than 36 meters/second for 1.0601 ≤ t ≤ 3.0173 seconds. Theheight is less than 5 meters or the speed is greater than 35 meters/second for 1.5494 ≤ t ≤2.528 seconds.

10. The script file is

price = [19,18,22,21,25,19,17,21,27,29];cost_new_shares = 100*sum(price.*(price<20))income_selling = 100*sum(price.*(price>25))shares_change =100*(sum(price<20)-sum(price>25));total_shares = 1000 + shares_changenet_increase = price(10)*total_shares - price(1)*1000

The results are: cost_new_shares = 7300, income_selling = 5600, total_shares =1200, and net_increase = 15800. Thus you spent $7300 in buying shares. You received$5600 from the sale of shares. After the tenth day you own 1200 shares. The net increasein the worth of your portfolio is $15,800.

11. The script file is

% Part (a).A = (~((x<10)&(x>=6)))==((~(x<10))|(~(x>=6)))% Part (b).B = (~((x==2)|(x>5)))==((~(x==2))&(~(x>5)))

Run this file for various values of x. The answers will be A = 1 and B = 1. This shows thatthe two identities are true.

12. (a) The following script file gives the result A = 0, which indicates false. Thus the twoexpressions are not equivalent.

a = 1; b = 1;c = 3;

4-7

Page 8: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

A = ((a==b)&((b==c)|(a==c)))==((a==b)|((b==c)&(a==c)))

(b) Both expressions are true if max(c, d) < a < b, and false otherwise. Thus bothexpressions are equivalent. This is difficult to prove numerically. The following script fileshould give the result B = 1, which indicates true, for any values of a, b, c, and d. Thisindicates that the two expressions are equivalent.

B = ((a<b)&((a>c)|(a>d)))==((a<b)&(a>c)|((a<b)&(a>d)))

13. The equivalent statements are

if (x<y)&(z<10)w = x*y*z

end

14. (a) The function file is

function package(W,k1,k2,d);if W < k1*d

x = W/k1;else

x = (W+2*k2*d)/(k1+2*k2);enddisp(′The distance travelled is:′)disp(x)

The session is

�package(500,10000,15000,.1)′The distance travelled is:′

0.0500�package(2000,10000,15000,.1)′The distance travelled is:′

0.1250

(b) The function file in part (a) must be modified to provide an output variable x andto eliminate the display statements. It is

function x = package(W,k1,k2,d);if W < k1*d

x = W/k1;

4-8

Page 9: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

elsex = (W+2*k2*d)/(k1+2*k2);

end

Note that the inputs must be scalars because of the if statement. Thus the following scriptfile would not give the correct plot. See the text for a discussion of this pitfall.

W = [0:5:3000];plot(W,package(W,10000,15000,.1)

The correct script file to obtain the plot is shown in the figure.

k1 = 10000;k2 = 15000;d = 0.1;if k1*d <= 3000

W = [0,k1*d,3000];x = [package(W(1),k1,k2,d),package(W(2),k1,k2,d),...package(W(3),k1,k2,d)];plot(W,x,′- ′),...xlabel(′Weight (N)′),ylabel(′Distance (m)′),...title(′k1 = 10,000, k2 = 15,000, d = 0.1′)

elseW = [0,3000];x = [package(W(1),k1,k2,d),package(W(2),k1,k2,d)];plot(W,x,′-′),xlabel(′Weight (N)′),...ylabel(′Distance (m)′),title(′k1 = 10,000, k2 = 15,000, d = 0.1′)

end

The plot is shown in the figure.It is easier to solve this problem using a for loop. The following script file is the solution

using such a loop.

W = linspace(0,3000,500);for k = 1:500

x(k) = package(W(k),10000,15000,.1);endplot(W,x)

15. (a) The function file is

function x = displace(k1,k2,d,W,h)x1 = sqrt(2*W*h/k1);if x1 < d

4-9

Page 10: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

0 500 1000 1500 2000 2500 30000

0.02

0.04

0.06

0.08

0.1

0.12

0.14

0.16

Weight (N)

Dis

tanc

e (m

)

k1 = 10,000, k2 = 15,000, d = 0.1

Figure : for Problem 14.

x = x1;else

p = [k1+2*k2,-4*k2*d,2*k2*d^2-2*W*h];x = max(roots(p));

end

The results are

�displace(10000,15000,.1,100,.5)ans =

0.1000�displace(10000,15000,.1,2000,.5)ans =

0.2944

(b) The script file is

k1 = 10000;k2 = 15000;d = .1;W = 100;for k = 1:201

4-10

Page 11: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

h(k) = (k-1)*.01;x(k) = displace(k1,k2,d,W,h(k));

endplot(h,x),xlabel(′h (meters)′),ylabel(′x (meters)′),...title(′k1=10,000; k2=15,000; W=100; d=0.1′)

The plot is shown in the figure.

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20

0.02

0.04

0.06

0.08

0.1

0.12

0.14

0.16

0.18

h (meters)

x (m

eter

s)

k1=10,000; k2=15,000; W=100; d=0.1

Figure : for Problem 15b.

(c) The script file for the surface mesh plot is

k1 = 10000;k2 = 15000;d = .1;w = [0:25:500];h = [0:.1:2];for k = 1:length(h)

for n = 1:length(w)x(k,n) = displace(k1,k2,d,w(n),h(k));

endend[H,W] = meshgrid(h,w);mesh(H,W,x),xlabel(′h (meters)′),ylabel(′W (newtons)′),...zlabel(′x (meters)′),...title(′Displacement x as a Function of Weight W and Height h′)

4-11

Page 12: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

To obtain the contour plot, replace the last line with

contour(H,W,x),xlabel(′h (meters)′),ylabel(′W (newtons)′),...title(′Displacement x as a Function of Weight W and Height h′)

The plots are shown in the figures.

4-12

Page 13: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

0

0.5

1

1.5

2

0

100

200

300

400

5000

0.05

0.1

0.15

0.2

0.25

0.3

0.35

h (meters)

Displacement x as a Function of Weight W and Height h

W (newtons)

x (m

eter

s)

Figure : for Problem 15c.

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20

50

100

150

h (meters)

W (n

ewto

ns)

Displacement x as a Function of Weight W and Height h

Figure : for Problem 15c.

4-13

Page 14: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

16. The script file is

connection = input(′Enter s for series or p for parallel:′,′s′);if (connection~ = ′s′)&(connection~ = ′p′)

disp(′You have not entered the connection type correctly.′)break

endn = input(′Enter the number of resistors:′);for k = 1:n

Ri(k) = input(′Enter a resistance value:′);enddisp(′The resistances are:′)disp(Ri)if connection==′s′

R = sum(Ri)elseif connection==′p′

invR = 0;for k = 1:n

invR = invR + 1/Ri(k);endR = 1/invR

end

17. (a) The script file is

t = linspace(0,10,300);for k = 1:300

vs = 3*exp(-t(k)/3)*sin(pi*t(k));if vs > 0

vL(k) = vs;else

vL(k) = 0;end

endplot(t,vL),ylabel(′Load Voltage′),xlabel(′Time (sec)′)

The plot is shown in the figure.(b) The script file is

t = linspace(0,10,300);for k = 1:300

vs = 3*exp(-t(k)/3)*sin(pi*t(k));

4-14

Page 15: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

0 1 2 3 4 5 6 7 8 9 100

0.5

1

1.5

2

2.5

3

Load

Vol

tage

Time (sec)

Figure : for Problem 17a.

if vs > 0.6vL(k) = vs-.6;

elsevL(k) = 0;

endendplot(t,vL),ylabel(′Load Voltage′),xlabel(′Time (sec)′)

The plot is shown in the figure.

4-15

Page 16: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

0 1 2 3 4 5 6 7 8 9 100

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

Load

Vol

tage

Time (sec)

Figure : for Problem 17b.

4-16

Page 17: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

18. The script file is shown below. There are two for loops to increment the x and ycoordinates of the plant location. The cost at each location is then computed. The innerfor loop is required to compute the cost for each of the six customers.

The elseif statement and the two statements following it were included to allow forthe possibility of multiple solutions. If a candidate solution gives a cost within $1.00 ofthe current minimum cost, that solution is kept and added to the matrices anotherx andanothery, which will contain the x and y coordinates of all those locations yielding a costwithin $1.00 of each other. The corresponding costs are stored in the vector C.

xloc = [1,7,8,17,22,27];yloc = [28,18,16,2,10,8];V = [3,7,4,5,2,6];best_loc_x = 31;best_loc_y = 31;min_cost = 1e+6;anotherx = [];anothery = [];C = [];for xp = 1:30

for yp = 1:30for k = 1:6

d(k) = sqrt((xloc(k)-xp).^2+(yloc(k)- yp).^2);cost(k) = d(k)*V(k);

endloc_cost(xp,yp) = sum(cost);if loc_cost(xp,yp) < min_cost

best_loc_x = xp;best_loc_y = yp;min_cost = loc_cost(xp,yp);

elseif (loc_cost(xp,yp) - min_cost)<=1anotherx = [anotherx,xp];anothery = [anothery,yp];C =[C,loc_cost(xp,yp)];

endend

endbest_loc_xbest_loc_ymin_cost[m,n] = size(anotherx);if m>1

disp(′The number of answers giving the same cost is:′)disp(m-1)

4-17

Page 18: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

disp(′The other possible answers are:′)disp(′The x coordinates are:′)disp(anotherx(2:m,:))disp(′The y coordinates are:′)disp(anothery(2:m,:))disp(′Other cost:′)disp(C(2:m)′)

elsedisp(′There is only one solution.′)

end

The results are best_loc_x = 9, best_loc_y = 16, and min_cost = 294.5101. There isonly one solution.

19. (a) The following script file uses four nested for loops to vary the number of productsproduced. The vector unit_profit contains the profit numbers in the last row of the table.The matrix hours contains the number of hours for each machine/product combinationgiven in the table. The vector p is the number of units of each product to be produced. It isset to zero initially. The upper limits on the loops were found by examining the table givenin the problem. For example, if we make only Product 1, the available lathe hours allowus to make 40/1 = 40 units, but the available milling hours limit us to 45/3 = 15 units.Thus the maximum number of Product 1 units that can be made is limited by the availablemilling hours. Similarly, if we make only Product 2, we can make no more than 15 units(the limit is due to the available grinder hours). Production of Product 3 is also limitedby the grinder hours, and we can make no more than 30/4 = 7.5, or 8 units. Product 4 islimited by the lathe hours, and we can make no more than 40/3 = 13.3, or 14 units. Thusthe upper limits on the four loops were set to be 15, 15, 8, and 14, respectively.

In the innermost loop, the constraint on available time is checked. If the constraint isviolated, the profit is set to zero to eliminate this set of values from further consideration.If the constraint is not violated, the profit is computed and the result is saved as a possiblesolution candidate. The solution is the one having the maximum profit.

The elseif statement and the two statements following it were included to allow forthe possibility of multiple solutions. If a candidate solution gives a profit within $1.00 ofthe current maximum profit, that solution is kept and added to the matrix another. Thusthe matrix another will contain all those solutions yielding a profit within $1.00 of eachother. The corresponding profits are stored in the vector P.

unit_profit = [100, 150, 90, 120];hours = [1,2,.5,3;0,2,4,1;3,1,5,2];p = [0,0,0,0];max_profit = 0;another = [];

4-18

Page 19: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

P = [];for p1 = 0:15

for p2 = 0:15for p3 = 0:8

for p4 = 0:14p = [p1,p2,p3,p4];if hours*p′ <= [40, 30, 45]′

profit = unit_profit*p′;else

profit = 0;endif profit > max_profit

max_profit = profit;production = [p1,p2,p3,p4];

elseif (max_profit - profit)<=1another = [another;p];P = [P,profit];

endend

endend

enddisp(′Optimal production is:′)disp(production)disp(′The profit is:′)disp(max_profit)[m,n] = size(another);if m>1

disp(′The number of answers giving the same profit is:′)disp(m-1)disp(′The other possible answers are:′)disp(another(2:m,:))disp(′Other profit:′)disp(P(2:m)′)

elsedisp(′There is only one solution.′)

end

The results are that the optimal production is given by the vector [10 15 0 0]. Thus weshould manufacture 10 units of product 1, 15 units of product 2, and no units of products3 and 4. The profit is $3250. There is only one solution.

(b) If we produce 9, 14, 0, and 0 units of each product, the profit would be 9(100) +

4-19

Page 20: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

14(150) = $3000. If we produce 11, 16, 1, and 1 units of each product, the profit wouldbe 11(100) + 16(150) + 1(90) + 1(120) = $3710, but this would require 46.5 hours on thelathe, 37 hours on the grinder, and 56 hours on the milling machine, which is more thanthe available hours.

20. To use an exhaustive search to find the optimum number of televisions, stereos, andspeakers to make, we need to determine the upper limit on the number of possible products.From the table given in the problem, we can tell that the maximum number of televisionswe can make with the given inventory is 250 (assuming we do not make any stereos orspeakers). Similarly, if we make only stereos, we can make no more than 300. If we makeonly speakers, we can make no more than 600. These are the upper limits of the threevariables we must find. If we use an exhaustive search, we must investigate (251)(301)(601)= 45,406,151 cases!

The following script file uses three nested for loops to vary the number of productsproduced. The vector unit_profit contains the profit numbers in the last row of the table.The matrix components contains the number of components required to build each product.The vector p is the number of units of each product to be produced. In the innermost loop,the constraint on available inventory is checked. If the constraint is violated, the profit isset to zero to eliminate this set of values from further consideration. If the constraint isnot violated, the profit is computed and the result is saved in the vector production as apossible solution candidate. The solution is the one having the maximum profit.

The elseif statement and the two statements following it were included to allow forthe possibility of multiple solutions. If a candidate solution gives a profit within $1.00 ofthe current maximum profit, that solution is kept and added to the matrix another. Thusthe matrix another will contain all those solutions yielding a profit within $1.00 of eachother. The corresponding profits are stored in the vector P.

unit_profit = [80, 50, 40];components = [1,1,0;1,0,0;2,2,1;1,1,0;2,2,1];max_profit = 0;another = [];P = [];p1lower = 0;p1inc = 5;p1upper = 250;p2lower = 0;p2inc = 5;p2upper = 300;p3lower = 0;p3inc = 5;p3upper = 600;for p1 = p1lower:p1inc:p1upper

for p2 = p2lower:p2inc:p2upperfor p3 = p3lower:p3inc:p3upper

p = [p1,p2,p3];if components*p′ <= [450, 250, 800, 450, 600]′

profit = unit_profit*p′;else

profit = 0;

4-20

Page 21: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

endif profit > max_profit

max_profit = profit;production = [p1,p2,p3];

elseif (max_profit - profit)<=1another = [another;p];P = [P,profit];

endend

endenddisp(′Optimal production is:′)disp(production)disp(′The profit is:′)disp(max_profit)[m,n] = size(another);if m>1

disp(′The number of answers giving the same profit is:′)disp(m-1)disp(′The other possible answers are:′)disp(another(2:m,:))disp(′Other profit:′)disp(P(2:m)′)

elsedisp(′There is only one solution.′)

end

To use a truly exhaustive search, you must investigate (251)(301)(601) = 45,406,151 cases!If you use the above program to do this, it will take several hours on a reasonably fastpersonal computer! Therefore, I used increments of 5 for the first run. This required that(51)(61)(121) = 376,431 cases be checked. The results showed that numerous solutionsexisted, all requiring that no stereos be produced. The profit was $24,000.

In light of these results, I then fixed p2 equal to 0 by setting p2lower = p2upper = 0and p2inc = 1, and varied p1 from 0 to 250 and p3 from 0 to 600, using an increment of 1.The results showed that 250 solutions exist. These solutions have a profit of $24,000. Thevalues for p1 are all the integers from 1 to 250. The values for p3 are all the integers from100 to 599. All the solutions satisfy the following relation: p1 + p3 = n where n is everyinteger from 350 to 599. Thus p1 = 1, p3 = 598 is the first solution, and p1 = 250, p3 = 100is the last solution.

These solutions might not be the optimal because I did not search all 45,406,151 cases.In contrast, the solution obtained from the Solver tool in the Excel spreadsheet programis p1 = 250, p2 = 0, p3 = 100. Excel found this solution very quickly, but Excel did

4-21

Page 22: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

not indicate that other solutions existed! The advantage of programming the problem inMatlab is that you are forced to consider the structure of the problem and are led toconsider the possibility of multiple solutions.

It would be nice to take advantage of Matlab’s vector operations to speed up thisprogram. If you develop such a solution, let me know!

21. The script file is

amt = 10000;k = 0;while amt < 1e+6

k = k+1;amt = amt*1.06 +10000;

endamtk

The result is amt = 1.0418e+006 and k = 33. Thus, after 33 years, the amount will be$1,041,800.

22. We can easily solve the two equations for TAC and TAB as follows:

TAC =W cos(θ)

cos(φ) sin(θ) + sin(φ) cos(θ)

TAB = TACcos(φ)cos(θ)

Note that the triangle does not exist if LAC = 3 (this situation corresponds to θ = φ = 0).Thus we use a loop with LAC going from 6.7 to 3.01. Note that because we are trying tofind the minimum value of LAC , we must decrease LAC in the loop. The second part of theprogram produces the plot. Note that we must use array operations in this part.

D = 6;LAB = 3;W = 2000;for LAC = 6.7:-.001:3.01

theta = acos((D^2+LAB^2- LAC^2)/(2*D*LAB));phi = asin(LAB*sin(theta)/LAC);TAC = W*cos(theta)/(cos(phi)*sin(theta)+sin(phi)*cos(theta));TAB = TAC*cos(phi)/cos(theta);if (TAB<=2000)&(TAC<=2000)

TAB_ans = TAB;TAC_ans = TAC;LAC_ans = LAC;

endend

4-22

Page 23: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

disp(′TAB is:′)disp(TAB_ans)disp(′TAC is:′)disp(TAC_ans)disp(′LAC is:′)disp(LAC_ans)%%Begin plot calculations.LAC = [LAC_ans:.01:6.7];theta = acos((D^2+LAB^2- LAC.^2)/(2*D*LAB));phi = asin(LAB*sin(theta)./LAC);TAC = W*cos(theta)./(cos(phi).*sin(theta)+sin(phi).*cos(theta));TAB = TAC.*cos(phi)./cos(theta);plot(LAC,TAC,LAC,TAB,′-- ′),xlabel(′Length LAC (ft)′),...ylabel(′TAB and TAC (lbs)′),legend(′TAC′,′TAB′)

The results are: TAB = 2000, TAC = 1788.8 lbs, and LAC = 4.0250 ft. The plot is shownin the figure.

TACTAB

4 4.5 5 5.5 6 6.5 70

200

400

600

800

1000

1200

1400

1600

1800

2000

Length LAC (ft)

TA

B a

nd T

AC

(lb

s)

Figure : for Problem 22.

4-23

Page 24: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

23. The script file using a for loop to solve the problem is

A = [1,1,-1,-1,0,-1;0,7/5,-1/5,-4/5,0,-6/5;0,0,1,1,-1,0;0,0,0,3,-2,0;0,0,0,0,1,1;0,0,0,0,0,3];for w = 20:400

b = [w,w,w,w,w,w]′;T = A\b;if T <= 4*[300;300;100;100;50;50]

acceptable(w) = w;end

endw = max(acceptable)b = [w,w,w,w,w,w]′;T = A\b

The results are w = 300 and

T = [428.5714, 471.4286, 266.6667, 233.3333, 200.0000, 100.0000]

24. (a) We can easily reduce the number of unknowns from five to three by using the lasttwo equations to substitute for i4 and i5. The equations become

(R1 + R4)i1 − R4i2 = v1

−R4i1 + (R2 + R4 + R5)i2 − R5i3 = 0

R5i2 − (R3 + R5)i3 = v2

and i4 = i2 − i1, i5 = i2 − i3. The script file is shown below. The vector c123 contains thecurrents i1, i2, and i3. The vector current contains all five currents.

R = [5,100,200,150,250]*1000;A1 = [R(1)+R(4),-R(4),0];A2 = [-R(4),R(2)+R(4)+R(5),-R(5)];A3 = [0,R(5),-(R(3)+R(5))];A = [A1;A2;A3];current = [0;0;0;0;0];v1 = 100;v2 =100;acceptable = 0;while abs(current) <= [1;1;1;1;1]./1000

b = [v1;0;v2];c123 = A\b;current = [c123;c123(1)-c123(2);c123(2)-c123(3)];

4-24

Page 25: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

if abs(current) <= [1;1;1;1;1]./1000acceptable = v2;

endv2 = v2 - .01;if v2<0

breakend

endv2 = acceptableb = [v1;0;v2];c123 = A\b;current = [c123;c123(1)-c123(2);c123(2)-c123(3)]

The results are v2 = 31.67 volts and current = [1, 0.3667, 0.1333, 0.6333, 0.2333]milliamperes. Thus 31.67 volts is the maximum allowable value for v2. When this voltageis applied, the current i1 will be at the maximum allowable value of 1 milliampere, and theother currents will be below that limit. The decrement of 0.01 volts was chosen arbitrarily.If we use a larger decrement, say 1, the answer is less accurate (v2 = 32 volts and i1 = 0.9992milliamperes).

(b) The required program is similar to that used in part (a) but it uses a loop toincrement the value of the resistance R3.

k = 0;for R3 = [150:250]k = k+1;R = [5,100,R3,150,250]*1000;A1 = [R(1)+R(4),-R(4),0];A2 = [-R(4),R(2)+R(4)+R(5),-R(5)];A3 = [0,R(5),-(R(3)+R(5))];A = [A1;A2;A3];current = [0;0;0;0;0];v1 = 100;v2 = 100;acceptable = 0;while abs(current) <= [1;1;1;1;1]./1000

b = [v1;0;v2];c123 = A\b;current = [c123;c123(1)-c123(2);c123(2)-c123(3)];if abs(current) <= [1;1;1;1;1]./1000

acceptable = v2;R3acceptable = R3;

end

4-25

Page 26: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

v2 = v2 - .1;if v2<0

breakend

endv2acc(k) = acceptable;R3acc(k) = R3acceptable;c123 = A\b;endplot(R3acc,v2acc),ylabel(′v2′),xlabel(′R3 ′),grid

The plot is shown in the figure.

150 160 170 180 190 200 210 220 230 240 25025

30

35

40

v2 (v

olts

)

R3 (ohms)

Figure : for Problem 24.

25. (a) We do not include the term 2/π because it appears in every term, and thus doesnot affect the relative magnitudes of the terms. Set x = y = 1 and W = L = 2 in the series.The script file is

n = [1,3,5,7,9,11,13,15,17,19];term = abs((2./n).*sin(n*pi/2).*(sinh(n*pi/2)./sinh(n*pi)));format short e

4-26

Page 27: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

disp(term′/term(1))

The results showing the ratios of the magnitudes of each term relative to that of the firstterm are

1.0000e+0001.5026e-0023.8963e-0041.2027e-0054.0423e-0071.4292e-0085.2260e-0101.9573e-0117.4630e-0132.8856e-014

Thus the magnitude of the second term is 1.5026 × 10−2 times smaller than the first term,and so on.

(b) The script file is

T_1 = 70;T_2 = 200;W = 2;L = 2;x = L/2;y = W/2;error = 100;term = 0;n = 1;m = 1;while abs(error) > .01

term_next = term + (2/n)*sin(n*pi*x/L)*(sinh(n*pi*y/L)/sinh(n*pi*W/L));if n>1

error = ((abs(term_next)- abs(term))/abs(term))*100;endterm = term_next;n = n+2;end

theta = (2/pi)*term_next;T_middle = (T_2- T_1)*theta+T_1;disp(′The temperature in the midddle of the plate is:′)disp(T_middle)disp(′The number of series terms required is:′)disp(n)

4-27

Page 28: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

The results are T_middle = 102.5 and n = 9. Thus it took nine terms in the series tocompute the middle temperature to an accuracy of 1%. The middle temperature is 102.5◦

F. This can be checked by computing the average of the temperatures on the four sides ofthe plate: (70 + 70 + 70 + 200)/4 = 102.5.

(c) The program shown below uses the program from part (b) within two for loops:one loop over x, and one loop over y.

T_1 = 70;T_2 = 200;W = 2;L = 2;i = 0;for x = 0:.2:2

i = i+1;j = 0;for y = 0:.2:2

j = j+1;error = 100;term = 10*eps;n = 1;m = 1;while abs(error) > .01

term_next = term +(2/n)*sin(n*pi*x/L)*(sinh(n*pi*y/L)/sinh(n*pi*W/L));if abs(error) ~ = 0

error = ((abs(term_next)- abs(term))/abs(term))*100;endterm = term_next;n = n+2;

endtheta = (2/pi)*term_next;T(j,i) = (T_2-T_1)*theta+T_1;

endend

(d) To obtain the plots, insert the following lines, one at a time, at the end of theprogram given in part (c).

contour([0:.2:2],[0:.2:2],T),xlabel(′x′),ylabel(′y′)

surface([0:.2:2],[0:.2:2],T),xlabel(′x′),ylabel(′y′),zlabel(′Temp (deg F)′)

4-28

Page 29: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

x

y

Figure : for Problem 25d.

mesh([0:.2:2],[0:.2:2],T),xlabel(′x′),ylabel(′y′), zlabel(′Temp (deg F)′)

The plots are shown in the figures.

4-29

Page 30: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

00.5

11.5

2

0

0.5

1

1.5

250

100

150

200

250

xy

Tem

p (d

eg F

)

Figure : for Problem 25d.

00.5

11.5

2

0

0.5

1

1.5

250

100

150

200

250

xy

Tem

p (d

eg F

)

Figure : for Problem 25d.

4-30

Page 31: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

26. The results are:

Pass k b x y1 1 -2 -1 -22 2 -2 0 -23 3 -3 1 -3

27. The function file is:

function move = tictac(n)% First computer move in tic-tac-toe.% Go for the center if empty.% Otherwise choose the upper left corner.if n = 5

move = 5;else

move = 1;end

28. The script file is

W = input(′Enter the weight: ′);materials = input(′Enter the materials: ′,′s′);switch materialscase ′metal on metal′

F = .2*Wcase ′wood on wood′

F = .35*Wcase ′metal on wood′

F = .4*Wcase ′rubber on concrete′

F = .7*Wotherwise

disp(′Improper material choice.′)end

29. The script file is

v0 = input(′Enter the initial speed: ′);A = input(′Enter the angle (degrees): ′);g = input(′Enter the acceleration due to gravity: ′);disp(′Enter the quantity to be computed:′)quantity = input(′(height, distance, or time):′,′s′);

4-31

Page 32: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

A_rad = A*pi/180;t_hit = 2*(v0/g)*sin(A_rad);t_max = t_hit/2;switch quantitycase ′height′

height = v0*t_max*sin(A_rad)- .5*g*t_max^2case ′distance′

distance = v0*t_hit*cos(A_rad)case ′time′

time_to_hit = t_hitotherwise

disp(′Improper choice.′)end

30. The script file is

deposit = input(′Enter the initial deposit: ′);frequency = input(′Enter the frequency(monthly, quarterly, semiannually, annually): ′,′s′);rate = input(′Enter the interest rate as a percent: ′);rate = rate/100;switch frequencycase ′monthly′

amount = deposit*(1+rate/12)^12case ′quarterly′

amount = deposit*(1+rate/4)^4case ′semiannually′

amount = deposit*(1+rate/2)^2case ′annually′

amount = deposit*(1+rate)otherwise

disp(′Improper frequency choice.′)end

31. The function file is:

function P = waals(T,Vhat,gas)R = 0.08206;go = 1;switch gascase ′helium′

a = 0.0341;b = 0.0237;

4-32

Page 33: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

case ′hydrogen′

a = 0.244;b = 0.0266;case ′oxygen′

a = 1.36;b = 0.0318;case ′chlorine′

a = 6.49;b = 0.0562;case ′carbon dioxide′

a = 3.59;b = 0.0427;otherwise

disp(′The table does not contain this gas.′)disp(′Check the spelling; all gas names must be lowercase.′)go = 0;

endif go == 1

P = R*T/(Vhat-b)-a/Vhat^2;end

32. The script file is almost identical to that given in Table 4.7-2. The only changes arethe use of the min command in four statements. The file is given below.

C = [.1,0,0,0;.75,.05,0,0;0,.9,.05,0;0,0,.9,.05];x = [500;400;300;280];a(1) = 1000;d(1) = 200;E(:,1) = x;for k = 2:10

if sum(x) <= 4000a(k) = min(900+100*k,1.2*x(2));d(k) = min(150+50*k,.1*x(1));

elsea(k) = min(a(k-1),1.2*x(2));d(k) = min(d(k-1),.1*x(1));

endb = [a(k);d(k);0;0];x = C*x+b;E(:,k) = x;

endfrosh = E(1,:);soph = E(2,:);jr = E(3,:);sr = E(4,:);plot(E′),hold,plot(E(1,:),′o′),plot(E(2,: ),′+′),plot(E(3,:),′*′),...plot(E(4,:),′x′),xlabel(′Year′), ylabel(′Number of Students′),...gtext(′Frosh′),gtext(′Soph′), gtext(′Jr′),gtext(′Sr′),...title(′Enrollment As A Function of Time′)

4-33

Page 34: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

The plot is shown in the figure.

1 2 3 4 5 6 7 8 9 10200

300

400

500

600

700

800

900

1000

1100

Year

Num

ber o

f Stu

dent

s

Enrollment As A Function of Time

Frosh

SophJr

Sr

Figure : for Problem 32.

33. Assuming that the interest is applied monthly, and that the desposit is made on thefirst of each month, the script file is

deposit = [300*ones(1,12),350*ones(1,12),350*ones(1,12),...350*ones(1,12),400*ones(1,12)];amount(1) = deposit(1);rate = .04/12;m = 0;n = 0;CD = [];for k = 1:60

if m==12m = 1;CD = 1.06*CD;if amount(k) >= 3000

amount(k) = amount(k) - 2000;CD(n+1) = 2000;n = n+1;

4-34

Page 35: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

endendamount(k+1) = amount(k)*(1+rate) + deposit(k);m = m+1;

enddisp(′The amount in savings is:′)disp(amount(60))disp(′The number of CDs is:′)disp(length(CD))disp(′The amount in CDs is:′)disp(sum(CD))disp(′The total amount you have is:′)disp(amount(60)+sum(CD))

The output for a 4% interest rate is

The amount in savings is:1.2106e+004

The number of CDs is:5

The amount in CDs is:1.1274e+004

The total amount you have is:2.3381e+004

To obtain the results for a 5% rate, change the second line to rate = .05/12;. The outputis

The amount in savings is:1.2435e+004

The number of CDs is:5

The amount in CDs is:1.1274e+004

The total amount you have is:2.3709e+004

34. The script file using a for loop to solve the problem for part (a) is shown below. Thesales are stored in the vector S, the number produced are in the vector P, and the inventoryis given by the vector I.

S = [50,55,60,70,70,75,80,80,90,55];

4-35

Page 36: Solutions to Problems in Chapter Four courses/matlab/INSTRUCTOR... · Solutions to Problems in Chapter Four ... Thus h17 for tt2. T4.3-1 ... Thefollowing

I(1) = 50;P(1) = 50;for k = 1:9

I(k+1) = I(k)+P(k)-S(k);P(k+1) = S(k);

endI

The sales are stored in the vector S, the number produced are in the vector P, and theinventory is given by the vector I. The results are I = [50, 50, 45, 40, 30, 30, 25,20, 20, 10].

For part (b) change the value of I(1) to 30. The results are

I = [30, 30, 25, 20, 10, 10, 5, 0, 0, -10]

The inventory becomes nonpositive in the tenth week.

35. The script file using a while loop to solve the problem for part (a) is shown below. Thesales are stored in the vector S, the number produced are in the vector P, and the inventoryis given by the vector I.

S = [50,55,60,70,70,75,80,80,90,55];I(1) = 50;P(1) = 50;k = 0;while I>= 0

k = k+1;I(k+1) = I(k)+P(k)-S(k);if I(k+1)>40

P(k+1) = 0;else

P(k+1) = S(k);end

enddisp(I)

The results are I = [50, 50, -5]. Thus the inventory remains at 50 for the first twoweeks and then becomes nonpositive, so the program stops. To solve the problem for part(b), change I(1) to 30. The results are

I = [30, 30, 25, 20, 10, 10, 5, 0, 0, -10]

Thus the inventory becomes nonpositive in the tenth week, so the program stops.

4-36