ddrs-lab2/code/5-solve.R
Filipe Rodrigues 3bb4f52cff Finished exercise 5.
Co-authored-by: Ricardo-Rodrigues <R1cardu7@users.noreply.github.com>
2023-12-04 17:49:26 +00:00

25 lines
606 B
R

trans_matrix <- matrix(
c(
c(0, 2, 0),
c(1, 0, 1),
c(1, 1, 0)
),
nrow = 3,
ncol = 3,
byrow = TRUE
)
# Build the coefficient matrix and constants
coef_matrix <- t(trans_matrix) - diag(rowSums(trans_matrix))
consts <- c(0, 0, 0)
# Substitute one of the rows by the final `pi0 + pi1 + pi2 = 1` equation.
subst_idx <- 3
coef_matrix[subst_idx, ] <- c(1, 1, 1)
consts[subst_idx] <- 1
solution <- solve(coef_matrix, consts)
solution <- sapply(solution, function(x) round(x, 4))
print(solution)
write.table(solution, "output/5-solve.csv", sep = "\t", col.names = FALSE, row.names = FALSE)