Simulações do Passeio Aleatório 2 e 3 dimensionais
passeio2d<-function(n){
dir=list(c(1,0),c(0,1),c(-1,0),c(0,-1))
direcoes=t(data.frame(sample(dir,n,replace = TRUE)))
rownames(direcoes)<-1:n
data<- cbind(cumsum(direcoes[,1]),cumsum(direcoes[,2]))
data
}
trespassseios2d <- data.frame(cbind(passeio2d(40000),passeio2d(40000),passeio2d(40000)))
colnames(trespassseios2d)<-c("x1","y1","x2","y2","x3","y3")
require(plotly)
## Loading required package: plotly
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
p <- plot_ly(trespassseios2d, x = ~x1, y = ~y1, type = 'scatter', mode = 'lines',
line = list(color = '#1f77b4', width = 1)) %>%
add_trace(x = ~x2, y = ~y2,
line = list(color = 'rgb(44, 160, 44)', width = 1)) %>%
add_trace(x = ~x3, y = ~y3,
line = list(color = "red", width = 1))
p
Vamos plotar a distância do passeio até a origem
numi=90000
distanciaateorigem<- function(){ mapply(function(x, y) sqrt(x^2+y^2), passeio2d(numi)[,1],passeio2d(numi)[,2])}
plot(distanciaateorigem(),type="l",col="red",main="Distância do Passeio Aleatório a Origem")
abline(h = 0, col = "red", lwd = 2)
lines(distanciaateorigem(), type = 'l',col="green")
lines(distanciaateorigem(), type = 'l',col="blue")
lines(distanciaateorigem(), type = 'l',col="pink")
lines(distanciaateorigem(), type = 'l',col="yellow")
passeio<-function(n){
dir=list(c(1,0,0),c(0,1,0),c(0,0,1),c(-1,0,0),c(0,-1,0),c(0,0,-1))
direcoes=t(data.frame(sample(dir,n,replace = TRUE)))
rownames(direcoes)<-1:n
data<- cbind(cumsum(direcoes[,1]),cumsum(direcoes[,2]),cumsum(direcoes[,3]))
data
}
trespassseios <- data.frame(cbind(passeio(40000),passeio(40000),passeio(40000)))
colnames(trespassseios)<-c("x1","y1","z1","x2","y2","z2","x3","y3","z3")
require(plotly)
p <- plot_ly(trespassseios, x = ~x1, y = ~y1, z = ~z1, type = 'scatter3d', mode = 'lines',
line = list(color = '#1f77b4', width = 1)) %>%
add_trace(x = ~x2, y = ~y2, z = ~z2,
line = list(color = 'rgb(44, 160, 44)', width = 1)) %>%
add_trace(x = ~x3, y = ~y3, z = ~z3,
line = list(color = "red", width = 1))
p
Vamos plotar a distância do passeio até a origem
numi=90000
distanciaateorigem<-function(){mapply(function(x, y,z) sqrt(x^2+y^2+z^2), passeio(numi)[,1],passeio(numi)[,2],passeio(numi)[,3])}
plot(distanciaateorigem(),type="l",col="red",main="Distância do Passeio Aleatório a Origem")
abline(h = 0, col = "red", lwd = 2)
lines(distanciaateorigem(), type = 'l',col="green")
lines(distanciaateorigem(), type = 'l',col="blue")
lines(distanciaateorigem(), type = 'l',col="pink")
lines(distanciaateorigem(), type = 'l',col="yellow")
passeio4d<-function(n){
dir=list(c(1,0,0,0),c(0,1,0,0),c(0,0,1,0),c(-1,0,0,0),c(0,-1,0,0),c(0,0,-1,0),c(0,0,0,1),c(0,0,0,-1))
direcoes=t(data.frame(sample(dir,n,replace = TRUE)))
rownames(direcoes)<-1:n
data<- cbind(cumsum(direcoes[,1]),cumsum(direcoes[,2]),cumsum(direcoes[,3]),cumsum(direcoes[,4]))
data
}
Distância até a origem
numi=90000
distanciaateorigem<-mapply(function(x, y,z,w) sqrt(x^2+y^2+z^2+w^2), passeio4d(numi)[,1],passeio4d(numi)[,2],passeio4d(numi)[,3],passeio4d(numi)[,4])
plot(distanciaateorigem,type="l",col="blue")
abline(h = 0, col = "red", lwd = 2)