f1<-function(t)
{
sqrt((t^2)+1)
}
curve(f1,from=0,to = 5,n=10)
plot(f1,from=0,to = 5,n=10)
曲线函数和绘图函数输出结果相同,那么它们的区别在哪里呢?
f1<-function(t)
{
sqrt((t^2)+1)
}
curve(f1,from=0,to = 5,n=10)
plot(f1,from=0,to = 5,n=10)
函数并不多。但是plot
最终会调用curve
。
plot
是一个通用函数,这意味着它有多个方法,具体取决于传递给它的对象的类(在这种情况下,是一个函数)。要查找特定方法背后的代码,可以键入graphcs:::plot.<method>
。
在这种情况下,您可以看到plot
应用于函数时,首先检查和调整其参数,最终只调用curve
。
> graphics:::plot.function
function (x, y = 0, to = 1, from = y, xlim = NULL, ylab = NULL,
...)
{
if (!missing(y) && missing(from))
from <- y
if (is.null(xlim)) {
if (is.null(from))
from <- 0
}
else {
if (missing(from))
from <- xlim[1L]
if (missing(to))
to <- xlim[2L]
}
if (is.null(ylab)) {
sx <- substitute(x)
ylab <- if (mode(x) != "name")
deparse(sx)[1L]
else {
xname <- list(...)[["xname"]]
if (is.null(xname))
xname <- "x"
paste0(sx, "(", xname, ")")
}
}
curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab,
...)
}