Home > Net >  Generate data inside ellipse and rectangle
Generate data inside ellipse and rectangle

Time:11-30

I need to generate data, which I should use in following calculations. One part of my data should include 15000 points, which have even distribution inside ellipse(runif in R). Here is the the equation of ellipse: Equation of ellipse Another part of data should include 10000 points inside rectangle. Here is the equation of rectangle: Equation of rectangleHow can I do this?

CodePudding user response:

Uniformly Distributed Points

Here is an example for the eclipse data (you could follow similar idea for the rectangle one)

n <- 1.5e4
feclipse <- function(x, y) 2 * (x - 6)^2   3 * (y - 4)^2 - 10
res <- c()
repeat {
  if (length(res) / 2 == n) break
  x <- runif(1, 6 - sqrt(5), 6   sqrt(5))
  y <- runif(1, 4 - sqrt(10 / 3), 4   sqrt(10 / 3))
  if (feclipse(x, y) <= 0) {
    res <- rbind(res, c(X = x, Y = y))
  }
}

and plot(res) gives

enter image description here


Non-uniformly Distributed Points

Another option is using analytical solution of y in terms of x to produce random tuples

x <- runif(n, 6 - sqrt(5), 6   sqrt(5))
y <- runif(n, 4 - sqrt((10 - 2 * (x - 6)^2) / 3), 4   sqrt((10 - 2 * (x - 6)^2) / 3))

plot(x, y)

which gives

enter image description here

Or, you can use polar representation to generate the points

theta <- runif(n, 0, 2 * pi)
rho <- runif(n)
x <- rho * sqrt(5) * sin(theta)   6
y <- rho * sqrt(10 / 3) * cos(theta)   4
plot(cbind(x, y))

which gives

enter image description here

CodePudding user response:

Rectangle:

set.seed(94)
u <- runif(1.5e4, max = 3)
v <- runif(1.5e4, max = 2)
x <- u   v - 10
y <- v - u   8
# sanity check
range(x   y)
#[1] -1.999774  1.999826
range(x - y   15)
#[1] -2.999646  2.999692
plot(x, y)

enter image description here

Ellipse (see enter image description here

  • Related