n <- 1000
n.t <- 1000
X <- matrix(0,n.t,n)
m <- n/10
X[1,10:50] <- 10
X[1,200:300] <- 100
X[1,500:800] <- 50
delta.t <- 1
D <- 0.4
for(i in 2:n.t){
tmp <- X[i-1,]
for(j in 1:n){
to.Left <- delta.t * D * X[i-1,j]
to.Right <- delta.t * D * X[i-1,j]
tmp[j] <- tmp[j] - to.Left - to.Right
if(j-1 >= 1){
tmp[j-1] <- tmp[j-1] + to.Left
}
if(j+1 <= n){
tmp[j+1] <- tmp[j+1] + to.Right
}
}
X[i,] <- tmp
}
image(X)
for(i in 1:n.t){
plot(1:n,X[i,],ylim = c(0,max(X)),type="l")
}
n <- 1000
n.t <- 3000
X <- Y <- matrix(0,n.t,n)
m <- n/10
X[1,10:50] <- 1
X[1,200:300] <- 10
X[1,500:800] <- 5
Y[1,] <- 0.5
delta.t <- 1
Dx <- 0.4
Dy <- 0.2
for(ii in 2:n.t){
new.X <- X[ii-1,]
new.Y <- Y[ii-1,]
for(j in 1:n){
delta.X <- 0.05*X[ii-1,j] - 0.06*Y[ii-1,j]*X[ii-1,j]
delta.Y <- 0.06*X[ii-1,j]*Y[ii-1,j] - 0.07*Y[ii-1,j]
new.X[j] <- new.X[j] + delta.X
new.Y[j] <- new.Y[j] + delta.Y
}
tmpX <- new.X
tmpY <- new.Y
for(j in 1:n){
to.LeftX <- delta.t * Dx * new.X[j]
to.RightX <- delta.t * Dx * new.X[j]
to.LeftY <- delta.t * Dy * new.Y[j]
to.RightY <- delta.t * Dy * new.Y[j]
tmpX[j] <- tmpX[j] - to.LeftX - to.RightX
tmpY[j] <- tmpY[j] - to.LeftY - to.RightY
if(j-1 >= 1){
tmpX[j-1] <- tmpX[j-1] + to.LeftX
tmpY[j-1] <- tmpY[j-1] + to.LeftY
}
if(j+1 <= n){
tmpX[j+1] <- tmpX[j+1] + to.RightX
tmpY[j+1] <- tmpY[j+1] + to.RightY
}
}
X[ii,] <- tmpX
Y[ii,] <- tmpY
}
for(i in 1:n.t){
matplot(1:n,cbind(X[i,],Y[i,]),ylim = c(0,max(c(X,Y))),type="l")
}
image(log(X+1),col=topo.colors(100),main="X",xlab="time",ylab="location")
image(log(Y+1),col=topo.colors(100),main="Y",xlab="time",ylab="location")