# Parameters
Mx <- 100
My <- 100
N <- 200
x <- seq(-pi, pi, l = Mx + 1)[-c(Mx + 1)]
y <- seq(-pi, pi, l = My + 1)[-c(My + 1)]
m <- c(pi / 2, pi)
p <- c(0, 1)
u0 <- c(outer(dWn1D(x, p[1], 0.5), dWn1D(y, p[2], 0.5)))
bx <- outer(x, y, function(x, y) 5 * sin(m[1] - x))
by <- outer(x, y, function(x, y) 5 * sin(m[2] - y))
sigma2 <- matrix(1, nrow = Mx, ncol = My)
sigmaxy <- matrix(0.5, nrow = Mx, ncol = My)
# Full trajectory of the solution (including initial condition)
u <- crankNicolson2D(u0 = cbind(u0), bx = bx, by = by, sigma2x = sigma2,
sigma2y = sigma2, sigmaxy = sigmaxy,
N = 0:N, deltat = 1 / N, Mx = Mx, deltax = 2 * pi / Mx,
My = My, deltay = 2 * pi / My)
# Mass conservation
colMeans(u) * 4 * pi^2
# Only final time
v <- crankNicolson2D(u0 = cbind(u0), bx = bx, by = by, sigma2x = sigma2,
sigma2y = sigma2, sigmaxy = sigmaxy,
N = N, deltat = 1 / N, Mx = Mx, deltax = 2 * pi / Mx,
My = My, deltay = 2 * pi / My)
sum(abs(u[, N + 1] - v))
if (FALSE) {
# Visualization of tpd
library(manipulate)
manipulate({
plotSurface2D(x, y, z = matrix(u[, j + 1], Mx, My),
main = round(mean(u[, j + 1]) * 4 * pi^2, 4),
levels = seq(0, 2, l = 21))
points(p[1], p[2], pch = 16)
points(m[1], m[2], pch = 16)
}, j = slider(0, N))
}
Run the code above in your browser using DataLab