Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a horizontal quadratic function?

Tags:

r

I am trying to plot a "sideways" or horizontal quadratic function in R but am running into the issue of having sqrt(-x) which is a problem of course.

eq1 = function(x){ -60*(sqrt(1-x)-1) }
eq2 = function(x){ 60*(sqrt(1-x)+1) }

plot(eq1, 0, 100, add=TRUE, xlim=c(0,1), ylim=c(0,100))
plot(eq2, 0, 100, add=TRUE)

Here is an example of the output plot

enter image description here

And the output from the R Console:

> eq1 = function(x){ -60*(sqrt(1-x)-1) }
> eq2 = function(x){ 60*(sqrt(1-x)+1) }
> 
> 
> plot(eq1, 0, 100, add=TRUE, xlim=c(0,1), ylim=c(0,100))
Warning messages:
1: In curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab,  :
  'add' will be ignored as there is no existing plot
2: In sqrt(1 - x) : NaNs produced
> plot(eq2, 0, 100, add=TRUE)
Warning message:
In sqrt(1 - x) : NaNs produced

If I understand correctly, there are domain restrictions on a quadratic function like this. If so, is there a way to incorporate them into the function as defined in R? Or is there are better way to go about drawing this function?

like image 683
Didymops Avatar asked Apr 10 '26 22:04

Didymops


1 Answers

You need to correctly define the range you want to plot the function on with the x argument to plot.function:

plot(eq1, seq(0, 1, 0.01), xlim=c(0,1), ylim=c(0,100))
plot(eq2, seq(0, 1, 0.01), add=TRUE)

Actually, plot.function is smart enough that even without a range it will fix that for you:

plot(eq1, xlim=c(0,1), ylim=c(0,100))
plot(eq2, add=TRUE)

enter image description here

(With no warnings.)

like image 155
Axeman Avatar answered Apr 12 '26 14:04

Axeman