Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

P7.1.7 Complete the following function so that it performs as specified: functio

ID: 3183820 • Letter: P

Question

P7.1.7 Complete the following function so that it performs as specified:

function y = stationary(P,x,tol,itMax)

% P is an n-by-n probability matrix.

% x is an n-by-1 state vector.

% tol is a positive real number.

% itMax is a positive integer.

% Let z be the state vector after M Markov steps. If there is an M<=itMax

% such that sum(abs(P*z - z)) <= tol, then y = z. Otherwise, y is the

% state vector after itMax steps.

**YOU CAN USE THE FUNCTION TRANSITION AT THE BOTTOM AS A SUBFUNCTION*

A probability matrix contains only nonnegative real values and each column sums to the value 1

SUBFUNCTION TRANSITION

function y = Transition(P,x)
% P is an n-by-n transition matrix.
% x is an n-by-1 vector
% y is an n-by-1 vector obtained by taking a Markov step.

% Initializations...
[n,n] = size(P);
y = zeros(n,1);
for i=1:n
% Compute the new ith state value...
for j=1:n
% Add in the contribution from the current jth state...
y(i) = y(i) + P(i,j)*x(j);
end
end

Explanation / Answer

matlab code

close all
clear
clc

P = [0.2 0.7;
0.8 0.3];
tol = 1e-6;
x = [1 2]';
itMax = 10000;

y = stationary(P,x,tol,itMax);
disp('Sample Result:');
disp(y);

function y = stationary(P,x,tol,itMax)
% P is an n-by-n probability matrix.
% x is an n-by-1 state vector.
% tol is a positive real number.
% itMax is a positive integer.
% Let z be the state vector after M Markov steps. If there is an M<=itMax
% such that sum(abs(P*z - z)) <= tol, then y = z. Otherwise, y is the
% state vector after itMax steps.
M = 1;
while M <= itMax
x_hist = x;
x = Transition(P, x);
if abs(x - x_hist) <= tol
break;
end
M = M+1;
end
y = x;
end

function y = Transition(P,x)
% P is an n-by-n transition matrix.
% x is an n-by-1 vector
% y is an n-by-1 vector obtained by taking a Markov step.
% Initializations...
[~,n] = size(P);
y = zeros(n,1);
for i=1:n
% Compute the new ith state value...
for j=1:n
% Add in the contribution from the current jth state...
y(i) = y(i) + P(i,j)*x(j);
end
end
end

output

Sample Result:
1.4000
1.6000