library(ggplot2) library(grid) ## Aufgabe 1i ----------------- f <- function(t, p) { with(as.list(p), { x <- K*x0/(x0+(K-x0)*exp(-r*t)) out <- data.frame(t = t, x = x, x0 = paste("x0 =", x0), r = paste("r =", r), K = paste("K =", K)) return(out) }) } times <- seq(0, 1, len=100) x0Sequence <- seq(0, 1, by=0.1) out <- data.frame() for(x0 in x0Sequence) { p <- c(x0 = x0, K = 0.5, r = 4) out <- rbind(out, f(times, p)) } P1i <- ggplot(out, aes(x=t, y=x, group=x0, color=x0)) + geom_line() print(P1i + theme_bw()) ## Aufgabe 1ii -------------------------------- times <- seq(0, 1, len=100) x0Sequence <- c(0.3, 0.5, 1) KSequence <- seq(0.2, 1, len=6) out <- data.frame() for(x0 in x0Sequence) { for(K in KSequence) { p <- c(x0 = x0, K = K, r = 4) out <- rbind(out, f(times, p)) } } P1ii <- ggplot(out, aes(x=t, y=x, group=x0, color=x0)) + facet_wrap(~K) + geom_line() print(P1ii + theme_bw()) ## Aufgabe 1iii ------------------------------- times <- seq(0, 1, len=100) x0Sequence <- c(0.3, 0.5, 1) KSequence <- seq(0.2, 1, len=3) rSequence <- seq(2, 4, len=3) out <- data.frame() for(x0 in x0Sequence) { for(K in KSequence) { for(r in rSequence) { p <- c(x0 = x0, K = K, r = r) out <- rbind(out, f(times, p)) } } } P1iii <- ggplot(out, aes(x=t, y=x, group=x0, color=x0)) + facet_grid(r~K) + geom_line() print(P1iii + theme_bw()) ## Aufgabe 2 ----------------------------- VH <- function(x, p) { with(as.list(p), { dx <- r*x*(1-x/K) out <- data.frame(x = x, dx = dx) return(out) }) } times <- seq(0, 1, len=100) x <- seq(0, 1, len=100) p <- c(K=0.75, r=4, x0=1) out <- VH(x, p) P2 <- ggplot(out, aes(x=x, y=dx)) + geom_line() print(P2 + theme_bw()) x2 <- seq(0, 1, len=10) out2 <- VH(x2, p) P2 <- P2 + geom_segment(aes(x = x, y = 0, xend = x + 0.1*dx, yend = 0), data=out2, arrow = arrow(angle=20, length=unit(0.12, "inches")), color="red") print(P2 + theme_bw()) ## Aufgabe 3 ------------------------- times <- seq(0, 1, len=100) timesData <- seq(0, 1, len=10) p <- c(x0 = 0.1, K = 0.5, r = 4) outSmooth <- cbind(f(times, p), what = "smooth", sigma=NA) outSmooth$sigma <- 0.1*outSmooth$x outData <- cbind(f(timesData, p), what = "data", sigma=NA) outData$sigma <- 0.1*outData$x noise <- rnorm(length(timesData), 0, outData$sigma) outData$x <- outData$x + noise out <- rbind(outSmooth, outData) P3 <- ggplot(out, aes(x=t, y=x, ymin=x-sigma, ymax=x+sigma)) P3 <- P3 + geom_line(data=outSmooth) + geom_ribbon(data=outSmooth, alpha=0.2) P3 <- P3 + geom_point(data=outData) + geom_errorbar(data=outData, width=0) print(P3 + theme_bw())