Skip to content
Prev 164856 / 398503 Next

Movement within a circle

The following has a bug in it, but it does a reasonable job.  The bug
occurs in the lines that says

if(inner(xnew,xnew) > R^2)
   xnew <- x[n,] - eps ##error - sometimes x[n,] - eps is outside
circle

If it is changed to something like xnew <- proj(x[n,] + eps) where
proj is a projection function onto the circle, you should be ok.

HTH,

Andrew.

##=========================================================
N <- 1000 #steps
x <- matrix(0, nrow=N+1, ncol =2)
R <- 1 #radius of circle
delta <- 0.5 #step size

inner <- function(x,y) {
	if(length(x) != length(y)){
		print("Wrong length")
		return(0)
	}

	return(t(x) %*% (y))
}

for(n in 1:N){
	eps <- delta*runif(2)
	xnew <- x[n,] + eps
	if(inner(xnew,xnew) > R^2)
		xnew <- x[n,] - eps ##error - sometimes x[n,] - eps is outside
circle

	x[n+1, ]<- xnew

}

semicircle <- function(x, centre = c(0,0), radius=R){
	return(sqrt(radius^2 - x^2))
}
xvaries <- seq(-R,R,by=0.01)
plot(xvaries, semicircle(xvaries), type = 'l', xlim= c(-R, R), ylim =c
(-R,R))
lines(xvaries,- semicircle(xvaries))
points(x)
On Dec 16, 10:34?am, David Winsemius <dwinsem... at comcast.net> wrote: