[英]R center given 3 points - solving simultaneous equations
假设我有 3 个二进制点 (5,0),(0,5),(-5,0) 并且我想找到一个与这 3 个点等距的点(简而言之,找到通过这 3 个点的圆心)。 我从几何学中知道,如果我的答案是 (a,b),那么我可以找到 (a,b) 和 3 个点之间的距离并将它们相等,然后求解 3 个联立方程。 我怎样才能在 R 中快速做到这一点? 我知道方程是线性的,所有的平方项都会抵消。
_____________________________更新1
我尝试在 google 上搜索如何在 R 中求解线性方程。但没有得到好的结果,因为所有链接都希望我为所有 3 个方程提供 LHS 系数和 RHS 值。 但是我没有RHS。 我必须一次取 2 个方程并移动项来找到 RHS。 是否有任何 R 包可以为我做到这一点?
您可能已经从现在开始继续前进,但为了后代,我将添加到此线程中。 我想人们只是因为你没有提出尝试而生气? 我偶然发现了这篇寻求帮助的帖子,那里的一些受访者完全没有帮助。 我在我的应用程序中尝试了你的方法,但没有太大成功,并编写了以下函数 circlefit(),如果提供了列 1=X 和列 2=Y 的数据帧,则对沿弧的点执行最小二乘逼近。 我很确定上面的代码在我的应用程序中失败了,因为我沿着“模糊”边缘有 >100 个点,所以我的应用程序更适合最小二乘法。
干杯!
x_x<-c(0,0.5, 1, 1.5, 2, 2.5, 3)
y_x<-c(0,.25, 1, 2.25, 4, 6.25, 9)
df<-as.data.frame(cbind(x_x, y_x))
circlefit<-function (df){
names(df)<-c("X", "Y")
#find mean x y so we can cacluate the difference of each X, Y from its respective mean
xmean<-mean(df$X)
ymean<-mean(df$Y)
#adds needed columns for summations required to perform least squares
mat2<-df%>%
mutate(a=X-xmean)%>%
mutate(b=Y-ymean)%>%
mutate(aa=a^2)%>%
mutate(ab=a*b)%>%
mutate(bb=b^2)%>%
mutate(aaa=a^3)%>%
mutate(abb=a*b^2)%>%
mutate(baa=b*a^2)%>%
mutate(bbb=b^3)
#column sums for construction of linear system of equations
Saa<-sum(mat2$aa)
Sab<-sum(mat2$ab)
Sbb<-sum(mat2$bb)
Saaa<-sum(mat2$aaa)
Sbbb<-sum(mat2$bbb)
Sabb<-sum(mat2$abb)
Sbaa<-sum(mat2$baa)
#linear stystem of equations
sums_row1<-c(Saa, Sab)
sums_row2<-c(Sab, Sbb)
sum_mat<-as.matrix(rbind(sums_row1, sums_row2), nrow=2)
#gauss elimination ratio
gauss_ratio<-sum_mat[1,2]/sum_mat[1,1]
#new eliminated row
elim_row2<-sums_row2-(sums_row1*gauss_ratio)
#initial (A,B)
Ac<-0.5*(Saaa+Sabb)
Bc<-0.5*(Sbbb+Sbaa)
#result of Bc after elimination
elim_Bc<-Bc-(gauss_ratio*Ac)
#final deviation of (A, B) from mean
fin_Bc<-elim_Bc/elim_row2[2]
fin_Ac<-(Ac-(fin_Bc*sum_mat[1,2]))/sum_mat[1,1]
#center of least squares fit of circle (xc,yc)
Xc<-xmean+fin_Ac
yc<-ymean+fin_Bc
alpha<-fin_Ac^2+fin_Bc^2+((Saa+Sbb)/nrow(mat2))
#radius of circle
radius<-sqrt(alpha)
#temporarily stores circle parameters, names them and then puts them in the globalEnv
circle_parms<-c(Xc, yc, radius)
names(circle_parms)<-c("Xc", "Yc", "Radius")
circle_parms<<-circle_parms
#generates a ggplot of the the input data and the approximated circle; puts plot in the globalEnv as circleplot
circleplot<<-ggplot(df, aes(x=X, y=Y))+geom_point()+
geom_point(aes(x=Xc, y=yc), color="Red", size=4)+theme(aspect.ratio = 1)
#defines a circle fit function such that it can be added to the circleplot above; this function is available in globalEnv
gg_circle <<- function(r, xc, yc, color="blue", fill=NA, ...) {
x <- xc + r*cos(seq(0, pi, length.out=100))
ymax <- yc + r*sin(seq(0, pi, length.out=100))
ymin <- yc + r*sin(seq(0, -pi, length.out=100))
annotate("ribbon", x=x, ymin=ymin, ymax=ymax, color=color, fill=fill, ...)
}
#adds the fit circle to the data.
circleplot+gg_circle(r=radius, xc=Xc, yc=yc)
}
circlefit(df)
circle_parms
我使用了评论中给出的链接。 我的代码如下
#finding circles center
p3=c(0,5,5,0,-5,0)#coordinates of point in (x1,y1,x2,y2,x3,y3) format
mat1=matrix(c(p3[1]^2+p3[2]^2,p3[2],1,p3[3]^2+p3[4]^2,p3[4],1,p3[5]^2+p3[6]^2,p3[6],1),nrow=3,ncol=3,byrow=TRUE)
mat2=matrix(c(p3[1],p3[1]^2+p3[2]^2,1,p3[3],p3[3]^2+p3[4]^2,1,p3[5],p3[5]^2+p3[6]^2,1),nrow=3,ncol=3,byrow=TRUE)
mat3=matrix(c(p3[1],p3[2],1,p3[3],p3[4],1,p3[5],p3[6],1),nrow=3,ncol=3,byrow=TRUE)
mat1
mat2
mat3
xcenter=det(mat1)/(2*det(mat3))
ycenter=det(mat2)/(2*det(mat3))
radius=sqrt((p3[1]-xcenter)^2+(p3[2]-ycenter)^2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.