【问题标题】:R center given 3 points - solving simultaneous equationsR中心给定3分 - 求解联立方程
【发布时间】:2016-03-04 18:44:12
【问题描述】:

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

_____________________________更新1

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

【问题讨论】:

  • 为什么你不想用谷歌搜索这个普遍存在的问题解决方案?
  • 我用谷歌搜索了 - google.com/…。但它不提供任何 R 代码。它只是解释了寻找中心背后的逻辑
  • 用首选语言编写简单的公式 (en.wikipedia.org/wiki/…) 真的有问题吗?
  • 使用det() 应该直接实现这个源:mathforum.org/library/drmath/view/55239.html
  • 该链接帮助很大。请将其发布为答案,我会接受它,并将我编写的使用链接中的逻辑的代码放入

标签: r center geometry simultaneous


【解决方案1】:

您可能从现在开始继续前进,但为了后代,我将添加到此线程中。我想人们只是因为你没有做出尝试而生气吗?我偶然发现了这篇寻求帮助的帖子,但上面的一些受访者完全没有帮助。我尝试了您的方法,但对我的应用程序没有多大成功,并编写了以下函数 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

【讨论】:

    【解决方案2】:

    我使用了 cmets 中给出的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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多