
Chapter 6 Solved Problems 1. Evaluate the following expressions without using MATLAB. Check the answers with MATLAB. (a)(b) (c)(d) Solution >> % Part (a) >> 6*4>32-3 ans = 0 >> % Part (b) >> y=4*3-7<15/3>-1 y = 1 >> % Part (c) >> y=2*(3<8/4+2)^2<(-2) y = 0 >> % Part (d) >> (5+~0)/3==3-~(10/5-2) ans = 1 >> 1 2 Chapter 6: Solved Problems 2. Given: , , . Evaluate the following expressions without using MATLAB. Check the answers with MATLAB. (a)(b) (c)(d) Solution >> d=6 d = 6 >> e=4 e = 4 >> f=-2 f = -2 >> % Part (a) >> y=d+f>=e>d-e y = 0 >> % Part (b) >> y=e>d>f y = 1 >> % Part (c) >> y=e-d<=d-e==f/f y = 1 >> % Part (d) >> y=(d/e*f<f)>-1*(e-d)/f y = 1 >> Chapter 6: Solved Problems 3 3. Given: v = [–2 4 1 0 2 1 2 ] and w = [2 5 0 1 2 –1 3 ]. Evaluate the follow- ing expressions without using MATLAB. Check the answers with MAT- LAB. (a)~v ==~w (b) w > = v (c) v > ~ –1*w (d) v > –1*w Solution >> v=[-2 4 1 0 2 1 2] v = -2 4 1 0 2 1 2 >> w=[2 5 0 1 2 -1 3] w = 2 5 0 1 2 -1 3 >> % Part (a) >> ~v==~w ans = 1 1 0 0 1 1 1 >> % Part (b) >> w>=v ans = 1 1 0 1 1 0 1 >> % Part (c) >> v>~1*w ans = 0 1 1 0 1 1 1 >> % Part (d) >> v>-1*w ans = 0 1 1 1 1 0 1 >> 4 Chapter 6: Solved Problems 4. Use the vectors v and w from Problem 3. Use relational operators to create a vector u that is made up of the elements of v that are smaller than or equal to the elements of w. Solution Command Window: >> v=[-2 4 1 0 2 1 2]; >> w=[2 5 0 1 2 -1 3]; >> u=v(v<=w) u = -2 4 0 2 2 5. Evaluate the following expressions without using MATLAB. Check the answers with MATLAB. (a)0|7&9&–3 (b)7>6&~0<=2 (c) ~4<5|0>=12/6 (d) – 7<–5<–2&2+3<=15/3 Solution >> % Part (a) >> 0|7&9&-3 ans = 1 >> % Part (b) >> 7>6&~0<=2 ans = 1 >> % Part (c) >> ~4<5|0>=12/6 ans = 1 >> % Part (d) >> -7<-5<-2&2+3<=15/3 ans = 0 >> Chapter 6: Solved Problems 5 6. Use loops to create a matrix in which the value of each element is two times its row number minus three times its column number. For example, the value of element (2,5) is . Solution Script File for i=1:4 for j=1:6 A(i,j)=2*i-3*j; end end A Command Window: A = -1 -4 -7 -10 -13 -16 1 -2 -5 -8 -11 -14 3 0 -3 -6 -9 -12 5 2 -1 -4 -7 -10 7. Write a program that generates a vector with 30 random integers between –20 and 20 and then finds the sum of the all the elements that are divisible by 3. Solution Script File: v=randi([-20 20],1,30) n=length(v); S=0; for i=1:n if rem(v(i),3)==0 S=S+v(i); end end S 6 Chapter 6: Solved Problems Command Window: v = Columns 1 through 12 10 -10 0 8 16 19 2 -15 -14 - 10 14 -10 Columns 13 through 24 13 -11 18 -6 -12 -10 5 -1 -6 14 3 2 Columns 25 through 30 17 -9 11 10 -5 3 S = -24 8. Write a program that asks the user to input a vector of integers of arbitrary length. Then, using a for loop the program examine each element of the vec- tor. If the element is positive its value is doubled. If the element is negative its value is tripled. The program displays the vector that was entered and the modified vector. Execute the program and when the program ask the user to input a vector type randi([-10 20],1,19). This creates a 19-element vector with random integers between –10 and 20. Solution Script File: v=randi([-10 20],1, 19) n=length(v); for i=1:n if v(i) > 0 v(i)=2*v(i); end if v(i) < 0 v(i)= 3*v(i); end end vNew=v Command Window: v = Columns 1 through 15 Chapter 6: Solved Problems 7 -8 -9 6 14 18 -6 7 4 -10 0 -5 14 -1 6 -5 Columns 16 through 19 8 -2 10 11 vNew = Columns 1 through 15 -24 -27 12 28 36 -18 14 8 -30 0 -15 28 -3 12 -15 Columns 16 through 19 16 -6 20 22 9. Write a program that asks the user to input a vector of integers of arbitrary length. Then, using a for loop the program eliminates all the negative ele- ments. The program displays the vector that was entered and the modified vector, and a message that says how many elements were eliminated. Exe- cute the program and when the program ask the user to input a vector type randi([-15 20],1,25). This creates a 25-element vector with random integers between –15 and 20. Solution Script File: v=randi([-15 20],1, 25) n=length(v); j=0; for i=1:n if v(i) >= 0 j=j+1; vNew(j)=v(i); end end elim=n-j; vNew fprintf('%.0f elements were eliminated.\n',elim) Command Window: v = Columns 1 through 13 8 Chapter 6: Solved Problems 17 6 7 15 13 5 -9 -7 16 - 14 2 -9 20 Columns 14 through 25 10 3 1 -13 9 -14 -13 3 -12 14 14 11 vNew = Columns 1 through 13 17 6 7 15 13 5 16 2 20 10 3 1 9 Columns 14 through 17 3 14 14 11 8 elements were eliminated. 10. The daily high temperature (°F) in New York City and Denver, Colorado during the month of January 2014 is given in the vectors below (data from the U.S. National Oceanic and Atmospheric Administration). NYC = [33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 39 36 45 33 18 19 19 28 34 44 21 23 30 39] DEN = [39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 52 62 45 62 40 25 57 60 57 20 32 50 48 28] where the elements in the vectors are in the order of the days in the month. Write a program in a script file that determines and displays the following information: (a) The average temperature for the month in each city (rounded to the nearest degree). (b) The number of days that the temperature was above the average in each city. (c) The number of days that the temperature in Denver was higher than the temperature in New York. Solution Script File: clear, clc NYC = [33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 ... 39 36 45 33 18 19 19 28 34 44 21 23 30 39]; DEN = [39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 ... 52 62 45 62 40 25 57 60 57 20 32 50 48 Chapter 6: Solved Problems 9 28]; %Part (a) NYCav=round(mean(NYC)); DENav=round(mean(DEN)); fprintf('The averager temperature in New York City is: % g F.\n',NYCav) fprintf('The averager temperature in Denver is: % g F.\n',DENav) %Part (b) nNYC=sum(NYC>NYCav); nDEN=sum(DEN>DENav); fprintf('During % g days the temperature in New York City was above the average.\n',nNYC) fprintf('During % g days the temperature in Denver was above the average.\n',nDEN) %Part (c) DENhNYC=sum(DEN>NYC); fprintf('During % g days the temperature in Denver was higher than in New York City.\n',DENhNYC) Command Window: The averager temperature in New York City is: 35 F. The averager temperature in Denver is: 44 F. During 15 days the temperature in New York City was above the average. During 18 days the temperature in Denver was above the average. During 22 days the temperature in Denver was higher than in New York City. 10 Chapter 6: Solved Problems 11. The Pascal’s triangle can be displayed as elements in a 10 0 0 00 lower-triangular matrix as shown on the right. Write a 11 0 0 00 MATLAB program that creates a matrix that dis- plays n rows of Pascal’s triangle. Use the program to create 12 1 0 00 4 and 7 rows Pascal’s triangles. (One way to calculate the 13 3 1 00 value of the elements in the lower portion of the matrix is 14 6 4 10 15101051 .) Solution Script File: n=6; pt=zeros(n); for i=1:n for j=1:i pt(i,j)=factorial(i-1)/(factorial(j- 1)*factorial(i-j)); end end pt Command Window: pt = 1 0 0 0 0 0 1 1 0 0 0 0 1 2 1 0 0 0 1 3 3 1 0 0 1 4 6 4 1 0 1 5 10 10 5 1 Chapter 6: Solved Problems 11 12. Tribonacci numbers are the numbers in a sequence in which the first three elements are 0, 1, and 1, and the value of each subsequent element is the sum of the previous three elements: 0, 1, 1, 2, 4, 7, 13, 24, ..
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages52 Page
-
File Size-