2
$\begingroup$

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 the 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?

$\endgroup$

4 Answers 4

6
$\begingroup$
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}] 
$\endgroup$
7
  • $\begingroup$ 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? $\endgroup$ Commented Apr 12, 2013 at 16:50
  • $\begingroup$ @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. $\endgroup$ Commented Apr 12, 2013 at 18:27
  • $\begingroup$ @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. $\endgroup$ Commented Apr 12, 2013 at 20:57
  • $\begingroup$ @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. $\endgroup$ Commented Apr 12, 2013 at 23:48
  • $\begingroup$ @Mr.Wizard Yes,it does.Thank you Mr.Wizard. I actually typed it in standard form and then copied to here. Then,it looked like in this form. I actually tried with "copy as Input text",even then it's same. Is there any other way to show it in standard form? $\endgroup$ Commented Apr 13, 2013 at 0:20
3
$\begingroup$
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 
$\endgroup$
3
  • 7
    $\begingroup$ MovingAverage[a b, 2].Differences[c] $\endgroup$ Commented Apr 11, 2013 at 16:12
  • $\begingroup$ @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? $\endgroup$ Commented Apr 11, 2013 at 22:09
  • 1
    $\begingroup$ @TMH thats the easy part: x[i_]:=MovingAverage[Take[a,i+1] Take[b,i+1], 2].Differences[Take[c,i+1])] for example. $\endgroup$ Commented Apr 12, 2013 at 6:47
3
$\begingroup$

Based on J. M.'s comment:

MovingAverage[a b, 2] * Differences[c] // Accumulate 
{-80, 249, -191, -(739/2)} 
$\endgroup$
0
0
$\begingroup$

Just a variant:

Needs["Developer`"] Accumulate[ Times @@ MapThread[ PartitionMap[Function[x, #2.x], #1, 2, 1] &, {{a b, c}, {{1/2, 1/2}, {-1, 1}}}]] 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.