R 中函数值的总和
我正在尝试计算以下内容(图像显示 f(n) = n \sum_{i=1}^{\infty} (c(i)*(1-c(i))^n)):
其中 c(i) 是
c <- function(i){1/i^3}
换句话说,f(2) 是 2*{1^(-3)(1-1^(-3))^2+2^( -3)(1-2^(-3))^2+3^(-3)(1-3^(-3))^2+4^(-3)(1-4^(-3))^2+...}。
如何在R中编写这样的f函数?
我最初的尝试是:
f <- function(n){n*sum(c*(1-c)^n)}
但这显然是错误的,
Error in 1 - c : non-numeric argument to binary operator
如果需要进一步澄清,请告诉我。谢谢。
I am trying to calculate the following ( the image says f(n) = n \sum_{i=1}^{\infty} (c(i)*(1-c(i))^n)):
where c(i) is
c <- function(i){1/i^3}
In other words, f(2) is 2*{1^(-3)(1-1^(-3))^2+2^(-3)(1-2^(-3))^2+3^(-3)(1-3^(-3))^2+4^(-3)(1-4^(-3))^2+...}.
How to write such an f function in R?
My initial attempt is:
f <- function(n){n*sum(c*(1-c)^n)}
but this is obviously wrong with error
Error in 1 - c : non-numeric argument to binary operator
Please let me know if further clarification is needed. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,除非您通过分析来解决它,否则您无法获得无限和,但由于我们可以看到它是一个收敛和,因此我们可以像这样看第一个百万:
这允许:
如果您担心这个不够准确,我们通过仅对前 10,000 项求和就得到了 7 位数字的相同结果,因此 100 万应该非常接近收敛值。
Clearly, you can't get an infinite sum unless you tackle it analytically, but since we can see that it's a convergent sum, we could look at, say, the first million like this:
Which allows:
In case you are worried that this is not accurate enough, we get the same result out to 7 digits by summing only the first 10,000 terms, so 1 million should be very close to the converged value.