我需要在R中绘制以下函数:
M(x) = 2 + 0.4x {when x <= 0}
-2 + 0.6x {when x > 0}到目前为止,我已经尝试了以下几点:
fx1 = function(x){
2+0.4*x
}
fx2 = function(x){
-2-0.6*x
}
plot(fx1, -10, 0)
plot(fx2, 0, 10)但是函数是在两个不同的窗口中绘制的。我还尝试将:add=TRUE添加到第二个情节中,我在Stack溢出上读到了它,但这对我也没有帮助。
发布于 2016-09-19 12:00:23
要绘制函数,请使用curve。使用plot,在添加曲线之前获取坐标:
fx1 = function(x){
2+0.4*x
}
fx2 = function(x){
-2-0.6*x
}
plot(NA, xlim=c(-10,10), ylim=c(-10,10))
curve(fx1, from = -10, to = 0, add=TRUE)
curve(fx2, from = 0, to = 10, add=TRUE)编辑:在x=0获得更好的定义,我可以建议
fx1 = function(x) 2+0.4*x
fx2 = function(x) -2-0.6*x
plot(NA, xlim=c(-10,10), ylim=c(-10,5), ylab="value")
curve(fx1, from = -10, to = 0, add=TRUE)
curve(fx2, from = 0, to = 10, add=TRUE)
points(0, fx1(0), pch=15)
points(0, fx2(0), pch=22)https://stackoverflow.com/questions/39573198
复制相似问题