0% found this document useful (0 votes)
25 views8 pages

CH 4

This document discusses matrices in MATLAB including how to define, extract data from, and perform operations on matrices. It covers creating special matrices like zeros, ones, and identity matrices. It also discusses solving systems of linear equations using matrix inverse, left division, and reduced row echelon form.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

CH 4

This document discusses matrices in MATLAB including how to define, extract data from, and perform operations on matrices. It covers creating special matrices like zeros, ones, and identity matrices. It also discusses solving systems of linear equations using matrix inverse, left division, and reduced row echelon form.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CHAPTER (4)

MATLAB
MATRICES
1. Defining Matrices

 You can define a matrix by typing in a list of numbers enclosed in square brackets. You
can separate the numbers by commas or by spaces or combine the two techniques in the
same matrix definition. To indicate a new row, you can use a semicolon:

a = [2.5,1.1]; or a = [2.5 1.1]; or a = [2.5, 1.1]; b = [-1, 0, 2; 1, 3, 0; -7, 0, 2];

 If there are too many numbers in a row to fit on one line, you can continue the statement
on the next line, but a comma and an ellipsis (…) are required at the end of the line,
indicating that the row is to be continued.

a=[2, 4, 8, 26, 197, 3, 90, 38, 28, 221, 234, 15, 97, 83, 90];
or
a=[2, 4, 8, 26, 197, 3, 90, 38, 28, ...,
221, 234, 15, 97, 83, 90];

 You can define a matrix in terms of another matrix that has already been defined.
a=[1, 4, 12], b=[2, 6, 9], c=[a; b]
→ c=
1 4 12
2 6 9

 We can change values in a matrix, or include additional values, by using an index


number to specify a particular element.
a=[2, 3, 4, 5]

a(3)=-7: changes the third value in the matrix a from 4 to -7.


a(3)=-7 → a=[2,3,-7,5]

a(5)=12: extends the matrix a by defining a fifth element.


a(5)=12 → a=[2 3 4 5 12]

If we define element a(9) = 1.4; matrix a will have eight values, and the values of a(5),
a(6), a(7), and a(8) will be set to 0
a(9)=1.4 → a=[2, 3, 4, 5, 0, 0, 0, 0, 1.4]

P a g e | 29
2. Extracting data from matrices

a=[2, 3, 4, 5; 8, 7, 6, 5; -3, 5, -2, 1]

x=a(r, :) : extracts row r from matrix a.


x=a(2, :) → a=[8 7 6 5]

x=a(:, c) : extracts column c from matrix a.


x=a(:,2) → a=[ 3; 7; 5]

x=a(r1:r2, :) : extracts from row r1 to row r2 from matrix a.


x=a(2:3, :)
→x =
8 7 6 5
-3 5 -2 1

x=a(:, c1:c2) : extracts from column c1 to column c2 from matrix a.


x=a(:,2:4)
→x =
3 4 5
7 6 5
5 -2 1

x=(r1:r2, c1:c2) extracts from row r1 to r2 and from column c1 to column c2 from a
matrix a.
x=a(1:3, 2:4)
→x =
3 4 5
7 6 5
5 -2 1

P a g e | 30
3. Special Matrices
 zeros(m): Creates an m×m matrix of zeros.
a=zeros(2) → a = 0 0
0 0

 zeros(m,n): Creates an m×n matrix of zeros.


a=zeros(2,4) →a =
0 0 0 0
0 0 0 0

 ones(m) Creates an m × m matrix of ones.


a=ones(2)
→a=
1 1
1 1

 ones(m,n) Creates an m × n matrix of ones.


a=ones(2,5)
→ a=
1 1 1 1 1
1 1 1 1 1

 eye(m) Creates an m × m Identity matrix


a = eye(3)
→ a=
1 0 0
0 1 0
0 0 1

 eye(m,n) generates an m × n Identity matrix


a = eye(3,5)
→ a=
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0

 diag(a) Extracts the diagonal of a two-dimensional matrix a.

a=[1,4,5; 2,5,7; 8,9,6] b=[2,3,4,5; 7,6,4,8] (not square ?)


c= diag(a) → c =[1; 5; 6] d=diag(b) → d =
2
6 ?

P a g e | 31
 diag(a)For any vector a, creates a square matrix with a as the diagonal.
a=[-2, 4, 7] b=diag(a)

→b =
-2 0 0
0 4 0
0 0 7

 magic(m) Creates an m×m “magic” matrix.

a= magic(3) → a= 8 1 6
3 5 7
4 9 2

4. Some matrix operations and functions


(') transpose operation: changes the rows of a matrix into columns and the columns into
rows

a=[2,7; 5,3;9,7;-3,1]
b=a' → b=
2 5 9 -3
7 3 7 1

(*) matrix multiplication a*b,


note:
- the number of columns of a must be equal to the number of rows of b
- a*b=b*a (in general)

a= [2,-3,5; -9,7,12;1,-1,8],b=[3,2; -4,5;4,1]


c=a*b → c=
38 -6
-7 29
39 5

d=b*b → ??? Error using ==>m times


Inner matrix dimensions must agree.

P a g e | 32
(^) matrix power.

a=[1,3,2;4,1,6;5,6,5], b=[2,-1,2; 3,-2,1; 6,7,9]


a^2=[23 18 30; 38 49 44; 54 51 71]
a^1.5=
6.4311 - 1.0283i 6.5499 + 2.1311i 8.1435 - 1.1446i
11.6708 + 0.8462i 11.8866 - 4.7337i 14.7785 + 3.1220i
15.9524 + 0.1314i 16.2473 + 2.1244i 20.2002 - 1.6072i

b^2=[13 14 21; 6 8 13; 87 43 100]


b^1.5=
2.6502 + 0.0000i 3.5656 - 0.0000i 6.6393
2.7926 + 0.0000i -0.2302 - 0.0000i 4.0222 - 0.0000i
26.6625 + 0.0000i 14.6660 - 0.0000i 29.6815 + 0.0000i

Note:
Raising a matrix c to the matrix power (^)of two is different from raising c to the array
power (.^) of two.

c=[3, 5, 2; 7, 2.4, 1.3; 5.5, 6, 2.8]


c^2=
55.0000 39.0000 18.1000
44.9500 48.5600 20.7600
73.9000 58.7000 26.6400

c.^2=
9.0000 25.0000 4.0000
49.0000 5.7600 1.6900
30.2500 36.0000 7.8400

inv or ^-1 determines the inverse of a square matrix


Note: a*inv(a)=inv(a)*a=eye(a) , the determinant of a must be not equal zero.

a=[1,3,2;4,1,6;5,6,5], b=[2,-1,2; 3,-2,1]


c = inv(a)
→ c=
-0.8378 -0.0811 0.4324
0.2703 -0.1351 0.0541
0.5135 0.2432 -0.2973

OR c = a^-1

d = inv(b)
→ d =??? Error using ==> inv Matrix must be square.

P a g e | 33
5. Solution of Linear equations
Given:

𝑎11 𝑥 + 𝑎12 𝑦 + 𝑎13 𝑧 = 𝑟1


𝑎21 𝑥 + 𝑎22 𝑦 + 𝑎23 𝑧 = 𝑟2
𝑎31 𝑥 + 𝑎32 𝑦 + 𝑎33 𝑧 = 𝑟3

or in matrix form Ax= r, with


𝑎11 𝑎12 𝑎13 𝑥 𝑟1
𝐀 = [𝑎21 𝑎22 𝑎23 ] , 𝐱 = [𝑦] , 𝐫 = [𝑟2 ]
𝑎31 𝑎32 𝑎33 𝑧 𝑟3

Three methods for solution:

a. Solution Using the Matrix Inverse

x = inv(A)*r

to be avoided since it can result excessive round-off errors

a=[2,3,-2;-1,-1,-1;1,-2,3], r=[-5;-4;13]

x = inv(a)*r → x = [2, -1,3]

b. Solution Using Matrix left Division

x = A\r

a=[2,3,-2;-1,-1,-1;1,-2,3], r=[-5;-4;13]
x = a\r → x=
2.0000
-1.0000
3.0000

P a g e | 34
c. Solution Using the Reduced Row Echelon Function

b = [a,r] (augmented matrix, expanded matrix)

rref(b)

a=[2,3,-2;-1,-1,-1;1,-2,3], r=[-5;-4;13]
b = [2, 3, -2, -5; -1, -1, -1, -4; 1, -2, 3, 13]
rref(b) →
1 0 0 2
0 1 0 -1
0 0 1 3

MATLAB is capable of solving problems which are either overdefined or underdefined


using left division.

a=[3,2,5;4,5,-2;1,1,1;2,-4,-7], r=[22;8;6;-27]
x=a\r →
1.0000
2.0000
3.0000

If the system is underdefined then MATLAB solves the problem by setting some variables
equal to zero.
a=[2,5,3;6,12,8], r=[18;8]
→ ans =
-29.3333
15.3333
0

a=[3,2,5,7;4,5,-2,3], r=[20;12]
→ ans =
0
0.8276
0
2.6207

P a g e | 35

You might also like