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

标签 r graph hyperbolic-function

任务

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

enter image description here

我的尝试

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

enter image description here

如您所见,我的图表有一个额外的线段,它显示在 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")

关于r - 我在尝试使用 R 绘制双曲线时哪里出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62764169/

相关文章:

algorithm - 寻找第 k 条最短路径?

string - 获取和删除字符串的第一个字符

r - 在可变位置整理具有多个部分/标题的数据集

r - 在 r 中使用符号或 '|'

algorithm - 小度有向无环图中的最短路径

javascript - 使用 jQuery Flot 在点击时隐藏系列

javascript - Tanh 为大输入返回 NaN?

machine-learning - 为什么使用 tanh 作为 MLP 的激活函数?

glsl - 四元数除法和双曲正切tanh

r - 基于R中的不同值合并数据帧