Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1.  
  2. # Markovketten
  3.  
  4. # Funktion
  5. markov.fun <- function(n, M){
  6.  
  7. text1 <- "\nn muss groesser als 0 sein!"
  8. text2 <- "\nM muss eine quadratische Matrix sein!"
  9.  
  10. # n muss < 0 sein
  11. if(!n > 0) {
  12. stop(text1)
  13. }
  14.  
  15. # M muss eine quadratische Matrix sein
  16. if(ncol(M) != nrow(M) || !(is.matrix(M))){
  17. stop(text2)
  18. }
  19.  
  20. # Funktion fuer eine Matrixpotenz
  21. E <- eigen(M)
  22. V <- E$values
  23. Q <- E$vectors
  24. res.1 <- Q%*%diag(V^n)%*%t(Q)
  25. return(res.1)
  26.  
  27. }
  28.  
  29. # Rekursion
  30. markov.rek <- function(n, M){
  31.  
  32. text1 <- "\nn muss groesser als 0 sein!"
  33. text2 <- "\nM muss eine quadratische Matrix sein!"
  34.  
  35. # n muss < 0 sein
  36. if(!n > 0) {
  37. stop(text1)
  38. }
  39.  
  40. # M muss eine quadratische Matrix sein
  41. if(ncol(M) != nrow(M) || !(is.matrix(M))){
  42. stop(text2)
  43. }
  44.  
  45. #
  46. if(n = 0) return(diag(n))
  47. return(M %*% markov.rek( n-1 , M))
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement