我在尝试使用 R 绘制双曲线时哪里出错了?

Where did I go wrong in my attempt to graph a hyperbola using R?

任务

用R绘制双曲线x2 - y2/3 = 1,如图4.3:

我的尝试

x <- seq(-5, 5, by = 0.01)
x <- x[(3*(x^2 - 1)) >= 0]

y.upper <- sqrt(3*(x^2 - 1))
y.lower <- -sqrt(3*(x^2 - 1))
y.max <- max(y.upper)
y.min <- min(y.lower)
d1 <- sqrt(3)*x
d2 <- -sqrt(3)*x

plot(c(-5, 5), c(y.min, y.max), type = "n", xlab = "x", ylab = "y")
lines(x, y.upper)
lines(x, y.lower)
lines(x,d1)
lines(x,d2)
points(2, 0)
points(-2,0)
text(2, 0, "focus (2, 0)", pos=4)
text(5, y.max, "asymptote y = sqrt(3)*x", pos = 2)
title("The hyperbola x^2 - y^2/3 = 1")

如您所见,我的图表有一个额外的线段,它显示在 y = 0 处,对于不应有任何结果的 x 值。我对我所做的导致这样的图表感到有点困惑。

使用lines通过连接点创建一条连续的线。这两个函数是针对上部和下部的,所以它们都连接点 (-1, 0) 和 (1, 0)。

可能还有其他方法可以完成此操作,但下面的更改显示了正在发生的事情:

plot(c(-5, 5), c(y.min, y.max), type = "n", xlab = "x", ylab = "y")
lines(x[x < 0], y.upper[x < 0])
lines(x[x > 0], y.upper[x > 0])
lines(x[x < 0], y.lower[x < 0])
lines(x[x > 0], y.lower[x > 0])
lines(x, d1)
lines(x, d2)
points(2, 0)
points(-2,0)
text(2, 0, "focus (2, 0)", pos=4)
text(5, y.max, "asymptote y = sqrt(3)*x", pos = 2)
title("The hyperbola x^2 - y^2/3 = 1")

您需要分四部分构建形状,以避免连接曲线的左右部分,这就是导致白线的原因。

此外,您可以使用表达式在注释中获得更好的数学符号:

x_left <- seq(-5, -1, by = 0.01)
x_right <- seq(1, 5, 0.01)

y.upper_left <- sqrt(3*(x_left^2 - 1))
y.upper_right <- sqrt(3*(x_right^2 - 1))
y.lower_left <- -sqrt(3*(x_left^2 - 1))
y.lower_right <- -sqrt(3*(x_right^2 - 1))
y.max <- max(y.upper_left)
y.min <- min(y.lower_left)

plot(c(-5, 5), c(y.min, y.max), type = "n", xlab = "x", ylab = "y")
lines(x_left, y.upper_left)
lines(x_right, y.upper_right)
lines(x_left, y.lower_left)
lines(x_right, y.lower_right)
abline(0, sqrt(3))
abline(0, -sqrt(3))
points(2, 0)
points(-2,0)
text(2, 0, "focus (2, 0)", pos=4)
text(5, y.max, expression(paste("asymptote y =") ~ sqrt(3)*x), pos = 2)
title(expression(paste("The hyperbola ") ~ x^2 - y^{2/3} ~ paste(" = 1") ))

然而,获得情节的聪明/懒惰的方法可能只是保持代码原样,并用一条白线画出有问题的部分。在绘制曲线之后但在绘制渐近线之前执行此操作:

x <- seq(-5, 5, by = 0.01)
x <- x[(3*(x^2 - 1)) >= 0]

y.upper <- sqrt(3*(x^2 - 1))
y.lower <- -sqrt(3*(x^2 - 1))
y.max <- max(y.upper)
y.min <- min(y.lower)
d1 <- sqrt(3)*x
d2 <- -sqrt(3)*x

plot(c(-5, 5), c(y.min, y.max), type = "n", xlab = "x", ylab = "y")
lines(x, y.upper)
lines(x, y.lower)
lines(c(-.99, .99), c(0, 0), col = "white")  ## lazy hack
lines(x,d1)
lines(x,d2)
points(2, 0)
points(-2,0)
text(2, 0, "focus (2, 0)", pos=4)
text(5, y.max, "asymptote y = sqrt(3)*x", pos = 2)
title("The hyperbola x^2 - y^2/3 = 1")