You are on page 1of 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 = 0 1 0 0 z = x&y = 1 1 1 1 z = x|y = 1 1 1 1 z = xor(x,y) = 0 0 0 0

T4.2-2 The script le 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*y elseif y >2.5 z = 2*y 4-1

else z = 0 end else y = 5*x z = 7*x end T4.3-2 Note that the asin(x) function returns the correct value (in radians) only if x is in the rst or fourth quadrant. Note also that it is possible to give incompatible values for q and x. For example, q cannot equal 1 if x < 0. The following script le protects against this error. if x> = 0&x<1 if q==1 y = asin(x)*180/pi; elseif q==2 y = asin(x)*180/pi + 270; end if q==3|q==4 disp( Incompatible values of x and q ) else disp(y) end elseif x<0&x>-1 if q==4 y = asin(x)*180/pi; else y = asin(x)*180/pi -90; end if q==1|q==2 disp( Incompatible values of x and q ) else disp(y) end else disp( The magnitude of x is greater than 1 ) end

4-2

T4.4-1 The script le 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; end end T4.4-2 The script le 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; end disp(sum_A) T4.4-3 The script le is x = 49; k = 1; while x>0 y = sqrt(x) k = k+1; x = 50 - k^2; end T4.4-4 The script le 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

end disp(x) T4.5-1 The script le is angle = input( Enter switch angle case 45 disp( Angle is case -45 disp( Angle is case 135 disp( Angle is case -135 disp( Angle is otherwise disp( Quadrant end an angle in degrees. )

in first quadrant ) in second quadrant ) in third quadrant ) in fourth quadrant ) is unknown. )

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 else statement 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 rst, second, and fth elements of x are greater than the corresponding elements in y. 4-4

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 ve days. 8. The answers are (a) z = [1,1,1,0,0,0], (b) z = [1,0,0,1,1,1] 4-5

40

35 Speed (m/s) 30

25 Height (m) 20

15

10

0.5

1.5

2 2.5 Time (sec)

3.5

4.5

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 gure. The rest of the following script le can be used to compute the answers with more precision 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

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 le 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 meters for 1.0194 t 3.0581 seconds. The height is no less than 15 meters and the speed is simultaneously no greater than 36 meters/second for 1.0601 t 3.0173 seconds. The height is less than 5 meters or the speed is greater than 35 meters/second for 1.5494 t 2.528 seconds. 10. The script le 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_change net_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 increase in the worth of your portfolio is $15,800. 11. The script le is % A % B Part (a). = (~((x<10)&(x>=6)))==((~(x<10))|(~(x>=6))) Part (b). = (~((x==2)|(x>5)))==((~(x==2))&(~(x>5)))

Run this le for various values of x. The answers will be A = 1 and B = 1. This shows that the two identities are true. 12. (a) The following script le gives the result A = 0, which indicates false. Thus the two expressions are not equivalent. a = 1; b = 1;c = 3; 4-7

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 both expressions are equivalent. This is dicult to prove numerically. The following script le should give the result B = 1, which indicates true, for any values of a, b, c, and d. This indicates 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 le is function package(W,k1,k2,d); if W < k1*d x = W/k1; else x = (W+2*k2*d)/(k1+2*k2); end disp( 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 le in part (a) must be modied to provide an output variable x and to eliminate the display statements. It is function x = package(W,k1,k2,d); if W < k1*d x = W/k1; 4-8

else x = (W+2*k2*d)/(k1+2*k2); end Note that the inputs must be scalars because of the if statement. Thus the following script le 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 le to obtain the plot is shown in the gure. 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 ) else W = [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 gure. It is easier to solve this problem using a for loop. The following script le is the solution using such a loop. W = linspace(0,3000,500); for k = 1:500 x(k) = package(W(k),10000,15000,.1); end plot(W,x) 15. (a) The function le is function x = displace(k1,k2,d,W,h) x1 = sqrt(2*W*h/k1); if x1 < d 4-9

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

0.14

0.12

0.1

Distance (m)

0.08

0.06

0.04

0.02

500

1000

1500 Weight (N)

2000

2500

3000

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 le is k1 = 10000;k2 = 15000;d = .1;W = 100; for k = 1:201 4-10

h(k) = (k-1)*.01; x(k) = displace(k1,k2,d,W,h(k)); end plot(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 gure.
k1=10,000; k2=15,000; W=100; d=0.1 0.18 0.16 0.14 0.12

x (meters)

0.1 0.08 0.06 0.04 0.02 0 0

0.2

0.4

0.6

0.8

1 1.2 h (meters)

1.4

1.6

1.8

Figure : for Problem 15b. (c) The script le 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)); end end [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

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

4-12

Displacement x as a Function of Weight W and Height h

0.35 0.3 0.25

x (meters)

0.2 0.15 0.1 0.05 0 500 400 300 200 100 W (newtons) 0 0 0.5 h (meters) 1 1.5 2

Figure : for Problem 15c.

Displacement x as a Function of Weight W and Height h

W (newtons)
150 100 50 0 0

0.2

0.4

0.6

0.8

1 h (meters)

1.2

1.4

1.6

1.8

Figure : for Problem 15c.

4-13

16. The script le 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 end n = input( Enter the number of resistors: ); for k = 1:n Ri(k) = input( Enter a resistance value: ); end disp( 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); end R = 1/invR end 17. (a) The script le 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 end plot(t,vL),ylabel( Load Voltage ),xlabel( Time (sec) ) The plot is shown in the gure. (b) The script le is t = linspace(0,10,300); for k = 1:300 vs = 3*exp(-t(k)/3)*sin(pi*t(k)); 4-14

2.5

Load Voltage

1.5

0.5

0 0

5 Time (sec)

10

Figure : for Problem 17a. if vs > 0.6 vL(k) = vs-.6; else vL(k) = 0; end end plot(t,vL),ylabel( Load Voltage ),xlabel( Time (sec) ) The plot is shown in the gure.

4-15

2 1.8 1.6 1.4

Load Voltage

1.2 1 0.8 0.6 0.4 0.2 0 0

5 Time (sec)

10

Figure : for Problem 17b.

4-16

18. The script le is shown below. There are two for loops to increment the x and y coordinates of the plant location. The cost at each location is then computed. The inner for 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 for the possibility of multiple solutions. If a candidate solution gives a cost within $1.00 of the current minimum cost, that solution is kept and added to the matrices anotherx and anothery, which will contain the x and y coordinates of all those locations yielding a cost within $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:30 for k = 1:6 d(k) = sqrt((xloc(k)-xp).^2+(yloc(k)- yp).^2); cost(k) = d(k)*V(k); end loc_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)<=1 anotherx = [anotherx,xp]; anothery = [anothery,yp]; C =[C,loc_cost(xp,yp)]; end end end best_loc_x best_loc_y min_cost [m,n] = size(anotherx); if m>1 disp( The number of answers giving the same cost is: ) disp(m-1) 4-17

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) ) else disp( There is only one solution. ) end

The results are best_loc_x = 9, best_loc_y = 16, and min_cost = 294.5101. There is only one solution. 19. (a) The following script le uses four nested for loops to vary the number of products produced. The vector unit_profit contains the prot numbers in the last row of the table. The matrix hours contains the number of hours for each machine/product combination given in the table. The vector p is the number of units of each product to be produced. It is set to zero initially. The upper limits on the loops were found by examining the table given in the problem. For example, if we make only Product 1, the available lathe hours allow us 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 available milling 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 limited by the grinder hours, and we can make no more than 30/4 = 7.5, or 8 units. Product 4 is limited by the lathe hours, and we can make no more than 40/3 = 13.3, or 14 units. Thus the 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 is violated, the prot is set to zero to eliminate this set of values from further consideration. If the constraint is not violated, the prot is computed and the result is saved as a possible solution candidate. The solution is the one having the maximum prot. The elseif statement and the two statements following it were included to allow for the possibility of multiple solutions. If a candidate solution gives a prot within $1.00 of the current maximum prot, that solution is kept and added to the matrix another. Thus the matrix another will contain all those solutions yielding a prot within $1.00 of each other. The corresponding prots 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

P = []; for p1 = 0:15 for p2 = 0:15 for p3 = 0:8 for p4 = 0:14 p = [p1,p2,p3,p4]; if hours*p <= [40, 30, 45] profit = unit_profit*p ; else profit = 0; end if profit > max_profit max_profit = profit; production = [p1,p2,p3,p4]; elseif (max_profit - profit)<=1 another = [another;p]; P = [P,profit]; end end end end end disp( 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) ) else disp( There is only one solution. ) end The results are that the optimal production is given by the vector [10 15 0 0]. Thus we should manufacture 10 units of product 1, 15 units of product 2, and no units of products 3 and 4. The prot is $3250. There is only one solution. (b) If we produce 9, 14, 0, and 0 units of each product, the prot would be 9(100) + 4-19

14(150) = $3000. If we produce 11, 16, 1, and 1 units of each product, the prot would be 11(100) + 16(150) + 1(90) + 1(120) = $3710, but this would require 46.5 hours on the lathe, 37 hours on the grinder, and 56 hours on the milling machine, which is more than the available hours. 20. To use an exhaustive search to nd the optimum number of televisions, stereos, and speakers 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 televisions we can make with the given inventory is 250 (assuming we do not make any stereos or speakers). Similarly, if we make only stereos, we can make no more than 300. If we make only speakers, we can make no more than 600. These are the upper limits of the three variables we must nd. If we use an exhaustive search, we must investigate (251)(301)(601) = 45,406,151 cases! The following script le uses three nested for loops to vary the number of products produced. The vector unit_profit contains the prot 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 prot is set to zero to eliminate this set of values from further consideration. If the constraint is not violated, the prot is computed and the result is saved in the vector production as a possible solution candidate. The solution is the one having the maximum prot. The elseif statement and the two statements following it were included to allow for the possibility of multiple solutions. If a candidate solution gives a prot within $1.00 of the current maximum prot, that solution is kept and added to the matrix another. Thus the matrix another will contain all those solutions yielding a prot within $1.00 of each other. The corresponding prots 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:p2upper for 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

end if profit > max_profit max_profit = profit; production = [p1,p2,p3]; elseif (max_profit - profit)<=1 another = [another;p]; P = [P,profit]; end end end end disp( 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) ) else disp( 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 fast personal computer! Therefore, I used increments of 5 for the rst run. This required that (51)(61)(121) = 376,431 cases be checked. The results showed that numerous solutions existed, all requiring that no stereos be produced. The prot was $24,000. In light of these results, I then xed p2 equal to 0 by setting p2lower = p2upper = 0 and 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 prot of $24,000. The values for p1 are all the integers from 1 to 250. The values for p3 are all the integers from 100 to 599. All the solutions satisfy the following relation: p1 + p3 = n where n is every integer from 350 to 599. Thus p1 = 1, p3 = 598 is the rst solution, and p1 = 250, p3 = 100 is 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 program is p1 = 250, p2 = 0, p3 = 100. Excel found this solution very quickly, but Excel did 4-21

not indicate that other solutions existed! The advantage of programming the problem in Matlab is that you are forced to consider the structure of the problem and are led to consider the possibility of multiple solutions. It would be nice to take advantage of Matlabs vector operations to speed up this program. If you develop such a solution, let me know! 21. The script le is amt = 10000; k = 0; while amt < 1e+6 k = k+1; amt = amt*1.06 +10000; end amt k 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 = TAC cos() 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 to nd the minimum value of LAC , we must decrease LAC in the loop. The second part of the program 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; end end 4-22

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 shown in the gure.
2000 1800 1600 1400

TAB and TAC (lbs)

1200 1000 800 600 400 200 0 4

TAC TAB

4.5

5.5 Length LAC (ft)

6.5

Figure : for Problem 22.

4-23

23. The script le 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 end w = 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 ve to three by using the last two equations to substitute for i4 and i5 . The equations become (R1 + R4 )i1 R4 i2 = v1 R4 i1 + (R2 + R4 + R5 )i2 R5 i3 = 0 R5 i2 (R3 + R5 )i3 = v2 and i4 = i2 i1 , i5 = i2 i3 . The script le is shown below. The vector c123 contains the currents i1 , i2 , and i3 . The vector current contains all ve 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

if abs(current) <= [1;1;1;1;1]./1000 acceptable = v2; end v2 = v2 - .01; if v2<0 break end end v2 = acceptable b = [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 voltage is applied, the current i1 will be at the maximum allowable value of 1 milliampere, and the other 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.9992 milliamperes). (b) The required program is similar to that used in part (a) but it uses a loop to increment 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

v2 = v2 - .1; if v2<0 break end end v2acc(k) = acceptable; R3acc(k) = R3acceptable; c123 = A\b; end plot(R3acc,v2acc),ylabel( v2 ),xlabel( R3 ),grid The plot is shown in the gure.
40

35

v2 (volts)
30 25 150

160

170

180

190

200 R3 (ohms)

210

220

230

240

250

Figure : for Problem 24. 25. (a) We do not include the term 2/ because it appears in every term, and thus does not aect the relative magnitudes of the terms. Set x = y = 1 and W = L = 2 in the series. The script le 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

disp(term /term(1)) The results showing the ratios of the magnitudes of each term relative to that of the rst term are 1.0000e+000 1.5026e-002 3.8963e-004 1.2027e-005 4.0423e-007 1.4292e-008 5.2260e-010 1.9573e-011 7.4630e-013 2.8856e-014 Thus the magnitude of the second term is 1.5026 102 times smaller than the rst term, and so on. (b) The script le 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; end term = 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

The results are T_middle = 102.5 and n = 9. Thus it took nine terms in the series to compute 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 of the 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; end term = term_next; n = n+2; end theta = (2/pi)*term_next; T(j,i) = (T_2-T_1)*theta+T_1; end end

(d) To obtain the plots, insert the following lines, one at a time, at the end of the program 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

2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 0

0.2

0.4

0.6

0.8

1 x

1.2

1.4

1.6

1.8

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

4-29

250

200

Temp (deg F)

150

100

50 2 1.5 1 0.5 y 0 0 1 0.5 x 1.5 2

Figure : for Problem 25d.

250

200

Temp (deg F)

150

100

50 2 1.5 1 0.5 y 0 0 1 0.5 x 1.5 2

Figure : for Problem 25d.

4-30

26. The results are: Pass 1 2 3 k 1 2 3 b -2 -2 -3 x -1 0 1 y -2 -2 -3

27. The function le 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 le is W = input( Enter the weight: ); materials = input( Enter the materials: switch materials case metal on metal F = .2*W case wood on wood F = .35*W case metal on wood F = .4*W case rubber on concrete F = .7*W otherwise disp( Improper material choice. ) end 29. The script le 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 , s );

A_rad = A*pi/180; t_hit = 2*(v0/g)*sin(A_rad); t_max = t_hit/2; switch quantity case height height = v0*t_max*sin(A_rad)- .5*g*t_max^2 case distance distance = v0*t_hit*cos(A_rad) case time time_to_hit = t_hit otherwise disp( Improper choice. ) end 30. The script le 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 frequency case monthly amount = deposit*(1+rate/12)^12 case quarterly amount = deposit*(1+rate/4)^4 case semiannually amount = deposit*(1+rate/2)^2 case annually amount = deposit*(1+rate) otherwise disp( Improper frequency choice. ) end 31. The function le is: function P = waals(T,Vhat,gas) R = 0.08206; go = 1; switch gas case helium a = 0.0341;b = 0.0237; 4-32

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; end if go == 1 P = R*T/(Vhat-b)-a/Vhat^2; end

32. The script le is almost identical to that given in Table 4.7-2. The only changes are the use of the min command in four statements. The le 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) <= 4000 a(k) = min(900+100*k,1.2*x(2)); d(k) = min(150+50*k,.1*x(1)); else a(k) = min(a(k-1),1.2*x(2)); d(k) = min(d(k-1),.1*x(1)); end b = [a(k);d(k);0;0]; x = C*x+b; E(:,k) = x; end frosh = 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

The plot is shown in the gure.


Enrollment As A Function of Time 1100 1000 900 800 700 600 500 400 300 200 1 Frosh Soph Jr

Number of Students

Sr

5 Year

10

Figure : for Problem 32. 33. Assuming that the interest is applied monthly, and that the desposit is made on the rst of each month, the script le 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==12 m = 1; CD = 1.06*CD; if amount(k) >= 3000 amount(k) = amount(k) - 2000; CD(n+1) = 2000; n = n+1; 4-34

end end amount(k+1) = amount(k)*(1+rate) + deposit(k); m = m+1; end disp( 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 output is 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 le using a for loop to solve the problem for part (a) is shown below. The sales are stored in the vector S, the number produced are in the vector P, and the inventory is given by the vector I. S = [50,55,60,70,70,75,80,80,90,55]; 4-35

I(1) = 50; P(1) = 50; for k = 1:9 I(k+1) = I(k)+P(k)-S(k); P(k+1) = S(k); end I The sales are stored in the vector S, the number produced are in the vector P, and the inventory 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 le using a while loop to solve the problem for part (a) is shown below. The sales are stored in the vector S, the number produced are in the vector P, and the inventory is 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 end disp(I) The results are I = [50, 50, -5]. Thus the inventory remains at 50 for the rst two weeks 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

You might also like