0% found this document useful (0 votes)
122 views12 pages

Matlab Code

Uploaded by

kthshlxyz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
122 views12 pages

Matlab Code

Uploaded by

kthshlxyz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
AGUILAR, KEITH ANGELINE A. BSCE 3¢ PROBLEM Create a variable b that is a row vector with the following elements: sin(2) 3 V5.3°,6.71 «10°, 3 +5. 1?)cos53*, 15.8 90, and Solution: clc, clear, close all %eFine (b) b = [sqrt(5.2°3),6.71*10%3, (345.12)*cosd(53),15.8, sqrt(9)*(1/3),sin(pi/3)/(tand(20))] b= 1K6 10? x 2.0119 6.7108 2.0175 8.0158 0.0021 8.0024 The variable b contains the required elements of the row vector PROBLEM Create a variable d that is a column vector with the following elements: 0.75x5.2°7, 11.1, «/60 , tan! WE) cos's and 0.116 Solution: clc, clear, close all wbeFine (d) d=[ (8.75*(5.2)9(8.7)),11.1, sqrt (68)*(1/3) ,tan(1@*pi) /11, (cosd(5)*2), (@.116)] ds 16 2.3783 11,1008 1.9786 -0.0008 0.9924 0.1160 Variable d is the column vector that contains the required elements. PROBLEM 6: Define the variables and o=4.5 and d=2.8 , and then use them to create a column vector (assign itto a variable named f) that has the following elements: @.c,(c+d),4,andd clc, clear, close all ‘define (F) Fa[d*2;c;c+d;c%dsd] f 1 7.8400 4.5008 7.3000 67.4520 2.8000 Vector f is the column vector containing the necessary components PROBLEM 8: Create a variable h that is a row vector with eight equally spaced elements in which the first element is 68 and the last element is 12 cle, clear, close all Define (h) h=[68,8,12] he ne 6 8 1 The row vector (vector fi) has the eight essential elements separated evenly. PROBLEM 10: Create a variable N that is a column vector with seven equally spaced elements in which the first element is 44 and the last element is 23. (A column vector can be created by the transpose of a row vector.) cle, clear, close all Define (N) N=[4457523] The column vector NV has seven necessary elements that must be uniformly spaced apart. PROBLEM 1 Using the linspace command, create a row vector (assign ito a variable named Fours) with nine elements that are all 4 cle, clear, close all Define Fours Fours=Linspace(4,4,9) Fours, 4 4 4 4 4 4 4 4 4@ ‘The row vector known as "Vector Fours” has nine members, all of which are 4. PROBLEM 14: Use a single command to create a row vector (assign it to a variable named P) with eight elements such that the last element is 5.9 and the rest of the elements are 0s. Do not type the vector elements explicitly. cle, clear, close all %DeFine (P) 0,5.9,8] Pete 2 5.9802 8.0000 Vector P is the row vector with the required elements. PROBLEM 16: Use a single command to create a row vector (assign it to a variable named R) with 10 elements such that R 4-12.58 14 18 22 26 30 Do not type the vector elements explicitly. cle, clear, close all Define (R) R = [-4,30,10] 3 18 ‘The row vector, vector R, contains the necessary items. PROBLEM 1 Create two column vectors T = [5:5 : 25'] and § = [27; 2: 33]'. Then, by only using the name of the vectors (T and S), create a column vector R that is made from the elements of T followed by the elements of S. cle, clear, close all % Define vectors T & S T = [5:5:25]" S = [27:2:33]" 1 a7 29 31 23 % Define variable R R = [15S] R= 9x1 5 10 1s 20 25 27 29 31 23 Vector Ris the column vector with the required elements. PROBLEM 20: Create a row vector vA = from the elements of vA such that the first five elements are the first five elements of the vector vA, and the last four are the last four elements of the vector vA. Use the colon symbol to address a range of elements. (Do not type the elements of the vector vA explicitly.) :3.: 34 that has 12 elements. Then, create a new nine-element vector vB SOLUTION: cle, clear, close all % Define vector vA vA = 1:3:34 vA = 112 104 7 1 33 36 19 2 2 2 3M % Define ve vB = [vA([1:5]),vA([9:12])] vB = 1x9 1 4 7 1 13 2 eo ‘Vector vB is the row vector with the required elements from vA. PROBLEM 22: Create two row vectors vD = 20:4: 44 and vE = 50: 3:71. Then, create the following vectors by assigning elements of vD and vE to the new vectors (a) A vector (name it vDE) that contains the 2nd through the Sth elements of vD and the 4th through 7th elements of vE; i.e., VDE = 24 28 32 36 59 62 65 68. (b) A vector (name it VED) that contains elements 6, 5, 4, 3, and 2 of vE and elements 4, 3, 2, and 1 of VD; i.e., VED = 65 62 59 56 53 32 28 24 20. In both parts use vectors to address the elements of vD and vE that are assigned to VDE and vED, respectively. Do not enter the elements of the vectors explicitly, Solutio! cle, clear, close all % Define vectors vD & vE vD = 20:4:44 vO = 1x7 2 2 28 2 3% ae ad VE = $0:3:71 VE = 1g 50 53 S659 62 SBT %(a) Define vDE vOE = [vD([2:5]), vE([4:7])] WOE = 1x8 2 28 32659 wk SB % Solution for (b) Define vED veD = [ve([6:-1:2]), vO([4:-1:1])] VED = 1x9 6 62 59 56 S382 Bh Vectors VDE & VED are the row vector with the required elements from vD and vE. Problem 24: Create the following matrix by assigning vectors with constant spacing to the rows (use the linspace command for the third row). Do not type individual elements explicitly. A= 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 7.0000 6.0000 5.0000 4.0000 3.0000 2.0000 1.0000 2.0000 3.1667 4.3333 5.5000 6.6667 7.8333 9.0000 Solutio! clc, clear, close all % Define matrix A A = [1:757:-1:45Linspace(2,9,7)] A= a7 1.8200 2.0000 3.0800 © 4.2000 © 5.2088 6.2000 7.0000 7.8200 6.800 5.2800 4.0000 3.0000 2.0008 1.0008 2.0000 3.1667 4.3333 5.5002 6.6667 7.8333 9.0008 Matrix A is a matrix that contains the required elements. Problem No. 26: Create the following matrix by typing one command. Do not type individual elements explicitly. C= 6 8686868 68 Solution: cle, clear, close all % Define matrix C © = [[linspace(6,6,5)]*, [linspace(8,8,5)]"] c= 5x2 6 8 6 8 6 8 5 8 6 8 ‘The matrix C is a matrix that contains the required elements. Problem No. 28: Create the following matrix by typing one command. Do not type individual elements explicily. E 00080000700006 Solutio clc, clear, close all % Define matrix E = [zeros(3,4), [linspace(8,6,3)]"] sans Matrix £ is a matrix that contains the required elements. Problem No. 30: Create the following matrix by typing one command. Do not type individual elements explicitly. G= 1 1111111141111100011 0001100014 Solution: clc, clear, close all % Define matrix G G = [[ones(3,3); zeros(3,3)], ones(6,2)] G = 6x5 aoa oa aod aoa oa aod aoa a aod ee @ a1 oa ee @ a oa ee @ a oa Matrix E is a matrix that contains the required elements Problem No. 32: Create the following three row vectors: a= [58-102], = [419-23], andc [3506 1}, (a) Use the three vectors in a MATLAB command to create a matrix in which the rows are the vectors ©,b, and a, respectively, (b) Use the three vectors in a MATLAB command to create a matrix in which the columns are the vectors ¢, b, and a, respectively. Solution: clc, clear, close all % Define vectors a= [58-102] asa 5 8 41 8 2 b= [419-23] % Solution for (a) Define R R= [csbsal] a= ans 3 5 6 6 3 18 23 5 a. 8 2 % Solution for (b) Define ¢ C= [c',b',a"] C= 503 324s 5 a ° a4 6 2 @ 1302 Matrix R isa matrix that contains the vectors ¢, b, and a as its rows Matrix C is a matrix that contains the vectors ¢, b,and a as its columns Problem No. 34: By hand (pencil and paper) write what will be displayed if the following commands are executed by MATLAB. Check your answers by executing the commands with MATLAB. In parts (b), (c), (d), (e), and (f) use the vector that was defined in part (a). (a) a=2:2:20 b) b =a (4:8) (c) ¢= a(1:2:7) (d) d= a(8:-1:4) (e) e = [a(1:5);a(6:10)] (f) e = [a(2:5)' a(6:9))] Solution: cle, clear, close all % Define vector a % Solution for (b) b = a(4:8) beans 8 1 2 mw 16 % Solution for (c) © = a(1:2:7) % Solution for (d) d = a(8:-1:4) des 6 4 2 eB % Solution for (e) e = [a(1:5)3a(6:28)] es ans 2 4 6 8 w mB 14 16 1828 % Solution for (#) e = [a(2:5)" a(6:9)"] es 402 2 6 14 8 16 1 a8 PROBLEM 36 Create the following Vectors: u=[0 9 -5 6 3 -1 2Qjandw By hand (pencil and paper) write what will be displayed if the following commands are executed by MATLAB. Check your ansqers by executing the commands with MATLAB a) A= [u(2 : 5); (7 5]) u(l67))] b) B= [w(4 : 7)',zeros(4, 2), u((1 3 57) clear, close all us [@9 -563-12); w=[-237-4015]; (a) Define A A = [u(2:5)5 w([7 51) u([6 7])] %(b) Define B B = [w(4:7)', zeros(4,2), u([1 3 5 7])'] B= axa 4 @ 8 @ ee @ -5 100 @ 3 5 @ @ 2 PROBLEM 38 Create the following matrix N: 0 3 6 9 12:15 N=]18 21 24 27 30 33 36 39 42 45 48 SI (|t can be done by typing: N=reshape(0:3:51,6,3)') By writing one command and using the colon to address range of elements (do not type individual elements explicitly), use the matrix to: a) Create a six-element row vector named Ua that contains the first three elements of the first row of N followed by the last three elements of the third row of N b) Create a nine-element column vector named Ub that contains the elements of the first column of N, followed by the elements of the third column of N, followed by the elements of the sixth column of N. ¢) Create a six-element column vector named Uc that contains elements 2, 3, 4, and 5 of the second row of N, followed by elements 2 and 3 of the fifth column of N. cle, clear, close all N = reshape(0:3:51,6,3)'5 %(a) Define Ua Ua = [N(1,1:3)3 N(3,4:6)] ua = 203 e 3 6 45° 481 %(b) Define Ub Ub = [N(:,1)5N(:,3)5 N(:,6)] Ub = 9x1 ° 18 36 6 24 42 15 33 51 %(c) Define Uc Uc = [N(2,2:5)'5M(2:3,5)] uc = eer 21 24 27 30 30 43 Vectors Ua, Ub, and Uc are vectors containing the required elements from vector N PROBLEM 40 Create the following matrix K: 0.5 0.75 1.0 1.25 1.5 1.75 4 6 8 10 12 14 30°35 «40 45 «50S: a) Create a 3x6 matrix Ga such that its first row includes the elements of the second column of K followed by the elements of the fifth column of K, the second row of Ga includes the first six elements of the second row of K, and the third row of Ga includes the last six elements of the third row of K. b) Create a 2x4 matrix Gb from the first two rows and columns 3 through 6 of K. cle, clear, close all k = [linspace(@.25,1.75,7); linspace(2,14,7); linspace(25,55,7)] K = a7 0.2508 @,[email protected]@0 1.0008 «1.2588 1.5888 1.7500 2.8200 4.8000 © 6.0800 8.2000 10.0008 12.2008 14.0000 25.0000 30.0000 35.0800 40.0000 45.0080 59.2000 55.0000 %(a) Define Ka Ka = [K(:,2)",K(:,5)"5K(2,1:6) 5k(3,2:7)] ka = 356 2.5008 4.0008 © 38.0008 © 1.2588 10.0008 45,0000 2.0000 4.0000 © 6.0800 © 8.0000 10.9082 12.2000 30.0000 35.0000 49.0000 45.0000 50.0000 55.0000 %(b) Define Kb Kb = [K(1:2,4:6)] kb = 283 1.0000 1.2500 1.5000 8.2000 [email protected] 12.0000 Matrices Ka and Kb are matrices containing the required elements from matrix K. PROBLEM 42 The following matrix is defined in MATLAB: 46 8 0 T=|12 14 16 18 20 22 24 26 28 30 By hand (pencil and paper) write what will be displayed if the following commands are executed by MATLAB. Check your answers by executing the commands with MATLAB. a) D=(T(1,1:4);7,2:5)] b) £ = (76.4): 72..)] ©) FB:5,3 =7(1:3,1:3) cle, clear, close all T = [2:2:10; 12:2:20; 22:2:30); %(a) Define D D = [T(4,1:4)5 T(3,2:5)] D = 2x4 2 4 6 8 2 2602838 %(b) Define E B= [1(:.4)3 1(2.2)"] E = ext 8 18 28 2 4 16 18 20 xc) F(3:5,3:5) = T(1:3,1:3) Fe 5s ee @ 8 @ 2 @ @ 8 @ 0 e@ 2 4 6 ee 2 mw 16 @ @ 2 2m 2% Mathlab answer and by hand answer are found to be the same. PROBLEM 44 Using the zeros, ones, and eye commands, create the following arrays by typing one command: 100100 ajo 10010 oo1001 ooo1r Poor 1001 [1000 1000 cle, clear, close all %(a) Define A A = [eye(3),eye(3)] 1 e@ @ 2 e e@ 1 @ 8® 1 @ ee 1 8 1 %(b) Define B B = [zeros(2,3),ones(2,2)] B= 2x5 ee @ a a ee @ 1 a %(c) Define C € = [ones(1,1),zeros(1,2),ones(1,1);ones(2,1),zeros(2,3)] c= axa e e 1 @ @ @ Therefore, using these commands will give you the required arrays.

You might also like