Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. # Markovketten
  2.  
  3. # Funktion
  4. markov.fun <- function(n = 2, M){
  5.  
  6. text1 <- "\nn muss eine nateurliche Zahl groesser gleich 0 sein!"
  7. text2 <- "\nM muss eine quadratische Matrix sein!"
  8.  
  9. # n muss eine natuerliche Zahl >= 0 sein
  10. if(!is.numeric(n) || n < 0 || !(n %% 1 == 0)) {
  11. stop(text1)
  12. }
  13.  
  14. # M muss eine quadratische Matrix sein
  15. if(ncol(M) != nrow(M) || !(is.matrix(M))){
  16. stop(text2)
  17. }
  18.  
  19. # bei n = 0
  20. if(n == 0){
  21. return(diag(ncol(M)))
  22. }
  23.  
  24. # bei n > 0
  25. if(n > 0){
  26. i <- 0
  27. while(i < n){
  28. res <- M %*% M
  29. i <- i + 1
  30. }
  31. }
  32. return(res)
  33. }
  34.  
  35. # Rekursion
  36. markov.rek <- function(n = 2, M){
  37.  
  38. text1 <- "\nn muss eine nateurliche Zahl groesser gleich 0 sein!"
  39. text2 <- "\nM muss eine quadratische Matrix sein!"
  40.  
  41. if(!is.numeric(n) || n < 0 || !(n %% 1 == 0)) {
  42. stop(text1)
  43. }
  44.  
  45. if(ncol(M) != nrow(M) || !(is.matrix(M))){
  46. stop(text2)
  47. }
  48.  
  49. if(n == 0){
  50.  
  51. return(diag(ncol(M)))
  52.  
  53. }
  54. M %*% markov.rek(n-1, M)
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement