Solved Problems: Solution
Solved Problems: Solution
Solved Problems: Solution
Solved Problems
1. Create a variable a that is a row vector with the following elements: 9, 1, 32,
7/4, 0, , 0.8, and .
Solution
Solution
1
2 Chapter 2: Solved Problems
Solution
>> c=[2.1E-2;sin(1.7*pi);28.5;2.7^(4/3);exp(3)]
c =
0.0210
-0.8090
28.5000
3.7597
20.0855
>>
Solution
>> d=[0.75*5.2^0.7;11.1;60^(1/3);tan(10*pi/
11);cosd(5)^2;0.116]
d =
2.3783
11.1000
3.9149
-0.2936
0.9924
0.1160
>>
5. Define the variables and , and then use them to create a row
vector (assign it to a variable named e) that has the following elements: x/y,
x+y, xy, , y2, and x.
Solution
5.8
>> e=[x/y x+y x^y x*y y^2 x]
e =
0.58621 9.2 1209.4 19.72
33.64 3.4
>>
6. Define the variables and , and then use them to create a col-
umn vector (assign it to a variable named f) that has the following elements:
d2, c, (c+d), cd, and d.
Solution
>> c=4.5
c =
4.5000
>> d=2.8
d =
2.8000
>> f=[d^2;c;(c+d);c^d;d]
f =
7.8400
4.5000
7.3000
67.4520
2.8000
>>
7. Create a variable g that is a row vector in which the first element is 3 and the
last element is 27, with an increment of 4 between the elements (3, 7, 11, … ,
27).
Solution
>> g=3:4:27
g =
3 7 11 15 19 23 27
>>
4 Chapter 2: Solved Problems
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.
Solution
>> h=linspace(68,12,8)
h =
68 60 52 44 36 28 20 12
>>
9. Create a variable M that is a column vector in which the first element is 6.4,
the elements increase with increments of 0.8, and the last element is 12. (A
column vector can be created by the transpose of a row vector.)
Solution
>> M=[6.4:0.8:12]'
M =
6.4000
7.2000
8.0000
8.8000
9.6000
10.4000
11.2000
12.0000
10. Create a variable N that is a column vector with seven equally spaced ele-
ments 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.)
Solution
>> N=linspace(44,23,7)'
N =
44.0000
40.5000
37.0000
33.5000
30.0000
26.5000
23.0000
>>
Chapter 2: Solved Problems 5
11. Using the colon symbol, create a row vector (assign it to a variable named
Time) in which the first element is 0, the spacing is 1, and the last element is 20.
Solution
>> Time=0:20
Time =
Columns 1 through 19
0 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17 18
Columns 20 through 21
19 20
>>
12. Using the linspace command, create a row vector (assign it to a variable
named Fours) with nine elements that are all 4.
Solution
>> Fours=linspace(4,4,9)
Fours =
4 4 4 4 4 4 4 4 4
13. Using the colon symbol, create a variable named Sevens that is a row vec-
tor of seven elements that are all the number 7.
Solution
>> Sevens(1:7)=7
Sevens =
7 7 7 7 7 7 7
>>
6 Chapter 2: Solved Problems
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 ele-
ments are 0s. Do not type the vector elements explicitly.
Solution
>> P(8)=5.9
P =
0 0 0 0 0 0
0 5.9000
>>
15. Use a single command to create a row vector (assign it to a variable named
q) with nine elements such that the last four elements are 8.1 and the rest of
the elements are 0s. Do not type the vector elements explicitly.
Solution
>> q(6:9)=8.1
q =
0 0 0 0 0 8.1000
8.1000 8.1000 8.1000
>>
16. Use a single command to create a row vector (assign it to a variable named
R) with 10 elements such that
R =
-4 -1 2 5 8 14 18 22 26 30
Do not type the vector elements explicitly.
Solution
17. Create two row vectors v=41:-3:29 and w=17:4:37. Then, by only
using the name of the vectors (v and w), create a row vector u that is made
from the elements of w followed by the elements of v.
Solution
>> v=41:-3:29
v =
41 38 35 32 29
>> w=17:4:37
w =
17 21 25 29 33 37
>> u=[w v]
u =
17 21 25 29 33 37 41 38 35
32 29
>>
Solution
>> T=[5:5:25]'
T =
5
10
15
20
25
>> S=[27:2:33]'
S =
27
29
31
33
>> R=[T; S]
R =
5
10
15
8 Chapter 2: Solved Problems
20
25
27
29
31
33
Solution
>> A=4:3:13
A =
4 7 10 13
>> B=[14:-2:6]'
B =
14
12
10
8
6
>> C=[B' A]
C =
14 12 10 8 6 4 7 10 13
>> D=[A';B]
D =
4
7
10
13
14
12
10
8
6
>>
Chapter 2: Solved Problems 9
20. Create a row vector vA=1:3:34 that has 12 elements. Then, create a new
nine-element vector vB from the elements of vA such that the first five ele-
ments 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.)
Solution
>> VA=1:3:34
VA =
1 4 7 10 13 16 19 22 25
28 31 34
>> VB=[VA(1:5) VA(9:12)]
VB =
1 4 7 10 13 25 28 31 34
21. Create a row vector vC=2:3:38 that has 13 elements. Then, create the fol-
lowing new vectors by assigning elements of vC to the new vectors:
(a) A vector (name it vCodd) that contains all the elements with odd index
of vC; i.e., vCodd = 2 8 14 ... 38.
(b) A vector (name it vCeven) that contains all the elements with even
index of vC; i.e., vCeven = 5 11 17 ... 35.
In both parts use vectors of odd and even numbers to address the elements
of vC that are assigned to vCodd, and vCeven, respectively. Do not enter
the elements of the vectors explicitly.
Solution
>> vC=2:3:38
vC =
2 5 8 11 14 17 20 23 26
29 32 35 38
>> vCodd=vC([1:2:13])
vCodd =
2 8 14 20 26 32 38
>> vCeven=vC([2:2:12])
vCeven =
5 11 17 23 29 35
10 Chapter 2: Solved Problems
22. Create two row vectors vD=20:4:44 and vE=50:3:71. Then, create the
following new vectors by assigning elements of vD and vE to the new vec-
tors:
(a) A vector (name it vDE) that contains the 2nd through the 5th 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 vec-
tors explicitly.
Solution
>> vD=20:4:44
vD =
20 24 28 32 36 40 44
>> vE=50:3:71
vE =
50 53 56 59 62 65 68 71
>> vDE=[vD(2:5) vE(4:7)]
vDE =
24 28 32 36 59 62 65 68
>> vED=[vE(6:-1:2) vD(4:-1:1)]
vED =
65 62 59 56 53 32 28 24 20
23. Create a nine-element row vector vF=5:7:61. Then create a vector (name
it vFrev) that consist of the elements of vF in reverse order. Do it by using
a vector to address the elements of VF. (Do not type the elements of vF vec-
tor explicitly.)
Solution
>> vF=5:7:61
vF =
5 12 19 26 33 40 47 54 61
>> vFrev=vF([9:-1:1])
vFrev =
61 54 47 40 33 26 19 12 5
Chapter 2: Solved Problems 11
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 indi-
vidual 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
Solution
>> A=[1:7;7:-1:1;linspace(2,9,7)]
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
>>
25. Create the following vector by using the linspace command. Do not type
individual elements explicitly.
B =
4 4 4 4 4 5 5 5 5
Solution
26. Create the following matrix by typing one command. Do not type individ-
ual elements explicitly.
C =
6 8
6 8
6 8
6 8
6 8
12 Chapter 2: Solved Problems
Solution
>> C=[linspace(6,6,5);linspace(8,8,5)]'
C =
6 8
6 8
6 8
6 8
6 8
>>
27. Create the following matrix by typing one command. Do not type individ-
ual elements explicitly.
D =
1 1 1 1
1 1 1 1
1 1 1 1
8 6 4 2
Solution
28. Create the following matrix by typing one command. Do not type individ-
ual elements explicitly.
E =
0 0 0 0 8
0 0 0 0 7
0 0 0 0 6
Solution
>> E(1:3,5)=[8:-1:6]
E =
0 0 0 0 8
0 0 0 0 7
0 0 0 0 6
>>
Chapter 2: Solved Problems 13
29. Create the following matrix by typing one command. Do not type individ-
ual elements explicitly.
F =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 8 6 4 2
Solution
>> F(3,[3:6])=8:-2:2
F =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 8 6 4 2
>>
30. Create the following matrix by typing one command. Do not type individ-
ual elements explicitly.
G =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
0 0 0 1 1
0 0 0 1 1
0 0 0 1 1
Solution
Solution
>> a=[5 8 -1 0 2]
a =
5 8 -1 0 2
>> b=[4 1 9 -2 3]
b =
4 1 9 -2 3
>> c=[-3 5 0 6 1]
c =
-3 5 0 6 1
>> % Part (a)
>> D=[a(1:3) b(1:3) c(1:3)]
D =
5 8 -1 4 1 9 -3 5 0
>> % Part (b)
>> E=[a(3:5) b(3:5) c(3:5)]'
E =
-1
0
2
9
-2
3
0
6
1
>>
Chapter 2: Solved Problems 15
Solution
>> a=[5 8 -1 0 2]
a =
5 8 -1 0 2
>> b=[4 1 9 -2 3]
b =
4 1 9 -2 3
>> c=[-3 5 0 6 1]
c =
-3 5 0 6 1
>> Ma=[c;b;a]
Ma =
-3 5 0 6 1
4 1 9 -2 3
5 8 -1 0 2
>> Mb=[c' b' a']
Mb =
-3 4 5
5 1 8
0 9 -1
6 -2 0
1 3 2
Solution
>> d=[6 -1 4 0 -2 5]
d =
6 -1 4 0 -2 5
>> e=[7 5 9 0 1 3]
e =
7 5 9 0 1 3
>> % Part (a)
>> M=[d(2:4);e(3:5);d(4:6)]
M =
-1 4 0
9 0 1
0 -2 5
>> % Part (b)
>> N=[d(2:5)' e(3:6)']
N =
-1 9
4 0
0 1
-2 3
>>
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. (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) 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
Solution
>> v=[5 0 -3 7 6 -1 2 8 4 9]
v =
5 0 -3 7 6 -1 2 8 4 9
>> a=v([4 5:7 10])
a =
7 6 -1 2 9
>> b=v([9, 1, 6:-2:2])
b =
4 5 -1 7 0
18 Chapter 2: Solved Problems
Solution
>> u=[0 9 -5 6 3 -1 2]
u =
0 9 -5 6 3 -1 2
>> w=[-2 3 7 -4 0 1 5]
w =
-2 3 7 -4 0 1 5
>> % Part (a)
>> A=[u(2:5);w([7 5]) u([6 7])]
A =
9 -5 6 3
5 0 -1 2
>> % Part (b)
>> B=[w(4:7)', zeros(4,2), u([1 3 5 7])']
B =
-4 0 0 0
0 0 0 -5
1 0 0 3
5 0 0 2
>>
Chapter 2: Solved Problems 19
By writing one command and using the colon to address range of elements
(do not type individual elements explicitly), use the matrix M to:
(a) Create a five-element row vector named Va that contains the elements
of the third row of M.
(b) Create a three-element column vector named Vb that contains the ele-
ments of the fourth column of M.
(c) Create an eight-element row vector named Vc that contains the ele-
ments of the second row of M followed by the elements of the third col-
umn of M.
Solution
(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 ele-
ments of the first column of N, followed by the elements of the third col-
umn of N, followed by the elements of the sixth column of N.
(c) 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.
Solution
(a) Create a matrix Ma from the first, third and fourth rows, and the
first two and last two columns of matrix G.
(b) Create a matrix Mb from the first three rows and the second,
fourth, and sixth columns of matrix G.
Solution
>> G=[0.1:0.1:0.7;10:-1:4;0:0.2:1.2;5:-2:-7]
G =
0.1000 0.2000 0.3000 0.4000 0.5000
0.6000 0.7000
10.0000 9.0000 8.0000 7.0000 6.0000
5.0000 4.0000
0 0.2000 0.4000 0.6000 0.8000
1.0000 1.2000
5.0000 3.0000 1.0000 -1.0000 -3.0000 -
5.0000 -7.0000
>> % Part (a)
>> Ma=G([1 3 4],[1 2 6 7])
Ma =
0.1000 0.2000 0.6000 0.7000
0 0.2000 1.0000 1.2000
5.0000 3.0000 -5.0000 -7.0000
>> % Part (b)
22 Chapter 2: Solved Problems
(a) Create a 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 include 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 matrix Gb the first two rows and columns 3 through 6 of
K.
Solution
>> K=[0.25:0.25:1.75;2:2:14;25:5:55]
K =
0.2500 0.5000 0.7500 1.0000 1.2500
1.5000 1.7500
2.0000 4.0000 6.0000 8.0000 10.0000
12.0000 14.0000
25.0000 30.0000 35.0000 40.0000 45.0000
50.0000 55.0000
>> % Part (a)
>> Ga=[K(:,2)' K(:,5)'; K(2,[1:6]); K(3,[2:7])]
Ga =
0.5000 4.0000 30.0000 1.2500 10.0000
45.0000
2.0000 4.0000 6.0000 8.0000 10.0000
12.0000
30.0000 35.0000 40.0000 45.0000 50.0000
55.0000
>> % Part (b)
>> Gb=K([1 2],[3:6])
Gb =
Chapter 2: Solved Problems 23
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) A=S(2,[2,4]) b) B=S(3,[3:5])
c) C=S([2:4],[4:6]) d) D=S(:,[1:3])
Solution
>> S=reshape(1:24,4,6)
S =
1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 23
4 8 12 16 20 24
>> A=S(2,[2,4])
A =
6 14
>> B=S(3,[3:5])
B =
11 15 19
>> C=S([2:4],[4:6])
C =
14 18 22
15 19 23
16 20 24
>> D=S(:,[1:3])
D=
1 5 9
2 6 10
3 7 11
4 8 12
>>
24 Chapter 2: Solved Problems
Solution
>> T=reshape(2:2:30,3,5)
T =
2 8 14 20 26
4 10 16 22 28
6 12 18 24 30
>> D=[T(1,1:4);T(3,2:5)]
D =
2 8 14 20
12 18 24 30
>> E=[T(:,4); T(2,:)']
E =
20
22
24
4
10
16
22
28
>> F(3:5,3:5)=T(1:3,1:3)
F =
0 0 0 0 0
0 0 0 0 0
0 0 2 8 14
0 0 4 10 16
0 0 6 12 18
>>
Chapter 2: Solved Problems 25
43. 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.
V=[1:2:11;13:2:23]
V(:,4:6)=V(:,1:3)
V(3:4,:)=V
V(:,[2:3 5:6])=[]
Solution
>> V=[1:2:11;13:2:23]
V =
1 3 5 7 9 11
13 15 17 19 21 23
>> V(:,4:6)=V(:,1:3)
V =
1 3 5 1 3 5
13 15 17 13 15 17
>> V(3:4,:)=V
V =
1 3 5 1 3 5
13 15 17 13 15 17
1 3 5 1 3 5
13 15 17 13 15 17
>> V(:,[2:3 5:6])=[]
V =
1 1
13 13
1 1
13 13
>>
44. Using the zeros, ones, and eye commands, create the following arrays by
typing one command:
Solution
A =
1 0 0 1 0 0
0 1 0 0 1 0
0 0 1 0 0 1
>> % Part (b)
>> B([1 2],[4 5])=ones(2)
B =
0 0 0 1 1
0 0 0 1 1
>> % Part (c)
>> C=[ones(3,1) zeros(3,2) [ones(1);zeros(2,1)]]
C =
1 0 0 1
1 0 0 0
1 0 0 0
>>
45. Use the eye, ones, and zeros commands to create the following arrays:
Using the variables A, B, and C, write a command that creates the following
matrix D:
Solution
>> A=eye(2)
A =
1 0
0 1
>> B=ones(1,2)
B =
1 1
>> C=zeros(3)
C =
0 0 0
0 0 0
Chapter 2: Solved Problems 27
0 0 0
>> D=[C [A;B]]
D =
0 0 0 1 0
0 0 0 0 1
0 0 0 1 1
>>
28 Chapter 2: Solved Problems