swh:1:snp:81eadaa089e8253d8469bcef66aa332632c6c669
Tip revision: 113add4c7a9bfd37466a9b41cf2dc060b7b87091 authored by HwB on 06 December 2012, 00:00:00 UTC
version 1.3.1
version 1.3.1
Tip revision: 113add4
meshgrid.R
##
## m e s h g r i d . R Generate a Mesh Grid
##
meshgrid <- function(x, y = x) {
if (!is.numeric(x) || !is.numeric(y))
stop("Arguments 'x' and 'y' must be numeric vectors.")
x <- c(x); y <- c(y)
n <- length(x)
m <- length(y)
X <- matrix(rep(x, each = m), nrow = m, ncol = n)
Y <- matrix(rep(y, times = n), nrow = m, ncol = n)
return(list(X = X, Y = Y))
}
peaks <- function(v = 49, w) {
stopifnot(is.numeric(v))
if (missing(w)) {
if (length(v) == 1 && v >= 1) {
mg <- meshgrid(linspace(-3, 3, floor(v)))
x <- mg$X; y <- mg$Y
} else {
mg <- meshgrid(v, v)
x <- mg$X; y <- mg$Y
}
} else {
stopifnot(is.numeric(w))
x <- v; y <- w
}
z <- 3 * (1-x)^2 * exp(-(x^2) - (y+1)^2) -
10 * (x/5 - x^3 - y^5) * exp(-x^2 - y^2) -
1/3 * exp(-(x+1)^2 - y^2)
return(list(X = x, Y = y, Z = z))
}