Question

I have a very simple question. I don't use Mathematica very often and I got stuck with one task. I need to compute this task:

T=5;
y (* it represents 54 numbers*);  
h = 2;
c (*starting at 3, see below*);   



Table[Sum[(y[[i]]*((i - c)/h)*((i - c)/h)), {i, T}]/
      Sum[((i - c)/h)*((i - c)/h), {i, T}], {c, 3, 54, 2}]

I need to compute the "sum…/sum…" 26 times, where "c" starts at 3 and in another step it is (3+2)-> 5 and so on (e.g. 2 steps). I managed to implement this task with Table function.

The problem is, that I also need the "i" to go from 1 to 54, but in one step it should compute just 5 numbers: 1st computing i=1,2,3,4,5 ; 2nd computing i=3,4,5,6,7 and so on. In the sum function I implemented T as 5, so in first step everything is ok, but I have no idea how to create the loop where "i" overlaps like that. I hope that someone will understand my "great" explanation.

Was it helpful?

Solution

You could write T as c+2, but your table is too long, i.e.

z = Table[c, {c, 3, 54, 2}]

{3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53}

z + 2

{5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55}

So again, if you wrote T as c+2, (and minimum i as c-2) . . .

Table[Sum[(y[[i]]*((i - c)/h)*((i - c)/h)), {i, c - 2, c + 2}]/
  Sum[((i - c)/h)*((i - c)/h), {i, c - 2, c + 2}], {c, 3, 54, 2}]

. . . you would need y to represent a list of 55 numbers, not 54.

For example, this works ok :-

y = Array[RandomInteger[10] &, 55];

Table[Sum[(y[[i]]*((i - c)/h)*((i - c)/h)), {i, c - 2, c + 2}]/
  Sum[((i - c)/h)*((i - c)/h), {i, c - 2, c + 2}], {c, 3, 54, 2}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top