How to obtain the lengths of semi axes of an ellipse? in R

倾然丶 夕夏残阳落幕 提交于 2019-11-30 20:50:02

The square of the semi-axes are the eigenvalues of the shape matrix, times the average squared radius.

x <- c(1.798805,2.402390,2.000000,3.000000,1.000000)
y <- c(0.3130147,0.4739707,0.2000000,0.8000000,0.1000000)
d <- cbind( x, y )
library(cluster)
r <- ellipsoidhull(d)
plot( x, y, asp=1, xlim=c(0,4) )
lines( predict(r) )
e <- sqrt(eigen(r$cov)$values)
a <- sqrt(r$d2) * e[1]  # semi-major axis
b <- sqrt(r$d2) * e[2]  # semi-minor axis
theta <- seq(0, 2*pi, length=200)
lines( r$loc[1] + a * cos(theta), r$loc[2] + a * sin(theta) )
lines( r$loc[1] + b * cos(theta), r$loc[2] + b * sin(theta) )
agstudy

You can do this:

exy <- predict(ellipsoidhull(d)) ## the ellipsoid boundary
me <- colMeans((exy))            ## center of the ellipse

Then you compute the minimum and maximum distance to get respectively minor and major axis:

dist2center <- sqrt(rowSums((t(t(exy)-me))^2))
max(dist2center)     ## major axis
[1] 1.264351
> min(dist2center)   ## minor axis
[1] 0.1537401

EDIT plot the ellipse with the axis:

plot(exy,type='l',asp=1)
points(d,col='blue')
points(me,col='red')
lines(rbind(me,exy[dist2center == min(dist2center),]))
lines(exy[dist2center == max(dist2center),])

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!