0

I define a function f as :

f <- function(x){
  if (x < 0) {
    x^3-x^2+4
  } else {
    -4*x+2
  }
}

and now i want to draw it with curve function for example on interval [-100,100].

So i write

curve(f,from=-100,to=100)

And i only get the function x^3-x^2+4 on interval [-100,100]

Why i don't get my function -4x+2 on interval [0,100] ?

When i want to draw function f for example on interval [3,4] it show's me a good function. It seem's to be a problem when it has to draw two functions in a one graph.

Sonny
  • 3,083
  • 1
  • 11
  • 19
John
  • 1,849
  • 2
  • 13
  • 23
  • 1
    Welp, there is a warning message saying *"the condition has length > 1 and only the first element will be used"* - did you try to Google that? In other words, `if` isn't vectrized. Try `f <- function(x) ifelse(x<0, x^3-x^2+4, -4*x+2)` instead. – David Arenburg Mar 10 '19 at 09:35
  • Thank you very much it's working !! But why i have vertical line in discontinuous point ? – John Mar 10 '19 at 09:43
  • I see this when i draw graph on a small interval, for example [-1,1] – John Mar 10 '19 at 09:45

0 Answers0