R中心给出3个点 - 求解联立方程

时间:2016-03-04 18:44:13

标签: r center geometry simultaneous

假设我有3个二进制点(5,0),(0,5),( - 5,0),我想找到一个与这3个点等距的点(在圆圈中找到通过的圆点)那3分)。我从几何学中知道,如果我的答案是(a,b),那么我可以找到(a,b)和3个点之间的距离并将它们等同,然后求解3个联立方程。我怎样才能在R中快速完成此操作?我知道方程式将是线性的,并且所有平方项都将抵消。

_____________________________ UPDATE1

我试图在谷歌搜索如何解决R中的线性方程。但是没有得到好结果,因为所有链接都希望我提供所有3个方程的LHS系数和RHS值。但我没有RHS。我必须一次取2个方程并移动术语以找到RHS。有没有R包可以帮我吗?

2 个答案:

答案 0 :(得分:1)

您可能已经从现在开始前进了,但是为了后代,我将添加到此线程中。我想人们只是为您没有尝试而生气?我偶然发现了该帖子以寻求帮助,那里的一些受访者完全没有帮助。对于我的应用程序,我尝试了您的方法,但收效甚微,并编写了以下函数circlefit(),以提供圆弧1 = X和column2 = 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

答案 1 :(得分:0)

我使用了评论中提供的link。我的代码如下

#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)