如何绘制y =max⁡{g(x),x}的图

时间:2019-03-26 11:22:17

标签: r function plot graph max

我是R编程的新手,我遇到了一个数学问题,我在R中毫无头绪。

Function

该问题要求绘制y=max⁡{g(x),0.5x}的图形,以获取x的10001个值(在-10与10之间)

这是我到目前为止从t.f示例中尝试过的内容:

first.func <- function(x) { 
if (x < 0){
return(x)
}

else if (x = 0){
return(0)
}

else
return(x)
}

second.func <- function(x) {
return(max(first.func(x), x * sin(1/x)))
}

x <- seq(-10, 10, length=10001)

y <- sapply(0.5 * x, second.func)

plot(y ~ x, type = 'l')

1 个答案:

答案 0 :(得分:1)

首先定义所需的功能。获得它们之后,您可以将它们组合起来并找到y值。病态显示了一个不同的示例,因为这似乎是一个硬件问题。

first.func <- function(x) { 
  if (x > 3) { 
    return(4)
  }
  if (x <= 3) { 
    return(5 * x)
  }
}

second.func <- function(x) {
  return(min(first.func(x), 3 * x^3))
}

x <- seq(-1, 4, 0.05)

y <- sapply(x, second.func)

plot(y ~ x, type = 'l')

enter image description here