绘制图表R.

时间:2018-02-05 18:39:19

标签: r graph trigonometry

尝试在R上绘制图表,需要在同一图中绘制2个图表

f(x) = sin(x)+pi/4, -2*Pi <= x <= 2*Pi 

g(x) = {sin(x), 0<= x <= Pi or -2*Pi <= x <= 2*Pi
       {-Pi/4             elsewhere.

我相信我已正确制定f(x),但不明白如何plot g(x)一点

f(x)我有:

t<-seq(from=2*pi, to="*pi, length=500)
plot(sin(t)+pi/4

很抱歉不确定如何编辑,我们将不胜感激。

任何关于如何像g(x)那样披露图形的指针都会很棒,只是为了让你知道这是作业,所以不要指望任何人这样做只是如何绘制这种函数的指针会很棒,谢谢你。

2 个答案:

答案 0 :(得分:2)

我会使用curve函数:

curve(sin(x) + pi / 4, from = -2 * pi, to = 2 * pi, ylim = c(-2, 2))

curve(sin(x), from = 0,       to = pi,     add = TRUE)
curve(sin(x), from = -2 * pi, to = 2 * pi, add = TRUE)

答案 1 :(得分:0)

另一种选择可能是为g(x编写自定义函数,并在expr函数中将其用作curve

calculate_gx <- function(x){
  ifelse(x <= (2*pi) & x >= (-2*pi), sin(x), -pi/4 )
}

curve(calculate_gx, from = -3*pi, to = 3 * pi, n = 200)

结果:

enter image description here