VIETNAM NATIONAL UNIVERSITY HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY Ho Chi Minh city, July 2020 LINEAR ALGEBRA (MT 1008) ____________________________ MATLAB PROJECT ____________________________ HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ GROUP MEMBER: 1.Nguyễn Thanh Tùng 1951110 2.Tạ Nguyễn Thành Tài 1951092 3.Đặng Hoàng Việt 1951023 4.Trần Quốc Huy 1951005 5.Lê Hoàng Anh Tài 1951091 6.Lê Thanh Đạt 1951035 7.Nguyễn Vĩnh Hảo 1951049 8.Nguyễn Bảo Khang 1951061 9.Nguyễn Khiêm 1951151 10.Trần Tân Thịnh 1951206 ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 1 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ CONTENTS 1. Problem 1 1.1 Theorem………………………………………...4 1.2 Problem………………………………………....5 1.3 MATLAB code...……………………………….7 2. Problem 2 2.1 Theorem…………………………………………9 2.2 Problem …....……………………………………9 2.3 MATLAB code.……………………..…….…….11 3. Problem 3 3.1 Theorem………………………………………….12 3.2 Problem...………………………………………...12 3.3 MATLAB code…..……………………………....13 ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 2 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 3 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ PROBLEM 1: 1.1 THEOREM: A Markov chain is a dynamical system whose state vectors at a succession of equally spaced times are probability vectors and for which the state vectors at successive times are related by an equation of the form x(k + 1) = Px(k) in which 𝑃 = [𝑃𝑖𝑗 ] ] is a stochastic matrix and 𝑃𝑖𝑗 is the probability that the system will be in state i at time t = k + 1 if it is in state j at time t = k. The matrix P is called the transition matrix for the system. In a Markov chain with an initial state of x(0), the successive state vectors are x(1) = Px(0), x(2) = Px(1), x(3) = Px(2), x(4) = Px(3), . . . For brevity, it is common to denote x(k) by xk, which allows us to write the successive state vectors more briefly as 𝑥1 = 𝑃𝑥0 , 𝑥2 = 𝑃𝑥1 , 𝑥3 = 𝑃𝑥2 , 𝑥4 = 𝑃𝑥3 . . .. Alternatively, these state vectors can be expressed in terms of the initial state vector 𝑥0 as 𝑥1 = 𝑃𝑥0 , 𝑥2 = 𝑃(𝑃𝑥0 ) = 𝑃2 𝑥0 , 𝑥3 = 𝑃(𝑃2 𝑥0 ) = 𝑃3 𝑥0 , 𝑥4 = 𝑃(𝑃3 𝑥0 ) = 𝑃4 𝑥0 . . .. from which it follows that 𝑥𝑘 = 𝑃𝑘 𝑥𝑜 A stochastic matrix P is said to be regular if P or some positive power of P has all positive entries, and a Markov chain whose transition matrix is regular is said to be a regular Markov chain If P is the transition matrix for a regular Markov chain, then: (a) There is a unique probability vector q with positive entries such that Pq = q. (b) For any initial probability vector 𝑥𝑜 , the sequence of state vectors 𝑥𝑜 , 𝑃𝑥𝑜 , . . . . ., 𝑃𝑘 𝑥𝑜 , . . .. converges to q. (c) The sequence 𝑃, 𝑃2 , 𝑃3 ,…., 𝑃𝑘 , . . .. converges to the matrix Q each of whose column vectors is q. The vector q is called the steady-state vector of the Markov chain. Because it is a nonzero vector that satisfies the equation Pq = q, it is an eigenvector corresponding to the eigenvalue λ = 1 of P. Thus, q can be found by solving the linear system (I − P )q = 0 ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 4 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ 1.2 PROBLEM: a. First we find the stochastic matrix for this Markov chain. The ith column of this matrix is the probability vector for the next observation given that the mouse was last observed in room i : Next we use command for … end to calculate 𝑃, 𝑃2 , 𝑃3 , … and repeat the loop until all entries in the matrix are positive (checking by using all ({matrix name} > 0 )) it will stop (using break) and inform at what number of power matrix P is regular : b. As the mouse is initially placed in room 4, the initial state of the mouse is given by the probability vector: ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 5 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ The state vector for the mouse after 1 step is Px, afer 2 steps 𝑃(𝑃𝑞0 ) = 𝑃2 𝑞0 ,… Thus, the state vector for the mouse after 5 steps is : c. Basically, we solve the linear system Pq = q and we can directly compute the eigenvector with eigenvalue one by computing the null space of the matrix {transition matrix} - eye(9) (eye(9) is the MATLAB notation for a 9-by-9 identity matrix). And sum all entries in steady-state vector must be equal 1, then we have to divide the result above by it sum. ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 6 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ 1.3 MATLAB CODE: clear; clc; format rat; % a: A = [2 3 2 3 4 3 2 3 2] + 1; B = 1./A; P = zeros(9); P(1,1)=B(1);P(2,1)=B(1);P(4,1)=B(1); P(1,2)=B(2);P(2,2)=B(2);P(3,2)=B(2);P(5,2)=B(2); P(2,3)=B(3);P(3,3)=B(3);P(6,3)=B(3); P(1,4)=B(4);P(4,4)=B(4);P(5,4)=B(4);P(7,4)=B(4); P(2,5)=B(5);P(4,5)=B(5);P(5,5)=B(5);P(6,5)=B(5);P(8,5)=B(5); P(3,6)=B(6);P(5,6)=B(6);P(6,6)=B(6);P(9,6)=B(6); P(4,7)=B(7);P(7,7)=B(7);P(8,7)=B(7); P(5,8)=B(8);P(7,8)=B(8);P(8,8)=B(8);P(9,8)=B(8); P(6,9)=B(9);P(8,9)=B(9);P(9,9)=B(9); disp('a.') disp('Matrix of transition probabilities P :') disp(P) C = P; for i = 1:1000 C=P^i; if all(C > 0); break end end fprintf('P^%.f =',i), disp(' '), format short; disp(C) fprintf('Since all entries in P^%.f are positive, matrix P is regular',i) disp(' ') disp(' ') %b disp('b. ') q0 = zeros(9,1); q0(4) = 1; disp('Initial state vector :') disp('q0 =') disp(q0) fprintf('The probability that the mouse stays at room 4 : ') disp(q0(4)) for i = 1 : 5; qi = P*q0; q0 = qi; end disp('q5 =') ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 7 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ disp(qi) fprintf('The probability that the mouse stays at room 9 after 5 steps : ') disp(qi(9)) %c disp('c.') null(P - eye(9),'r'); q = ans/sum(ans) disp('Steady-state vector: ') disp('q =') format rat; disp(q) ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 8 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ PROBLEM 2: 2.1 THEOREM The Gram_Schmidt Process: We define the projection operator by <𝑣,𝑢> Proju(v) = <𝑢,𝑢> 𝑢 where <u,v> denotes the inner product of the vectors u and v. This operator projects the vector v orthogonally onto the line spanned by vector u. If u = 0, we define proju(v)=0. i.e., the projection map proju(v)=0 is the zero map, sending every vector to the zero vector. The Gram–Schmidt process then works as follows: The sequence u1, ..., uk is the required system of orthogonal vectors, and the normalized vectors e1, ..., ek form an orthogonal set. The calculation of the sequence u1, ..., uk is known as Gram–Schmidt orthogonalization, while the calculation of the sequence e1, ..., ek is known as Gram–Schmidt orthogonalization as the vectors are normalized 2.2 PROBLEM “function U = Gram_Schmidt(V1)” Using this function to input V1 which is a basis in the space Rn including these vectors you input “V = transpose(V1); ” Transposing these vector V=V1T “U(:,1) = V(:,1)/sqrt(V(:,1)'*V(:,1));” ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 9 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ Calculating the orthogonal basis of column 1 “for i = 2:b U(:,i) = V(:,i);” Because u1 = v1, take I runs starting from 2 to b ”for j = 1:i-1 U(:,i) = U(:,i)-( U(:,j)'*U(:,i) )/( U(:,j)'*U(:,j) )*U(:,j); end” <𝑣2,𝑢1> <𝑣3,𝑢1> <𝑣3,𝑢2> And then u2= v2- <𝑢1,𝑢1> u1 , u3=v3 - <𝑢1,𝑢1>u1 - <𝑢2,𝑢2>u2 … continue for b steps (Calculate the orthogonal basis) “U(:,i) = U(:,i)/sqrt(U(:,i)'*U(:,i));” Calculating the orthonormal basis • Example : ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 10 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ 2.3 MATLAB CODE function U = Gram_Sch(V1) V = transpose(V1) a = size(V,1); b = size(V,2); U = zeros(a,b); U(:,1) = V(:,1)/sqrt(V(:,1)'*V(:,1)); disp('Orthogonal basis: ') disp(V(:,1)); for i = 2:b U(:,i) = V(:,i); for j = 1:i-1 U(:,i) = U(:,i)-( U(:,j)'*U(:,i) )/( U(:,j)'*U(:,j) )*U(:,j); end disp(U(:,i)); U(:,i) = U(:,i)/sqrt(U(:,i)'*U(:,i)); end disp('Orthonormal basis: '); disp('Each column is the orthonormal basis of the space spanned by these vectors you input') end ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 11 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ PROBLEM 3: 3.1 THEOREM: Step 1: Choose a n × n matrix with integer entries 𝑎11 ⋯ 𝑎1𝑛 ⋱ ⋮ ] A= [ ⋮ 𝑎𝑛1 ⋯ 𝑎𝑛𝑛 to perform the encoding. Certain additional conditions on A will be displayed later. Step 2: Group successively plaintext letters into groups of n letters, adding one or some abitrary "special" letters to fill out the last group if the plaintext does not have enough letters to group, and replace each plaintext letter by its numerical value. Step 3: Successively convert each plaintext group 𝑝1 𝑝2... 𝑝𝑛 into a column vector: 𝑝1 p= [ ⋮ ] 𝑝𝑛 and form the product Ap. We will call p a plaintext vector and Ap the corresponding ciphertext vector. A union of column vectors can be regrouped into a Source matrix. Step 4: Convert each ciphertext vector, or matrix into its alphabetic equivalent. 3.2 PROBLEM: a. Step 1: Insert the code which we want to encode s = 'SEND@HIM@MONEY@' Step 2: Convert letters into numbers and make a matrix from these source value S = double(s) -double('A')+1; Step 3: Reform the size of Source matrix: S S = reshape(S,3,[]); Step 4: Input Key matrix: K K = [1 2 3; 1 1 2; 0 1 2]; Step 5: Get Code matrix by multiplying Key matrix and Source matrix: C = KS C = K*S; Step 6: Reform Code matrix to have numerical code series c = reshape(C,1,[]); RESULT: ➔ [71 52 33 28 20 16 35 22 13 85 56 43 55 30 25] ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 12 HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY ________________________________________________________________________ b. Step 1: Insert the code which we want to decode c = [67 44 41 49 39 19 113 76 62 104 69 55]; Step 2: Reform the size of Code matrix: C C = reshape(c,3,[]); Step 3: Input Key matrix: K K = [1 2 3; 1 1 2; 0 1 2]; Step 4: Find the inverse Key matrix: K −1 Step 5: Multiplying inverse Key matrix and Code matrix to find the Source matrix : S = K −1 C S = inv(K)*C; Step 6: Get the aphabetical code by reforming Source matrix and convert numbers into letters s = char(S - 1 + double('A')); RESULT: ➔ CERTAINLYNOT 3.3 MATLAB CODE: a. s = 'SEND@HIM@MONEY@' %Question 3a S = double(s) -double('A')+1; %Take numbers series from string S = reshape(S,3,[]); K = [1 2 3; 1 1 2; 0 1 2]; C = K*S; c = reshape(C,1,[]); disp(c) b. c = [67 44 41 49 39 19 113 76 62 104 69 55]; %Question 3b C = reshape(c,3,[]); K = [1 2 3; 1 1 2; 0 1 2]; S = inv(K)*C; s = char(S - 1 + double('A')); %From numbers series we can have string s = reshape(s,1,[]); disp(s) ________________________________________________________________________________________ Matlab Project – Linear algebra (MT 1008) - Semester 2 - 2019 – 2020 Page 13