辅导案例-PROJECT 3

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
MATLAB PROJECT 3
Please read the Instructions located on the Assignments page prior
to working on the Project.

BEGIN with creating a Live Script Project3.
Note: All exercises in this project have to be completed in the Live Script using the Live
Editor. Please refer to the MATLAB video that explains how to use the Live Script:
https://www.mathworks.com/videos/using-the- live-editor-117940.html?s_tid=srchtitle
The final script has to be generated by exporting the Live Script to PDF.

Each exercise has to begin with the line
Exercise#
You should also mark down the parts such as (a), (b), (c), and etc. This makes grading easier.
Important: we use the default format short for the numbers in all exercises unless it is
specified otherwise. We do not employ format rat since it may cause problems with
running the codes and displaying matrices in the Live Script. If format long had been used
in an exercise, please make sure to return to the default format in the next exercise.
Part I. Subspaces & Bases

Exercise 1 (4 points) Difficulty: Moderate
In this exercise, you will create a function which will compare the Row Spaces of two
matrices A and B. First, it will determine whether the Row A and Row B are subspaces of the
same space n

. If yes, your code will check if the dimensions of Row A and Row B are the
same and, if it is true, whether Row A = Row B. Obviously, when two subspaces have the
same dimension, it might not be true that they are the same set. For example, a line through
the origin in 3

is a one-dimensional subspace of 3

, but two lines might be different sets.
Use a MATLAB function rank within your code. Remember, the rank of a matrix can be
defined as the dimension of the row space of the matrix.

**Create a function in the file that begins with
function []=rowspace(A,B)

First, your function has to check if Row A and Row B are subspaces of the same vector space
n

.
**If Row A and Row B are subspaces of the different spaces, we output the corresponding
message and terminate the program.
**If Row A and Row B are subspaces of the same vector space n

, you will code the
corresponding message which should also output the dimension of the vector space n

, which
is n. An example of an output message is below:
fprintf('Row A and Row B are subspaces of R^%i\n',n)
**Then, your function will continue with calculating the dimensions of Row A and Row B.
Output them with the corresponding messages.
**Next, check if both conditions hold: Row A is the whole n

and Row B is the whole n

. If
it is the case, output a message that Row A = Row B = n

and terminate the program.
**If either Row A or Row B or both are not the whole n

, your code will continue.
First, it will check if Row A and Row B have the same dimension. If not, output the message
that Row A and Row B have different dimensions and cannot be equal.
**If the dimensions are the same, the code has to check whether Row A = Row B.
If it is not the case, output a message that the dimensions of Row A and Row B are the same
but Row A ~= Row B.
Otherwise, if Row A = Row B, output the corresponding message. Also, find a basis for
RowA (or Row B). The vectors in the basis have to form the columns of a matrix P. Output P
with the message: “a basis for Row A (or Row B) is” and display P.

**Type the function rowspace in your Live Script.

**Input a matrix C in your Live Script:
C=[2 6 2 4 -6;-4 -9 -7 -2 3;-2 -5 -3 -2 3;3 8 9 -1 4]
**Run the function rowspace(A,B)on the following choices of the matrices (a)-(f):
(a) A=C, B=rref(C)
(b) A=A', B=B'
%Analyze the output for part (b) and write a comment in your Live Script on a possible effect
of elementary row operations on the column space of a matrix.
(c) A=[C;zeros(1,5)], B=rref(C)
(d) A=eye(3);B=eye(4);
(e) A=magic(4);B=eye(4);
(f) A=magic(5);B=eye(5);
BASES FOR COLUMN SPACE, ROW SPACE AND NULL SPACE

Exercise 2 (6 points) Difficulty: Moderate
In this exercise, you will create bases for the Col A, Null A, and Row A in various ways by
using built-in MATLAB functions: colspace() and null( ,'r'), as well as the functions
that you will create yourself: shrink and noll . You will also investigate the properties of
the subspaces associated with the matrix A.

Part 1: Column Space
(1) The column space of a matrix A is a subspace spanned by the columns of A. It might
happen that the set of columns of A is not linearly independent, that is, not all columns of A
will be in a basis. The set of columns of A can be “shrunk” into a basis for Col A by using the
function shrink. Here is a code:

function B=shrink(A)
[~,pivot]=rref(A);
B=A(:,pivot);
end
**Create the function B=shrink(A)in a file.
**Type the function shrink in your Live Script.
**Input a matrix
A=magic(4)
Run the command
rref(A)
Then, run the two lines given below (display the outputs):
[r,pivot]=rref(A)
B=A(:,pivot)
%Write a comment in the Live Script on the outputs for each of the two lines above.

**Run the function B=shrink(A)
%Explain in the Live Script a reason why the set of the columns of B forms a basis for the
column space of A.

(2) There is a MATLAB built-in function colspace(sym(A)) which also creates a matrix
whose columns form a basis for the column space of A.
**Type and run in the Live Script:
R=rref(transpose(A)), M=colspace(sym(A))
%Analyze the outputs R and M and explain in your Live Script which pass the function
colspace takes to generate a matrix whose columns form a basis for the Col A.

Part 2: Null Space and Row Space
(1) **Create the function that begins with
function [C,p]=noll(A)
n=size(A,2);

This function will be working with the Null space of a matrix A. It has to be generated from
the function homobasis created in Project1 by removing some “extra” commands and
modifying the output messages. The outputs for the function noll have to be: a matrix C, the
dimension of the Null space p, and the messages as indicated below:

**Case 1: If Null A ={0}, the output matrix C will be the column vector of zeros of the
length n , for the dimension p we assign a 0, and the output message has to indicate that the
null space is the zero subspace of R^n (the value for n has to be displayed – please use the
fprintf command).
**Case 2: If Null A ≠{0}, the output has to be a matrix C, whose columns form a basis for the
Null space of A. You will also calculate and outputs the dimension p of the Null A. The two
outputs have to be supply with the messages: one has to indicate that Null A is a p-
dimensional subspace of R^n” (the values of p and n have to be displayed in your message);
and the other message has to state that a basis for Null A is formed by the columns of the
matrix C, which has to be displayed.
**Type the function noll in your Live Script.
**Input the matrix
A=magic(5)
and run the function
[C,p]=noll(A)
Your outputs should match the ones for Case 1.
**Next, run the functions given below in the indicated order on each of the matrices (a)-(d):
[C,p] = noll(A);
N=null(A,'r')
isequal(C,N)
(a) A=[2 -4 -2 3; 6 -9 -5 8; 2 -7 -3 9; 4 -2 -2 -1; -6 3 3 4]
(b) A=magic(4)
(c) A=ones(5)
(d) A=[magic(4),ones(4,1)]
%Analyze the outputs, which should match the ones for Case 2, and write a comment in the
Live Script how the function null(A,'r')works to generate a basis for Null A. (You may
also need to run the command help null in the Command Window in MATLAB – do not
include it in your Live Script.)

(2) Next, input the matrix in the Live Script:
A=[2 -4 -2 3; 6 -9 -5 8; 2 -7 -3 9; 4 -2 -2 -1; -6 3 3 4]
and complete the tasks outlined below:
**Use the function colspace() (with some adjustments) to output a basis for the Row A.
Your output will be in the format of a matrix.
**Calculate and output the dimension of the Row A, q.
**Calculate the dimension n of the vector space R^n, of which the Row A is a subspace.
** Use the Rank Theorem to calculate and output the dimension, d, of the Null A.
**Run the function
[C,p] = noll(A);
**Verify by using a logical statement that the output p of the function noll(A) matches the
dimension d of the Null A calculated by the Rank Theorem.
Bonus! (1 point)
**Demonstrate using the matrix A that the Row A and Null A are orthogonal complements of
each other (to the whole n

), that is, not only their dimensions sum to n (according to the
Rank Theorem), but also every vector in a spanning set (or in a basis) for the Row A is
orthogonal to every vector in a basis for the Null A.
Hint: two vectors u and v are orthogonal if and only if dot(u,v)=0. You can also employ a
matrix multiplication to verify that Row A and Null A are orthogonal complements of each
other (recall a definition of the dot product discussed in Project 0).
Part II. Isomorphism & Change of Basis

Exercise 3 (5 points) Difficulty: Hard
In this exercise, you will be given a set of n polynomials – it will be denoted a set B. The
polynomials in B are from the subspace 1nP − of the polynomials whose degrees do not exceed
( 1n − ). The standard basis for 1nP − is { }2 11, , , ... , nE t t t −= . You will determine whether the
given set B forms a basis for 1nP − . This could be done by employing isomorphism from 1nP −
onto n

. You will create a matrix P, which is the matrix of the E-coordinates of the
polynomials in B. According to the isomorphism, the polynomials in B form a basis for a
subspace 1nP − if and only if the columns of P form a basis for n .
For a help with this exercise, please refer to the lecture “Module 19”.
If the set B is a basis for 1nP − , your function will continue with two more tasks: (1) you will
find a vector y of the B-coordinates of a polynomial Q, with Q given in a symbolic form
through the standard basis; and (2) you will output a polynomial R in a symbolic form
(through the standard basis), given the vector r of the B-coordinates of R.
We will be using the function B=closetozeroroundoff(A,p)with the parameter p = 7 within
the code. This function was created in Project 0 and you should have it in your Current Folder
in MATLAB.

**Create a function in a file that begins with
function P=polyspace(B,Q,r)
format

An input [ ](1), (2), ... , ( )B B B B n= is a vector whose components are polynomials from the
vector space 1nP − , Q is a single polynomial from the same space 1nP − , and r is a numerical
vector with n entries.

Note on the format of the input polynomials: for the purpose of this program, it is required
that the coefficient of the leading term 1nx − of a polynomial must not be zero. However, the
zero leading coefficient is accepted by the definition of the subspace 1nP − , and some of the
given polynomials do not have term 1nx − , that is, the coefficient of 1nx − is a zero. To be able
to work with such polynomials, we insert the coefficient 10^(-8) of 1nx − and we will convert
that leading coefficient into a 0 by running the function closetozeroroundoff with p = 7 on
the matrix P within the code.

**Continue your function polyspace with the commands:
u=sym2poly(B(1));
n=length(u);

The command sym2poly(B(1)) takes the coefficients of the polynomial B(1), which is
written through the standard basis in the descending order according to the degree, and writes
them as a row vector (a 1 n× matrix).
Note: The number n is the dimension of the vector space 1nP − ; thus, 1nP − is isomorphic to the
Euclidean space n

. The number n will be used later in this program.

**To use isomorphism, you will create an n n× matrix P whose columns are the vectors of the
coefficients of the polynomials in the set B.
Hint: to output P, you can employ a “for” loop and the commands sym2poly and transpose
in your code.
Then, you will convert to 0 the entries that are “closer” to zero than 10^(-7) by running the
function
P=closetozeroroundoff(P,7)

**Output P with the message:
fprintf('matrix of the E-coordinate vectors of the polynomials in B is\n')
and display P.
**Then you will check if the columns of P form a basis for n

- use the command rank.
**If P is not a basis, the program calculates and outputs matrix A=rref(P), which is the
reduced echelon form of the matrix P; matrix A should visualize the fact that the columns of P
do not form a basis for n

. Your outputs will be supplied with the messages. The examples of
the output messages are given below:
sprintf('the polynomials do not form a basis for P%d',n-1)
fprintf('the reduced echelon form of P is\n')
and output matrix A.

**If P is a basis for n

, create a message indicating that the polynomials in B form a basis for
the subspace of the polynomials 1nP − , and your function will continue with two more tasks:
(1) Given the polynomial Q written in a symbolic form through the standard basis E, calculate
the B-coordinate vector y of Q.
Hint: the MATLAB command sym2poly will output the row vector of the E-coordinates of
the polynomial Q. To find a vector y of B-coordinates of the polynomial Q, you will need to
use the Change-of-Coordinate equation. Please make sure that you will also use the function
closetozeroroundoff in your code with p = 7 to convert the leading coefficient of Q to a 0
when needed.
Your output for this part should contain a message and the vector y. The message may have a
form:
fprintf('the vector of coordinates of Q with respect to the basis B is\n')
and output vector y.

(2) Given the B-coordinate vector r of a polynomial R, calculate the coordinate vector z of the
polynomial R with respect to the standard basis (do not display z). Then, output the
polynomial R written in a symbolic form through the standard basis.
Hint: the vector z should be calculated by using the Change-of-Coordinate equation. The
command poly2sym applied to the vector z will allow you to get the required polynomial R.
The output R should be supplied with a message, for example:
fprintf('the polynomial whose B-coordinates form the vector r is\n')
and output R.

For a help with this exercise, you may find it useful to review the second Example of the
lecture “Module 19”.
This is the end of the function polyspace.

**Type the functions closetozeroroundoff and polyspace in the Live Script.
**Then, type in the Live Script
syms x
This command will allow you to input the polynomials in the variable x in your Live Script by
typing (or copying and pasting) the variables B and Q given below.
** Run the function P=polyspace(B,Q,r) on each set of the variables (a)-(c). (Display the
inputs in the Live Script.)
(a)
B=[x^3+3*x^2,10^(-8)*x^3+x,10^(-8)*x^3+4*x^2+x,x^3+x]
Q=10^(-8)*x^3+x^2+6*x
r=[2;-3;1;0]
(b)
B=[x^3-1,10^(-8)*x^3+2*x^2,10^(-8)*x^3+x,x^3+x]
Q and r are the same as in (a).
(c)
B=[x^4+x^3+x^2+1,10^(-8)*x^4+x^3+x^2+x+1,10^(-8)*x^4+x^2+x+1,10^(-
8)*x^4+x+1,10^(-8)*x^4+1]
Q=10^(-8)*x^4+3*x^3-1
r=diag(magic(5))
Part III. Matrix Factorization

Exercise 4 (5 points) Difficulty Moderate
In this exercise, you will work with a QR factorization of an m n× matrix. We will proceed in
the way chosen by MATLAB, which is different from the textbook approach.

Theory: first, we will represent an m n× matrix A as a product of unitary (or orthogonal)
m m× matrix Q and an upper-triangular m n× matrix R, A Q R= ∗ . A square m m× matrix Q
is called unitary (or orthogonal) if 1 TQ Q− = , or equivalently, * ( )TQ Q eye m= .

Next, when A is a square m m× matrix, we will use the procedure outlined below to create a
sequence of the similar matrices 1 2, , ,...A A A and use them to approximate the eigenvalues of
A, as it is known from the theory, that similar matrices have the same eigenvalues with the
same multiplicities. You will go through the following steps:
(1) decompose A into a product *Q R and interchange the factors to create a matrix
1 *A R Q= ;
(2) decompose 1A into a product *Q R (with the new matrices Q and R) and interchange the
factors to create the matrix 2 *A R Q= ;
and so on.

For certain square matrices, such as symmetrical, tridiagonal, Hessenberg matrices, this
process produces a sequence of the matrices 1 2, , ,...A A A , all similar to A, that become almost
upper triangular and whose diagonal entries we use to approximate the eigenvalues of A.
**Create a function in MATLAB that begins with:
function [Q,R] = quer(A)
format
[m,n]=size(A);

Part I:
**Generate the first factorization by using a MATLAB in-built function
[Q,R]=qr(A)

Output (and display) the matrices Q and R.
**Then, your function has to verify that you did get the factorization, A Q R= ∗ , that is, you
will verify that the following condition holds:
closetozeroroundoff(A-Q*R,7)==0
If yes, output a message:
'the product of Q and R forms a QR decomposition of A'
If not, output something like:
'No, it cannot be true!'
and terminate the program.

**Next, you will use a conditional statement to verify that Q is unitary (see the Theory above)
and R is upper triangular. If both conditions are satisfied, output a message:
disp('Q*R forms an orthogonal-triangular decomposition of A').
Otherwise, output something like 'What is wrong?!' and terminate the program.

Part II:
** If m = n, that is, A is square, we continue with creating a sequence of the matrices
1 2 3, , ,...A A A , all similar to A, as described in the Theory. You can use the “while” loop to get
the consecutive iterations. To make the code simpler, you can re-assign a consecutive iteration
to A again. Your loop will run until a consecutive iteration (a new A) becomes close to an
upper triangular in a sense that the matrix P=closetozeroroundoff(A-triu(A),7) is a zero
matrix for the first time.
Please make sure that you will suppress all intermediate iterations within the loop!

The condition P=0 will terminate the loop and your outputs have to be as specified below.
Each output has to be supplied with the corresponding message which should indicate its
meaning:
(1) output the matrix B, to which you will assign the last iteration A;
(2) output the number of iterations, k;
(3) output the main diagonal of the matrix B, a vector E, which will be an approximation of
the vector of eigenvalues of A.

**Type the functions closetozeroroundoff and quer in your Live Script.

**Run the function [Q,R] = quer(A) on the following matrices (display the inputs in your
Live Script):
(a) A=randi(10,3,4)
(b) A=ones(5,3)
(c) A=ones(4,4)
(d) A=diag([1,2,3,4])
(e) A=triu(magic(4))
(f) A=tril(magic(4))
(g) A=triu(tril(rand(6),1),-1)
(h) A=[1 1 4;0 -4 0;-5 -1 -8]





Exercise 5 (6 points) Difficulty: Moderate
In this exercise you will work with LU factorization of a matrix.
Theory: Any m n× matrix A can be reduced to an echelon form by using only row
replacement and row interchanging operations. Row interchanging is almost always necessary
for a computer realization because it reduces the round off errors in calculations - this strategy
in computer calculation is called partial pivoting, which refers to selecting for a pivot the
largest by absolute value entry in a column.
The MATLAB command [L,U] = lu(A) returns a permuted lower-triangular matrix L and
an upper-triangular matrix U, such that, *A L U= . The matrix U is an echelon form of a
square matrix A or resembles an echelon form for a general matrix A; the rows of matrix L
can be rearranged (permuted) to a lower-triangular matrix with 1’s on the main diagonal.

To learn more about the LU factorization, please read section 2.5 of the textbook and the
corresponding section in the “Study Guide” which you will find in MyLab and Mastering
under the link “Tools for Success”.

Note: The MATLAB algorithm for LU factorization is somewhat different from the one
presented in Section 2.5 of the textbook. The computer algorithm is better adjusted to the
computational purposes.

PRACTICAL APPLICATIONS OF THE LU FACTORIZATION FOR SQUARE MATRICES

Part 1
When A is invertible, MATLAB uses LU factorization of *A L U= , to find the inverse
matrix by, first, inverting L and U and, then, computing 1 1 1*A U L− − −= .

**Create a function in MATLAB that will begin with the commands:
function [L,U] = eluinv(A)
[~,n]=size(A);
[L,U] = lu(A)

(Output matrices L and U.)
**Verify that A is equal to L*U. If it is the case, display the message:
disp('Yes, I have got LU factorization')
Note: You will need to use the function closetozeroroundoff with p=7 for this check and
for all the other checks that follow.

**Verify that U is an echelon form of A. If it is the case, output a message:
disp('U is an echelon form of A')
If it is not the case, the output message should be something like:
disp('Is anything wrong?')
and terminate the code.

Next, check whether A is invertible or not. Please use the command rank.
**If A is not invertible, output:
sprintf('A is not invertible')
and terminate the program.
**If A is invertible, calculate the inverses of the matrices L and U, invL and invU,
respectively, by applying the row-reduction algorithm to [L eye(n)] and [U eye(n)].
Due to the structure of the matrices L and U, these computations will not take too many
arithmetic operations.
**Calculate and output the inverse matrix of A, invA, using the matrices invL and invU (see
the theory above).
**Run a built-in MATLAB command inv to compute the matrix P=inv(A); display P with
the corresponding message.
**Verify that the matrices invA and P match - you should use the function
closetozeroroundoff with p=7. If invA and P=inv(A) match within the given precision,
display the message:
disp('Yes, LU factorization works for calculating the inverses')
Otherwise, the message should be:
disp('LU factorization does not work for me!?')
**Type the function eluinv in your Live Script.
**Run the function [L,U] = eluinv(A) on the following matrices:
(a) A=[1 1 4;0 -4 0;-5 -1 -8]
(b) A=magic(4)
(c) A=[2 1 -3 1;0 5 -3 5;-4 3 3 3;-2 5 1 3]
(d) A=magic(3)

Part 2
LU factorization can be successfully used for solving simultaneously p matrix equations
( 2p ≥ ) with the same matrix A. This algorithm significantly reduces the number of arithmetic
operations, or “flops” (floating point operations) in comparison with the other methods,
provided L and U are calculated,
Suppose that we have p matrix equations:
1 2, , ... , pA A A= = =x b x b x b
Let 1 2[ ... ]pB = b b b . Solving simultaneously the equations above, we are looking for the
solution of the matrix equation
AX B=
where 1 2[ ... ]pX = x x x is the matrix whose columns are the solutions of the p systems,
respectively, that is,
1 2 1 2 1 2[ ... ] [ ... ] [ ... ]p p pAX A A A A B= = = =x x x x x x b b b .
The first algorithm for finding the solutions is to use the inverse matrix (for an invertible
matrix A):
inv( )X A B= ∗ (1)
The second algorithm for finding the solutions is to use the “backslash” operator:
\X A B= (2)
And the third method is based on the LU factorization of A:
Assume that we have LU factorization of an invertible matrix A: A LU= . The logical chain
below demonstrates the procedure of using LU factorization to find the solution X:
( )
output
&AX B L UX B LY B UX Y X= ⇔ = ⇔ = = ⇒ (3)
To output X, we, first, solve LY B= for Y, and, then, we use Y to solve UX Y= for X.
**Create a function in MATLAB:
function [X,X1,X2] = msystem(A,B)
[~,n]=size(A);
[~,p]=size(B);
[L,U]=lu(A);

The inputs will be an invertible n n× matrix A and n p× matrix B. The line [L,U]=lu(A);
of the code above will produce the matrices of LU factorization of A (we are suppressing this
output).
**First, we will calculate and output the solution X1 according to the algorithm in (1).
**Then, we calculate and output the solution X2 according to the algorithm in (2).
**Last, calculate and output the solution X by using LU factorization. You will proceed in the
following way:
Find the solution Y (which is a matrix!) of the system LY B= using the row-reduction
algorithm – you will employ the command “rref” in your code. Then, find the solution X of
the equation UX Y= (Y is the output from the previous step) also using the row-reduction
algorithm. The output matrix X must have p columns. (Remember, we are solving
simultaneously p matrix equations!)

**Next, run the function closetozeroroundoff with p=7 on each of the matrices (X1-X2)
and (X1-X). If both outputs are the zero matrix, display the message: 'The solutions
calculated by different methods are the same'
Otherwise, output: 'There is a problem with my code?!'

**Type the function msystem in your Live Script.
**Run the function [X,X1,X2] = msystem(A,B) on the following matrices:
(a) A=[1 1 4;0 -4 0;-5 -1 -8], B=randi(10,3,4)
(b) A=magic(3), B=[magic(3),eye(3)]
(c) A=magic(5), B=randi(10,5,3)
Part IV. Application to Markov Chains

In this exercise, you will work with Markov Chains. Please read the part Theory and perform
the tasks indicated below.

Theory: A vector with nonnegative entries that add up to 1 is called a probability vector. A
stochastic matrix is a square matrix whose columns are probability vectors.
Important Note: in this Exercise, the definition of a stochastic matrix matches the definition of
the left-stochastic matrix in Exercise 6 of Project 1.

A Markov chain is a sequence of probability vectors 0 1 2, , ,...x x x , together with a stochastic
matrix P, such that
1 0 2 1 3 2, , , ...P P P= = =x x x x x x .
In other words, a Markov chain is described by the first-order difference equation 1k kP+ =x x
for 0,1, 2,...k =
The n entries of vectors kx list, respectively, a probability that the system is in one the n
possible states. For this reason, kx is called a state vector. If P is a stochastic matrix, then a
steady-state vector for P is a probability vector q such that P =q q .
A stochastic matrix P is called regular if some matrix power kP contains only strictly
positive entries.
Theorem: If P is an n n× regular stochastic matrix, then P has a unique steady-state vector q.
Further, if 0x is any initial state and 1k kP+ =x x for 0,1, 2,...k = , then the Markov chain
{ }kx converges to q as k →∞ .
For more on Markov Chains read the textbook: Section 4.9, Applications to Markov Chains.


Exercise 6 (4 points): Difficulty: Moderate
**Create a function in a file that begins with
function q=markov(P,x0)
format

**First, the function has to check whether the given matrix P, whose entries will be positive
numbers, is stochastic (that is, left-stochastic). If P is not left-stochastic, the program displays
a message “P is not a stochastic matrix“, returns q=[]; and terminates.

**If P is left-stochastic (then it will be a regular stochastic matrix), we do the following:
(1) Find the unique steady-state vector q.
Recall: the steady-state vector q is a probability vector that is a solution of the equation
P =q q , or, equivalently, a solution of ( ( ))P eye n− =q 0 . A rational basis for the solution set
of this system (which has to contain only one vector) can be found as
Q=null(P-eye(n),'r');
where n is a number of rows (or columns) of P. To find the unique steady-state vector q,
which has to be a probability vector, you will need to scale the vector Q: (1/ )*c=q Q , where
( )c sum= Q . Display the vector q with the message that it is the steady-state vector of the
system.
(2) Next, verify that the Markov chain converges to q by calculating consecutive
1 2 1 3 2* 0, * , * , ...P P P= = =x x x x x x , until, for the first time, ( ) 710knorm −− Output the number of iterations k that are required to archive this accuracy – supply your
output with the corresponding message.

**Type the function markov in your Live Script.
**Run the function q=markov(P,x0) on the following matrices P and vectors x0:
(a)
.6 .3
.5 .7
P  =  
 
and
.4
0
.6
 
=  
 
x ,
or, in more convenient form:
P=[.6 .3;.5 .7], x0=[.4;.6]
(b)
P=[.5 .3;.5 .7]
and x0 is the same as in part (a).
(c)
P=[.9 .2;.1 .8], x0=[.12;.88]
where P is a migration matrix between two regions.
(d)
The migration matrix P is the same as in part (c). And the initial vector is
x0=[.86;.14]

% Compare the output vectors q for parts (c) and (d) for the different vectors x0 and write a
comment whether the initial vector x0 has an effect on the steady-state vector q. What about
the number of iterations k? Write a comment about it.

(e)
P is a car rental pick-up/return matrix between the Airport, Downtown, and Metro:
P=[.90 .01 .09;.01 .90 .01;.09 .09 .90], x0=[.5; .3; .2]

This is the end of Project 3
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468