BAHCESEHIR UNIVERSITY
MAT3012 NUMERICAL ANALYSIS
(LAB APPLICATOINS)
Week-7, Fall
Resch. Asst. Aysun Soysal
LAB-7 OBJECTIVES
• Vectors
• Matrices
• Solving System of Linear Equations
2
1. VECTORS
▪ Row vectors can be defined by giving the list of entries separeted by either commas or spaces
and enclosed in square brackets.
>> v=[1,2,3,4,5]
v=
1 2 3 4 5
>> v=[1 2 3 4 5]
v=
1 2 3 4 5
3
▪ We can do certain arithmetic operations on vectors with the same length.
>> v=[1 2 3 4 5];
>> w=[6 7 8 9 10];
>> v+w
ans =
7 9 11 13 15
▪ Column vectors can be defined by giving the list of entries separeted by semicolon and
enclosed in square brackets.
4
>> v=[1;2;3;4;5]
v=
1
2
3
4
5
▪ We can convert a row vector into a column vector and vice versa by transposing:
>> v=[1;2;3;4;5]
v=
1
2
3
4
5
>> v'
ans = 5
1 2 3 4 5
▪ Scalar product and dot product/ division/ power of two vectors respectively:
>> a=[2 4 7 9]; % row vector
>> b=[3;5;8;1]; % column vector
>> a*b % scalar product
ans =
91
>> a=[2 4 7 9]; % row vector
>> c=[1 3 5 7]; % row vector
>> a.*c % dot product
ans =
2 12 35 63
>> a./c % dot division
ans =
2.0000 1.3333 1.4000 1.2857
>> a.^3 % dot power
ans = 6
8 64 343 729
1.1. Some Special Vectors
>> a=ones(1,7)
a=
1 1 1 1 1 1 1
>> b=zeros(4,1)
b=
0
0
0
0 7
▪ We can build up vectors from existing ones:
>> w=[1 2 3];
>> v=[8 9];
>> d=[2*v, -w]
d=
16 18 -1 -2 -3
>> r=[1:2:6, -1:-2:-7]
r=
1 3 5 -1 -3 -5 -7
8
▪ We can extract bits of vectors:
>> r=[1:2:6, -1:-2:-7]
r=
1 3 5 -1 -3 -5 -7
>> t=r(3:5)
t=
5 -1 -3
▪ We can sort the elements of a vector:
>> r=[1:2:6, -1:-2:-7]
r=
1 3 5 -1 -3 -5 -7
>> b=sort(r)
b=
-7 -5 -3 -1 1 3 5
9
▪ We can find maximum and minimum element of a vector:
>> r=[1:2:6, -1:-2:-7]
r=
1 3 5 -1 -3 -5 -7
>> m=max(r)
m=
5
>> n=min(r)
n=
-7
▪ We can find Euclidean norm and maximum norm of a vector:
r=
1 3 5 -1 -3 -5 -7
>> norm(r)
ans =
10.9087
>> norm(r,inf) 10
ans =
7
2. MATRICES
▪ In order to define a matrix in MATLAB, we enter the numbers row by row; rows must be
separeted by semicolons:
>> A=[2 4 6;4 7 9;1 2 3]
A=
2 4 6
4 7 9
1 2 3
▪ We can do certain arithmetic operations with matrices of the same size:
>> B=[-3 -5 -8;3 5 1; 11 13 14];
>> A=[2 4 6;4 7 9;1 2 3];
>> A+B
ans =
-1 -1 -2
11
7 12 10
12 15 17
>> 2*A
ans =
4 8 12
8 14 18
2 4 6
>> A
A=
2 4 6
4 7 9
1 2 3
>> C
C=
3 4 6
5 7 9
>> A*C
Error using * 12
Inner matrix dimensions must agree.
▪ Transpose of a matrix:
>> C
C=
3 4 6
5 7 9
>> C'
ans =
3 5
4 7
6 9
13
▪ In order to get the dimensions of the matrix:
>> C=[3 4 6;5 7 9];
>> size(C)
ans =
2 3
▪ The colon notation can be used in generation of matrices:
>> D=[1:5; 6:10; 11:2:20]
D=
1 2 3 4 5
6 7 8 9 10
11 13 15 17 19 14
2.2 Some Special Matrices
>> A=zeros(2,3)
A=
0 0 0
0 0 0
>> B=ones(4,2)
B=
1 1
1 1
1 1
1 1 15
>> I=eye(3) % identity matrix
I=
1 0 0
0 1 0
0 0 1
▪ To construct a diagonal matrix:
>> d=[-3 4 5];
>> D=diag(d)
D=
-3 0 0
0 4 0
16
0 0 5
>> F=[0 1 8; 3 -2 -4 ; 4 2 1]
F=
0 1 8
3 -2 -4
4 2 1
>> diag(F)
ans =
0
-2
1
17
▪ Building up Matrices:
>> x=[8; -4 ;1 ]
x=
8
-4
1
>> C=[0 1 ; 3 -2 ; 4 2]
C=
0 1
3 -2
4 2
>> G=[C x]
G=
0 1 8
3 -2 -4
4 2 1 18
>> J=[1:4; 5:8; 9:12; 20 0 5 4]
J=
1 2 3 4
5 6 7 8
9 10 11 12
20 0 5 4
>> K=[diag(1:4) J ; J’ ones(4,4)]
K=
1 0 0 0 1 2 3 4
0 2 0 0 5 6 7 8
0 0 3 0 9 10 11 12
0 0 0 4 20 0 5 4
1 5 9 20 1 1 1 1
2 6 10 0 1 1 1 1
3 7 11 5 1 1 1 1
4 8 12 4 1 1 1 1
19
▪ Extracting bits of matrices:
>> J=[1:4; 5:8; 9:12; 20 0 5 4]
J=
1 2 3 4
5 6 7 8
9 10 11 12
20 0 5 4
>> J(1,1)
ans =
1
>> J(4,3)
ans =
5 20
>> J(:,3)
ans =
3
7
11
5
>> J(2,:)
ans =
5 6 7 8
>> J (:,2:3)
ans =
2 3
6 7
10 11
0 5
>> J(2:3,2:3)
ans =
6 7 21
10 11
3. SOLVING SYSTEM OF LINEAR EQUATIONS
Ex1. Solve in MATLAB the system of following equations:
𝑥2 + 𝑥3 = 2
2𝑥1 − 𝑥2 − 𝑥3 = 0
𝑥1 + 𝑥2 − 𝑥3 = 1
22
Solution.
>> A=[0 1 1;2 -1 -1;1 1 -1]
A=
0 1 1
2 -1 -1
1 1 -1
>> b=[2;0 ;1]
b=
2
0
1
>> x=inv(A)*b % by using matrix inversion method
x=
1
1
1
>> y=A\b % by using left division method
y=
1.0000 23
1.0000
1.0000
Ex2: Consider the system of the following equations:
𝐴𝑋 = 𝐵
where 𝐴: hilbert matrix with size 10 × 10 and 𝐵 = ones(10,1) matrix.
a) Solve the given sytem by using matrix inversion method
b) Determine the errors in matrix inversion method
c) Solve the given sytem by using left division method
d) Determine the errors in left division method
24
Solution:
>> A=hilb(10);
>> b=ones(10,1);
>> x_aprox=inv(A)*b % (a ) by using matrix inversion method
x_aprox =
1.0e+06 *
-0.0000
0.0010
-0.0238
0.2397
-1.2772
3.6450
-7.0979
6.7051
-3.9883
25
0.9237
>> x_true=invhilb(10)*b % exact solution by invhilb buit-in function
x_true =
-10
990
-23760
240240
-1261260
3783780
-6726720
7001280
-3938220
923780
>> err_inv=norm(x_true - x_aprox, Inf)/norm(x_true, Inf) % (b)
err_inv =
0.0530 26
>> x_aprox2=A\b % (c) by using left division method
x_aprox2 =
1.0e+06 *
-0.0000
0.0010
-0.0238
0.2402
-1.2611
3.7834
-6.7261
7.0007
-3.9379
0.9237
>> err_left=norm(x_aprox2 - x_true,Inf)/norm(x_true,Inf) % (d )
err_left =
27
9.1573e-05
THANK YOU FOR YOUR ATTENTION !
28