Advertisement
Guest User

Untitled

a guest
May 27th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. n <- 51 # Number of interpolation points
  2. k.1 <- floor(n * 2/3) # Width of the left-hand interval
  3. k.2 <- n - k.1 # ............ right-hand interval
  4. x <- seq(0, 1, length.out=n) # x coordinates
  5. set.seed(17)
  6.  
  7. # Generate random values of the second derivative that are first negative,
  8. # then positive. Modify to suit.
  9. y.2 <- (c(runif(k.1, -1, 0), 0.5*runif(k.2, 0, 1))) * abs(cos(3*pi * x)) +
  10. c(rep(-.1, k.1), rep(.5,k.2))
  11.  
  12. # Recover the first derivative and then the transformation. Control the
  13. # minimum slope of the transformation.
  14. y.1 <- cumsum(y.2)
  15. y.1 <- y.1 - min(y.1) + 0.005 * diff(range(y.1))
  16. y <- cumsum(y.1)
  17. y <- (y - y[1]) / (y[n] - y[1]) # Normalize the transformation
  18.  
  19. #
  20. # Plot the graphs.
  21. par(mfrow=c(1,3))
  22. plot(x, y.2, type="l", bty="n", main="Second derivative")
  23. points(x, y.2, pch=20, cex=0.5)
  24. abline(h=0, col="Red", lty=3)
  25. plot(x, y.1, type="l", bty="n", lwd=2, main="First derivative")
  26. abline(h=0, col="Red", lty=3)
  27. plot(x, y, type="l", lwd=2, main="Transformation")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement