Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have three lists. As an example,

a = {10, 5, 6, 8, 7};
b = {4, 8, 9, 7, 9};
c = {7, 5, 12, 4, 1};

I want to get $$x(i)=\sum _{i=0}^i\frac{1}{2}\left(a_ib_i+a_{i+1}b_{i+1}\right)\left(c_{i+1}-c_i\right)$$ i goes from 1 to 4. i.e. I want to get: x(1),x(2),x(3) and x(4)

So, I used following code

x=Sum[1/2*(a[[i]]*b[[i]] + a[[i + 1]]*b[[i + 1]])*(c[[i + 1]] - 
c[[i]]), {i, 1, 4}]
For[i = 0, i < 4, x, Print[x]]

But, I didn't get expected output. Could anyone please tell me how to do this?

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted
Table[Sum[1/2*(a[[i]]*b[[i]]+a[[i+1]]b[[i+1]])*(c[[i+1]]-c[[i]]), {i,1,k}],{k, 1, 4}]
share|improve this answer
    
Thank you so much. That is what I want. –  TMH Apr 11 '13 at 22:52
    
Since I want to find $$y (i) = (Subscript[a, i + 1] - Subscript[a, i])/( Subscript[b, i + 1] - Subscript[b, i])$$ I tried with this code: Table[(a[i + 1] - a[i])/(b[[i + 1]] - b[[i]]), {i, 1, k}], {k, 1, 4} But I didn't get an error. Could you please tell me how to do this? –  TMH Apr 12 '13 at 16:50
    
@TMH you seem to be confusing a[i] and a[[i]] in that code. Try: Table[Sum[(a[[i + 1]] - a[[i]])/(b[[i + 1]] - b[[i]]), {i, 1, k}], {k, 1, 4}] ; incidentially this looks rather nice if you select the expression and convert to StandardForm. –  Mr.Wizard Apr 12 '13 at 18:27
    
@Mr.Wizard Thank you. It works. but, I don't want a sum. So, I removed it. I actually don't know how to convert the expression into standard form. Really appreciate your help on that too.Thanks again. –  TMH Apr 12 '13 at 20:57
    
@TMH I see. Does the rest of the code work for you? You can make the conversion from the menu Cell > Convert To or with the keyboard shortcuts listed therein. –  Mr.Wizard Apr 12 '13 at 23:48
show 3 more comments
Sum[1/2*(a[[i]]*b[[i]] + a[[i + 1]]*b[[i + 1]])*(c[[i + 1]] - c[[i]]), {i, 1, 4}]
(*
-(739/2)
*)

But I think this is better:

Tr[Differences@c (Most[a b] + Rest[a b])]/2
share|improve this answer
7  
MovingAverage[a b, 2].Differences[c] –  J. M. Apr 11 '13 at 16:12
    
@J.M. and belisarius Thank you for the answers. But, I want to get x values for each i values. i.e. I want to get values for x(1),x(2),x(3) and x(4) Could you please tell me how to do that? –  TMH Apr 11 '13 at 22:09
1  
@TMH thats the easy part: x[i_]:=MovingAverage[Take[a,i+1] Take[b,i+1], 2].Differences[Take[c,i+1])] for example. –  Spawn1701D Apr 12 '13 at 6:47
add comment

Based on J. M.'s comment:

MovingAverage[a b, 2] * Differences[c] // Accumulate
{-80, 249, -191, -(739/2)}
share|improve this answer
    
Thank you Mr.Wizard. –  TMH Apr 12 '13 at 1:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.