Home > front end >  R - how to define a "symbolic sequence"
R - how to define a "symbolic sequence"

Time:09-23

Let's say that we have this sequence of numbers:

1/2, 1/3, 1/4, ..., 1/N

Sorry for the bad formatting, but the LaTeX doesn't work here. Let's say that we define a function which sums all the elements of this sequence:

Σ n = 1N 1/n

The N is supposed to be in the superscipt, but I can't align it properly here - anyway, this function would calculate the sum of all 1/n elements starting from n=1 to N.

I now want to find the limit of this function when N tends to infinity using R. For this, I was planning to use the lim function from the Ryacas package as suggested in this question.

However, I can't seem to find a way to make my script work. My idea was this:

x <- ysym("x")
sq <- 1:x
fun <- sum(1/sq)
lim(fun, x, Inf)

However, I am already getting an error at the second step of this process. I can't seem to run the sq <- 1:x:

Error in 1:x : NA/NaN argument
In addition: Warning message:
In 1:x : numerical expression has 3 elements: only the first used

So it seems that it's not possible to define a "symbolic sequence" (don't know what else I would call it) in this way.

What would be the proper way to calculate what I want?

CodePudding user response:

This series goes to infinity:

library(Ryacas)
yac_str("Sum(n, 1, Infinity, 1/n)")
# "Infinity"

Let's try a convergent series:

yac_str("Sum(n, 1, Infinity, 1/n^2)")
# "Pi^2/6"

If you really want to use Limit:

yac_str("Limit(n, Infinity) Sum(i, 1, n, 1/i^2)")

Use yac_str to execute a Yacas command.

  • Related