0

I thought it should be an easy one but I have been scratching my head and couldn't find the correct way.

I would like to calculate a summation of series A:

A<-*summation((i=2 to i=s)*K(c1+c2(i-1)))

where k, c1 & c2 are fixed values.

Expand A, i would like to get the sum of all these:

K(c1+c2(2-1))+K(c1+c2(3-1))+.......K(c1+c2(s-1))

To do that in R, here is what I wrote:

A<-function(s){
    for (i in 2:s){
    c1=5
    c2=13.6
    k=10
    sum(k*(c1+c2*(i-1)))
}}

but when I do

A(5)

it didn't come up with anything

So I modified the function and asked it to print what it did:

A<-function(s){
    for (i in 2:s){
    c1=5
    c2=13.6
    k=10
    a<-sum(k*(c1+c2*(i-1)))
    print(a)
}}

> A(5)
[1] 186
[1] 322
[1] 458
[1] 594

Apparently it didn't "sum" all of them but calculated them independently.

So what exactly is the correct codes for summation?

Thanks.

lamushidi
  • 303
  • 3
  • 5
  • 14
  • I'm worried. Very worried ... that you think the "(n)" form is somehow connected with picking the n-th item from a vector of value. In R that is done with "[n]" and not "(n)". – IRTFM May 04 '13 at 01:51

1 Answers1

3

Perhaps like this:

K <- 10
c1 <- 5
c2 <- 13.6

sum(K * (c1 + (c2 * (1:4))))
joran
  • 169,992
  • 32
  • 429
  • 468