0

I am trying to plot a besselJ() function of order 0 (nu = 0) for x = 0 to x = 20 in R (working in RStudio). Here is my current code:

plot (
  x = NULL,
  xlim = c(0, 20),
  ylim = c(-0.4, 1),
  main = "Plot of Bessel functions",
  xlab = "x",
  ylab = "J_nu(x)"
)


# grid

grid(
col = "gray60",
lwd = 1.5
)

# horizontal reference line

segments(
    x0 = 0,
    y0 = 0,
    x1 = 20,
    y1 = 0,
    lty = "solid",
    lwd = 2,
    col = "gray50"
)


# vertical reference line

segments(
    x0 = 0,
    y0 = -0.4,
    x1 = 0,
    y1 = 1,
    lty = "solid",
    lwd = 2,
    col = "gray50"
)


curve(
    besselJ(0:20, 1),
    lty = "solid",
    lwd = 3,
    col = "salmon2",
    add = TRUE
)

This results in the following error:

Error in curve(besselJ(0:20, 1), lty = "solid", lwd = 3, col = "salmon2", : 'expr' must be a function, or a call or an expression containing 'x'

My working thought is that besselJ() returns a value y for a particular value x, which is why curve() is not treating besselJ as a 'function' Anyone else have other ideas?

Dragon-Ash
  • 71
  • 1
  • 1
  • 9
  • 1
    try `curve(besselJ(x, 1), ...)`, or generate the curve yourself and use `lines()` to overlay it. `x` is a "magic" variable in this context – Ben Bolker Jul 31 '22 at 00:51
  • Txs, that did the trick; I thought I had to explicitily call x = 1 through 20 – Dragon-Ash Jul 31 '22 at 02:51
  • Moral: Read the error code very carefully. It said your call had to have an "x" argument. – IRTFM Jul 31 '22 at 04:26

0 Answers0