Skip to content
Snippets Groups Projects
Commit 7e04a1f4 authored by Andrew E. Torda's avatar Andrew E. Torda
Browse files

Docs and getting ready for Uebung.

parent 25da441f
No related branches found
No related tags found
No related merge requests found
# This only works in two dimensions, but it is enough for the Uebung
# description
ackley <- function(x, y, a, b, c) {
d <- 2
sum1 <- x * x + y * y
sum2 <- cos(c * x) + cos(c * y)
term1 <- -a * exp(-b * sqrt(sum1 / d))
term2 <- -exp(sum2 / d)
y <- term1 + term2 + a + exp(1)
return(y)
}
mymain <- function() {
a <- 20; b <- 0.2; c <- 2 * pi; # defaults
x <- seq(from = -4, to = 4, length.out = 200)
z <- outer(X = x, Y = x, FUN = ackley, a, b, c)
lim <- c(min(x), max(x))
if (T) {
png(filename = "ackley2d.png", bg = "white")
}
persp(x = x, y = x, z = z,
xlab = "x1", ylab = "x2", zlab = "ackley",
theta = -20,
ticktype = "detailed")
dev.off()
}
mymain()
# Let us see if we can make a 2D histogram # Make a 2D plot of some example results.
# Do not use default axis scaling. It is determined by the most extreme points
# visited and there are always a few outliers. Set the boundaries manually with
# plotmin and plotmax.
input_file <- "ex2.csv" input_file <- "ex2.csv"
plotmin <- -5 plotmin <- -5
...@@ -23,23 +26,23 @@ mymain <- function() { ...@@ -23,23 +26,23 @@ mymain <- function() {
df <- enforce_limits(df) df <- enforce_limits(df)
nbins <- 100 nbins <- 100
if (F) { if (F) { # This would be default scaling, but it looks awful.
x.bin <- seq(floor(min(df$x1)), ceiling(max(df$x1)), length = nbins) x_bin <- seq(floor(min(df$x1)), ceiling(max(df$x1)), length = nbins)
y.bin <- seq(floor(min(df$x2)), ceiling(max(df$x2)), length = nbins) y_bin <- seq(floor(min(df$x2)), ceiling(max(df$x2)), length = nbins)
} else { } else {
x.bin <- seq(-5,5,length = nbins) x_bin <- seq(-5, 5, length = nbins)
y.bin <- seq(-5,5,length = nbins) y_bin <- seq(-5, 5, length = nbins)
} }
freq <- as.data.frame(table(findInterval(df$x1, x.bin),findInterval(df$x2, y.bin))) freq <- as.data.frame(table(findInterval(df$x1, x_bin), findInterval(df$x2, y_bin)))
freq[, 1] <- as.numeric(freq[, 1]) freq[, 1] <- as.numeric(freq[, 1])
freq[, 2] <- as.numeric(freq[, 2]) freq[, 2] <- as.numeric(freq[, 2])
freq2D <- diag(nbins) * 0 freq2D <- diag(nbins) * 0
freq2D[cbind(freq[, 1], freq[, 2])] <- freq[, 3] freq2D[cbind(freq[, 1], freq[, 2])] <- freq[, 3]
par(mfrow=c(1, 2)) par(mfrow=c(1, 2))
image(x.bin, y.bin, freq2D, col=topo.colors(max(freq2D))) image(x_bin, y_bin, freq2D, col=topo.colors(max(freq2D)))
contour(x.bin, y.bin, freq2D, add=TRUE, col=rgb(1,1,1,.7)) contour(x_bin, y_bin, freq2D, add=TRUE, col=rgb(1,1,1,.7))
palette(rainbow(max(freq2D))) palette(rainbow(max(freq2D)))
cols <- (freq2D[-1,-1] + freq2D[-1,-(nbins-1)] + freq2D[-(nbins-1),-(nbins-1)] + freq2D[-(nbins-1),-1])/4 cols <- (freq2D[-1,-1] + freq2D[-1,-(nbins-1)] + freq2D[-(nbins-1),-(nbins-1)] + freq2D[-(nbins-1),-1])/4
......
File deleted
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment