我收到错误绘制曲线:
rho<-function(t,k) ifelse(abs(t)<=k,t^2,(2*k*abs(t))-k^2)
plot(farm~land,data=farmland)
curve(rho(k=19000),xlim=c(-10,10),col="blue", add="TRUE")
Error in curve(rho(k = 19000), xlim = c(-10, 10), col = "blue", add = "TRUE") :
'expr' must be a function, or a call or an expression containing 'x'
我该如何解决?
答案 0 :(得分:3)
你可以试试这个:
rho <- function(x, k = 19000) ifelse(abs(x) <= k, x^2, (2 * k * abs(x)) - k^2)
plot(farm ~ land, data = farmland)
curve(rho, from = -10, to = 10, col = "blue", add = TRUE)
请注意,我为您的代码添加了一些空间,它更容易阅读。此外,add
参数
取逻辑值(TRUE或FALSE),而不是字符向量(“TRUE”可能会评估为TRUE,但永远不会指望它。)
另一种选择是使用lines
:
plot(farm ~ land, data = farmland)
x = seq(-10, 10, by = 0.01)
lines(x, rho(x, k = 19000), col = 'blue')