I'm relatively new to R and I've been trying to simulate a normal distribution with R's builtin functions rnorm and dnorm and subsequently plotting it.
Why is it the case that it plots this wrong density function when my code is this
x <- rnorm(1000, mean=5, sd=2)
hist(x, border='red',freq=F)
y <- curve(dnorm(x,mean(x), sd(x)), add=T)
but when my code is like this it does plot the correct density function
x <- rnorm(1000, mean=5, sd=2)
hist(x, border='red',freq=F)
meanx <- mean(x)
sdx <- sd(x)
y <- curve(dnorm(x,meanx,sdx), add=T)

