我想在R中建立一个函数,它代表范围[0,1]范围内有100Hz的正方形和锯齿波。我尝试这样做:编程方波和锯齿波在R
squarewave <- function (t) {
# 0.01 == 100Hz(=2Pi) -> 1 Period of the Squarewave
# 0.005 == Pi -> Half Period of the Squarewave
# if t smaller than a half period -> 1
# if t greater or equal than half a period -> 0
if ((t %% 0.01) < 0.005)
return (1)
else if ((t %% 0.01) >= 0.005)
return (0)
}
当我尝试绘图使用此功能:
plot(squarewave)
我得到以下错误:
> plot(squarewave)
Error in curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab, :
'expr' has not been evaluated to an object of length 'n'
In addition: Warning message:
In if ((t%%0.01) < 0.005) return(1) else if ((t%%0.01) >= 0.005) return(0) :
the condition has length > 1 and only the first element will be used
那么,为什么这不是工作?
用英文发表您的错误,而不是德文。 – Naruto
当您绘制它时,您没有将任何参数传递给'squarewave'。你不能直接绘制一个函数。 –