Problems plotting a custom function in R -
i program , plot function r
:
the code have been using is:
js <- sapply( seq(1,50, 1), fun <- function(x) x^{-4}) pim <- sapply(seq(1,50, 1), fun <- function(x) 2*pi*x) beta <- function(x) sum(js*sqrt(2)*cos(pim*x))
but while returns right values each point, running trouble when try plot using curve function is:
error in curve(beta, 0, 1) : 'expr' did not evaluate object of length 'n' in addition: warning messages: 1: in pim * x : longer object length not multiple of shorter object length 2: in js * sqrt(2) * cos(pim * x) : longer object length not multiple of shorter object length
could please me fix problem , working plot? thank you.
the error in beta
function. when use sum
takes sum of entire vector t = 1,...,t. not want this. instead, want evaluate function each t. so, small change beta
can use curve
function.
js <- sapply( seq(1,50, 1), function(x) x^{-4}) pim <- sapply(seq(1,50, 1), function(x) 2*pi*x) beta <- function(x) { sapply(x, function(y) sum(js*sqrt(2)*cos(pim*y))) } curve(beta, 0 ,1)
Comments
Post a Comment