## Podiplomski studij statistike, 2002/2003 ## V. Batagelj: Informacijska tehnologija v analizi podatkov ## Risanje funkcij v R-ju ## 10.jan 03 / 05.jan 03 ## # ------------------------------------------------------------------ ## Grid # risanje funkcij f4 <- function(x){x^4 - 14*x^3 + 60*x^2 - 70*x } curve(f4,0,7) # Rosenbrock Banana function fun <- function(x,y){100 * (y - x * x)^2 + (1 - x)^2} x <- seq(-5,5,0.1); y <- x z <- outer(x,y,fun) contour(x,y,z) contour(x,y,log(z),nlevels=20) x <- seq(-0.5,1.5,0.01); y <- x; z <- outer(x,y,fun) contour(x,y,log(z),nlevels=20) image(x,y,log(z)) persp(x,y,log(z),zlim=c(-10,10),theta=180,phi=45) persp(x,y,log(z),zlim=c(-11,6),theta=180,phi=-10) # ------------------------------------------------------------------ ## Lattice library(lattice) x <- seq(0.9,1.1,0.0025); y <- x g <- expand.grid(x=x,y=y) g$z <- fun(g$x,g$y); g$z <- log(g$z) which.min(g$z) [1] 3281 g$z[3281] <- -20 levelplot(z~x*y,data=g,cuts=50,xlab="x",ylab="y", main="Rosenbrock Banana",colorkey=FALSE) wireframe(z ~ x * y,data=g,drape=TRUE,colorkey=TRUE,shade=TRUE) pdf(file="ban1.pdf") wireframe(z ~ x * y,data=g,drape=TRUE,colorkey=TRUE,shade=TRUE) dev.off(); pdf(file="ban2.pdf") wireframe(z ~ x * y,data=g,drape=TRUE,colorkey=TRUE,shade=TRUE, screen=list(z=30,x=-60)) dev.off(); pdf(file="ban3.pdf") wireframe(z ~ x * y,data=g,drape=TRUE,colorkey=TRUE,shade=TRUE, screen=list(z=25,x=-50),scales=list(draw=FALSE)) dev.off() # ------------------------------------------------------------------