Dear All, Take this code:> f <- function(x) exp(-x)*x-0.05 > g <- function(x) 0 > curve(f,0,5) > curve(g,add=T)Error in xy.coords(x, y) : 'x' and 'y' lengths differ>However, with g <- function(x) x-x no error is generated. Is this a bug? I am using> version_ platform i386-redhat-linux-gnu arch i386 os linux-gnu system i386, linux-gnu status major 2 minor 6.1 year 2007 month 11 day 26 svn rev 43537 language R version.string R version 2.6.1 (2007-11-26)>Thanks in advance, Paul
Paul Smith <phhs80 <at> gmail.com> writes:> > Dear All, > > Take this code: > > > f <- function(x) exp(-x)*x-0.05 > > g <- function(x) 0 > > curve(f,0,5) > > curve(g,add=T) > Error in xy.coords(x, y) : 'x' and 'y' lengths differ > > > > However, with > > g <- function(x) x-x > > no error is generated. > >The first paragraph of "details" in the help page says: . 'x(t)' or 'expr' (with 'x' inside) must return a numeric of the same length as the argument 't' or 'x'. So no, technically this is not a bug. The R idiom for adding a horizontal line to the plot is abline(h=...) cheers Ben Bolker
Paul Smith wrote:> >> f <- function(x) exp(-x)*x-0.05 >> g <- function(x) 0 >> curve(f,0,5) >> curve(g,add=T) >> Error in xy.coords(x, y) : 'x' and 'y' lengths differ >> > > However, with > > g <- function(x) x-x > > no error is generated. > > Is this a bug? >No; simplify it: g1 <- function(x) 0 g2 <- function(x) x - x g1(anything) will return (scalar) 0 g2(scalar) will return (scalar) 0, g2(vector) will return (vector) 0, g2(matrix) will return (matrix) 0. So, in your original problem, f and (first) g have different behaviours. Alberto Monteiro