palm_ch4

# palm_ch4 - Solutions to Problems in Chapter Four Test Your...

This preview shows pages 1–6. Sign up to view the full content.

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 = 0 1 0 0 z = x&y z = 1 1 1 1 z = x|y z = 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 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 first 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 file 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( 0 Incompatible values of x and q 0 ) 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( 0 Incompatible values of x and q 0 ) else disp(y) end else disp( 0 The magnitude of x is greater than 1 0 ) end 4-2
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; end end 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; end disp(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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
end disp(x) T4.5-1 The script file is angle = input( 0 Enter an angle in degrees. 0 ) switch angle case 45 disp( 0 Angle is in first quadrant 0 ) case -45 disp( 0 Angle is in second quadrant 0 ) case 135 disp( 0 Angle is in third quadrant 0 ) case -135 disp( 0 Angle is in fourth quadrant 0 ) otherwise disp( 0 Quadrant is unknown. 0 ) 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 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 first, second, and fifth 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.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern