# Issue with mathematica derivative with sum

I need to obtain set of derivatives for the function f and so I use the command:

f = z (Sum[x[i], i])^2 + y Sum[x[i], i]+c;
D[f,{{x[i],y}}];


However, the answer I get is {0,0}. The derivatives individually give

D[f, #] & /@ {x[i], y}
= {i y + 2 i z Sum[x[i],i], Sum[x[i],i] }


which makes sense to me.

However, for a more complicated application I need to use a command of the form:

D[function, {vars1_list}, {vars2_list}]


where vars1 and vars2 are list of variables with respect to which I wish to find the derivative. In addition to the above when I try to use Table for the same f with

l1 = {x[i], y}
l2 = {z,c}

Table[D[f, l1[[1]], l2[[j]]], {i, 1, 2}, {j, 1, 2}]


gives me all zeroes again.

Any suggestions ?

• Please post code as text only. Do not mix it with LaTeX formatting. It must be possible for us to copy it, paste it in our own notebook and execute it as is. Jun 28, 2018 at 17:55
• Note that the i in Sum[x[i],i] is not the same as the i in x[i]. I don't think that Mathematica can support this sort of differentiation out of the box. Jun 28, 2018 at 19:44
• Sorry about that MarcoB. I will keep that in mind next time. Jun 28, 2018 at 22:17

Mathematica can take derivatives of finite sums as long as it is a definite sum, i.e., a sum with limits. I will take the liberty of modifying your expression to use a definite sum:

expr = z Sum[x[i], {i, n}]^2 + y Sum[x[i], {i, n}] + c;
expr //TeXForm


$c+y \sum _i^n x(i)+z \left(\sum _i^n x(i)\right){}^2$

Clearly the symbol i here is just a dummy variable, so when taking a derivative it would be better to use a different symbol, say j. Also, Mathematica does not know what the relationship between j and n is, and it also doesn't know that j is an integer. If we add these constraints and differentiate:

res = Simplify[
D[expr, {{x[j], y}}],
j \[Element] Integers && 1 <= j <= n
];
res //TeXForm


$\left\{2 z \sum _i^n x(i)+y,\sum _i^n x(i)\right\}$

we get a nice result.

• Thank you very much Carl. It worked... Jun 28, 2018 at 22:16