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

(With no warnings.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With