在R中给出一般方程如何绘制椭圆?

问题描述 投票:2回答:2

椭圆一般方程:

a * x ^ 2 + b * y ^ 2 + c * x * y + d * x + e * y + f = 0

enter image description here

r math plot ellipse
2个回答
4
投票

另一个答案显示了当您知道椭圆的中心轴和主轴时如何绘制椭圆。但它们从一般椭圆方程中并不明显。所以在这里,我将从头开始。

省略数学推导,您需要从以下等式求解中心:

enter image description here

enter image description here

(oops:应该是“生成qazxsw poi”而不是“生成qazxsw poi”;我无法解决它,因为原来的LaTeX现在已经丢失了,我不想再打字......)

这是一个R函数来执行此操作:

v

几点评论:

  1. 参数u有条件,以确保方程是椭圆而不是其他东西(比如抛物线)。所以,不要传递任意参数值来测试。实际上,从等式中你可以粗略地看到这样的要求。例如,矩阵plot.ellipse <- function (a, b, c, d, e, f, n.points = 1000) { ## solve for centre A <- matrix(c(a, c / 2, c / 2, b), 2L) B <- c(-d / 2, -e / 2) mu <- solve(A, B) ## generate points on circle r <- sqrt(a * mu[1] ^ 2 + b * mu[2] ^ 2 + c * mu[1] * mu[2] - f) theta <- seq(0, 2 * pi, length = n.points) v <- rbind(r * cos(theta), r * sin(theta)) ## transform for points on ellipse z <- backsolve(chol(A), v) + mu ## plot points plot(t(z), type = "l") } 必须是正定的,所以a, b, ..., fA;还有,a > 0
  2. 我使用了Cholesky分解,因为这是我最喜欢的。然而,最美丽的结果来自特征分解。我不会在这方面进一步追求。如果您对此感兴趣,请阅读我的另一个答案det(A) > 0。有漂亮的数字来说明Cholesky分解和特征分解的几何。

4
投票

我们可以从r ^ 2 > 0Obtain vertices of the ellipse on an ellipse covariance plot (created by car::ellipse)方程开始(以下一个来自维基百科),我们需要5个参数:中心parametricellipse在另一种表示法,轴长度(xc, yc)和x轴与主轴(h,k)a, b之间的角度另一种表示法。

phi

tau

enter image description here

现在,如果我们想从xc <- 1 # center x_c or h yc <- 2 # y_c or k a <- 5 # major axis length b <- 2 # minor axis length phi <- pi/3 # angle of major axis with x axis phi or tau t <- seq(0, 2*pi, 0.01) x <- xc + a*cos(t)*cos(phi) - b*sin(t)*sin(phi) y <- yc + a*cos(t)*cos(phi) + b*sin(t)*cos(phi) plot(x,y,pch=19, col='blue') 方程开始,那么这是一个两步过程。

  1. enter image description here方程转换为cartesian coniccartesian),形式我们可以使用以下方程式首先使用下图中的5个方程式获得5个参数(取自polar,可以在那里找到详细的数学)。
  2. 使用获得的参数绘制椭圆,如上所示。

parametric

对于步骤(1),我们可以使用以下代码(当我们知道http://www.cs.cornell.edu/cv/OtherPdf/Ellipse.pdf时):

enter image description here

对于步骤(2),请使用以下代码:

A,B,C,D,E,F
© www.soinside.com 2019 - 2024. All rights reserved.