air_nomad
air_nomad

Reputation: 33

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

The Task

Use R to plot the hyperbola x2 - y2/3 = 1, as in Figure 4.3:

enter image description here

My Attempt

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

As you can see, my graph has an extra line segment that shows up at y = 0 for x-values that should not have any result. I'm a bit confused as to what I did that resulted in such a graph.

Upvotes: 3

Views: 302

Answers (2)

Allan Cameron
Allan Cameron

Reputation: 173793

You need to construct the shape in four parts to avoid joining the left and right parts of the curve, which is what is causing your white line.

Also, you can use expressions to get nicer mathematical symbols in your annotations:

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

enter image description here

However, the smart / lazy way to get your plot is probably just to keep your code as it is and draw over the offending segment with a single white line. Do this after drawing the curves but before drawing the asymptotes:

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

enter image description here

Upvotes: 2

manotheshark
manotheshark

Reputation: 4365

Use of lines creates a continuous lines by joining the points. The two functions are for the upper and lower portions so they both connect points (-1, 0) and (1, 0).

There are likely other ways to accomplish this, but the changes below show what's happening:

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

Upvotes: 2

Related Questions