Home > Software engineering >  Why is the power of a negative value omitted from my graph?
Why is the power of a negative value omitted from my graph?

Time:11-21

I would like to depict a graph that is the power of some values in a continuous way.

f <- function(x) {
  return (x^(1/3))
}  

ggplot(data.frame(x=seq(-5,5,length.out=10)), aes(x))  
  stat_function(fun=f) 

This only shows the values that are x > 0, though if I use online graph tool desmos it shows values of x < 0 too. In interactive sessions, -3^(1/3) returns -1.44225.

Why does R omit it and how can I depict the negative x values too?

enter image description here

CodePudding user response:

Exponential functions with a negative base are considered undefined over the set of real numbers. This is since e.g. (-2)^(m/n) is impossible if n is even and given any x there will be infinitely many fractions of that form which are arbitrarily close to x. If you want the cube root you can use

cuberoot <- function(x){sign(x)*abs(x)^(1/3)} 

CodePudding user response:

As @JohnColeman explains, the results of your function are undefined for negative x. The reason -3^(1/3) seems to work in the console but not your functions is due to enter image description here

Created on 2022-11-20 with reprex v2.0.2

  • Related