我想在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'。你不能直接繪製一個函數。 –