Professional Documents
Culture Documents
=
23 22 21
13 12 11
A A A
A A A
A and
(
=
23 22 21
13 12 11
B B B
B B B
B
Then ,
(
+ + +
+ + +
= +
B A B A B A
B A B A B A
B A
23 23 22 22 21 21
13 13 12 12 11 11
And,
(
=
B A B A B A
B A B A B A
B A
23 23 22 22 21 21
13 13 12 12 11 11
MATLAB Example :
>>A=[1 2 3;4 5 6];
>>B=[8 9 10;11 12 13];
>>A+B
ans =
9 11 13
15 17 19
>>A-B
ans =
8
-7 -7 -7
-7 -7 -7
(B) Array Multiplications :
Mathematical Review :
If
(
(
(
(
=
43 42 41
33 32 31
23 22 21
13 12 11
A A A
A A A
A A A
A A A
A
and
(
(
(
=
32 31
22 21
12 11
B B
B B
B B
B
Be sure that, number of columns of Matrix A is equal to number of rows of Matrix B .Otherwise multiplication
is not defined.
Then,
(
(
(
(
=
43 42 41
33 32 31
23 22 21
13 12 11
A A A
A A A
A A A
A A A
AB
(
(
(
32 31
22 21
12 11
B B
B B
B B
=
(
(
(
(
+ + + +
+ + + +
+ + + +
+ + + +
32 43 22 42 12 41 31 43 21 42 11 41
32 33 22 32 12 31 31 33 21 32 11 31
32 23 22 22 12 21 31 23 21 22 11 21
32 13 22 12 12 11 31 13 21 12 11 11
B A B A B A B A B A B A
B A B A B A B A B A B A
B A B A B A B A B A B A
B A B A B A B A B A B A
A numerical example :
A=
(
(
(
8 2 5
1 6 2
3 4 1
and B=
(
(
(
6 2
3 1
4 5
Then,
= AB
(
(
(
8 2 5
1 6 2
3 4 1
(
(
(
6 2
3 1
4 5
=
(
(
(
+ + + +
+ + + +
+ + + +
8.6 2.3 5.4 8.2 2.1 5.5
1.6 6.3 2.4 1.2 6.1 2.5
3.6 4.3 1.4 2 . 3 1 . 4 5 . 1
=
(
(
(
74 43
32 18
34 15
MATLAB Example :
>>A=[1 4 3;2 6 1;5 2 8];
>>B=[5 4;1 3;2 6];
>>A*B
ans =
15 34
18 32
43 74
(C) Inverse of a Matrix
MATLAB Example :
>>A=[2 1 4;4 1 8;2 -1 3];
>>inv(A)
ans =
5.5000 -3.5000 2.0000
9
2.0000 -1.0000 0
-3.0000 2.0000 -1.0000
Note: The Command >>A^-1 will have the same effect.
(D)Left Division and right Division
Command Name Meaning Example
A\B Left Division A
-1
B >>A=[1 2 3;4 5 6;7 8 9];
>>B=[10 11 12;13 14 15;16 17 18];
>>A\B % Left division
ans =
-20.0000 -17.0000 -18.0000
33.0000 26.0000 27.0000
-12.0000 -8.0000 -8.0000
A/B Right Division AB
-1
>>A/B % right division
ans =
1.0000 3.0000 -3.0000
1.0000 2.0000 -2.0000
0.5000 2.0000 -1.5000
(E)Element-By-Element Operations (Very Very Important)
If two matrices are
(
(
(
=
33 32 31
23 22 21
13 12 11
A A A
A A A
A A A
A and
(
(
(
=
33 32 31
23 22 21
13 12 11
B B B
B B B
B B B
B
Then, observe the following operation :
Command Meaning Outcome
A.*B Multiplies
every element
of A with
corresponding
element of B
(
(
(
=
33 33 32 32 31 31
23 23 22 22 21 21
13 13 12 12 11 11
B A B A B A
B A B A B A
B A B A B A
B * . A
A./B Divides every
element of A
by
corresponding
element of B
(
(
(
=
33 33 32 32 31 31
23 23 22 22 21 21
13 13 12 12 11 11
B / A /B A /B A
B / A /B A /B A
B / A /B A /B A
B / . A
A.^n Every
element of A
is raised by a
power n.
(
(
(
(
=
n
33
n
32
n
31
n
23
n
22
n
21
n
13
n
12
n
11
A A A
A A A
A A A
n .^ A
Note: Observe carefully difference between using dot and not using dot.
(F)Built in functions for analyzing arrays :
Function Description Example
mean(A) Returns mean value of the elements of vector A. >>A=[5 9 2 4];
>>mean(A)
10
ans =
5
c =max(A) Returns maximum value of the elements of vector A >>c=max(A)
c =
9
[d n] =max(A) Returns maximum value of the elements of vector A and
also return its position.
>>A=[5 9 2 4];
>>[d n] =max(A)
d =
9
n =
2
c=min(A) Returns minimum value of the elements of vector A >>A=[3 4 5 7 -5 10];
>>c=min(A)
c =
-5
[d n]=min(A) Returns maximum value of the elements of vector A and
also return its position.
>>A=[3 4 5 7 -5 10];
>>[d n]=min(A)
d =
-5
n =
5
sum(A) Returns the sum of the vector A. >>A=[5 9 2 4];
>>sum(A)
ans =
20
sort(A) Arranges the elements of vector A in ascending order. >>A=[5 9 2 4];
>>sort(A)
ans =
2 4 5 9
median(A) Returns the median value of the elements of vector A. >>A=[5 9 2 4];
>>median(A)
ans =
4.5000
std(A) Returns the standard deviation of the elements of vector A. >>A=[5 9 2 4];
>>std(A)
ans =
2.9439
det(A) Returns the determinant of matrix A.
A must be square matrix.
>>A=[2 4 6;6 9 1;0 2 7];
>>det(A)
ans =
26
dot(a,b) Returns the scalar or dot product of two vectors a and b. >>a=[1 2 3];
>>b=[3 4 5];
>>dot(a,b)
ans =
26
cross(a,b) Returns the scalar or dot product of two vectors a and b. >>a=[1 2 3];
>>b=[3 4 5];
>>cross(a,b)
ans =
-2 4 -2
11
Report : [Submit as lab report in the next session.]
Q1. Use MATLAB to determine how many elements are in the array
cos(0.1):0.02:log10(105) .
Use MATLAB to find the 25
th
element.
Q2. The table 1.1 gives the speed of an aircraft on each leg of a certain trip and the time
spent on each leg . Compute the km traveled on each leg and the sum of km traveled
by all four legs.
Legs
1 2 3 4
Speed (Km/hr) 200 250 400 300
Time(Hr) 2 5 3 4
Table 1.1
Q3. Write MATLAB Command to evaluate the following :
W 10 W , 87 . 60 , 10 R , 5 I , 255 V for ,
W R I cos VI
cos VI
Efficiency
c
0
c
2
= = u = = =
+ + u
u
=
Q4. Use matrix operations to solve the following system of linear equations :
0 z 3 y 10 x 6
4 z 2 y 8 x 2
8 z 6 y 2 x 4
= + +
= + +
= +
[Hints : AX=B
Where, A=
(
(
(
3 10 6
2 8 2
6 2 - 4
and, B=
(
(
(
0
4
8
Q5. Create the following matrix C :
(
(
(
=
35 28 21 14 7
15 12 9 6 3
10 8 6 4 2
C
Use the matrix C to:
Create a six element column vector named A that contains the elements of the 2
nd
and 3rd column of C.
Lab Sheet prepared by:
B.K.M. Mizanur Rahman
Assistant Professor , Department of EEE,
United International University.
Last update : Spring 2013
The easiest and best way to learn MATLAB is to use MATLAB.