Exercise 13 Write a program to compute \(u^{(n)}\) given u and P. Use this program to compute \(u^{(10)}\) for the Land of Oz example, with u = (0, 1, 0), and with u = (1/3, 1/3, 1/3).
library(expm)
## Warning: package 'expm' was built under R version 3.6.3
## Loading required package: Matrix
##
## Attaching package: 'expm'
## The following object is masked from 'package:Matrix':
##
## expm
compute_un <- function(n,u,P){
Pn <- P %^% n #find p^n
out <- u %*% Pn #find uP^n
return(out)
}
#set up the transition max for oz
oz <- matrix(c(0.5,0.25,0.25,0.5,0,0.5,0.25,0.25,0.5),nrow = 3,byrow=T)
u1 <- c(0,1,0)
u2 <- c(0.33,0.33,0.33)
#for u1
round(compute_un(10,u1,oz),2)
## [,1] [,2] [,3]
## [1,] 0.4 0.2 0.4
#for u2
round(compute_un(10,u2,oz),2)
## [,1] [,2] [,3]
## [1,] 0.4 0.2 0.4