6
$\begingroup$

I searched already a lot about indexed Variables, and it tends to be the most applicable way to use tensor notation. But I am having a hard time to solve for undefined indexed variables:

Here my stylized problem:

F := Table[f[i], {i, 1, n}];

The Derivative gives the correct answer if applied for a specified i=1

D[Sum[f[i], {i, 1, 100}], f[1]]

=1

If I want to solve more generally let say $ i \in [1, 100] $

D[Sum[f[i], {i, 1, 100}], f[i]]

=0

Obviously does not work, how can I solve such a problem generally?

$\endgroup$

1 Answer 1

11
$\begingroup$

The variable i is a dummy one. The evaluated expression:

Sum[f[i], {i, 1, 10}]

f[1] + f[2] + f[3] + f[4] + f[5] + f[6] + f[7] + f[8] + f[9] + f[10]

contains no explicit variable f[i], hence, the result is 0.

Try to first Inactivate the sum, and only then to calculate the derivative:

expr1 = D[Inactivate[Sum[f[i], {i, 1, 100}], Sum], f[i]]

The result is

enter image description here

Now let us activate it and select a certain value of i:

Activate[expr1] /. i -> 20

(*  1   *)

Now, one can combine all that into a function, like this:

dif[f_, j_] := (D[Inactivate[Sum[f[i], {i, 1, 100}], Sum], f[i]] // 
     Activate) /. i -> j;

Let us check it up:

dif[g, 30]

(*  1  *)

Have fun!

$\endgroup$
8
  • $\begingroup$ Hey, thanks for the fast help! I see that this workaround works good for that specific case. In my field, however, I am exposed quite a lot to unspecified indexed variables. Is there maybe a more direct way i.e to put a placeholder on the specific dummy variable place. Something like: D[Sum[f[i], {i, 1, 100}], f[0<i<100]] Or: D[Sum[f[i], {i, 1, 100}], f[i_PositiveInteger]] ? $\endgroup$
    – oyy
    Commented Nov 26, 2020 at 11:32
  • $\begingroup$ This way seems to be (1) quite direct and (2) rather flexible. Another thing that from your example I cannot imagine other terms that you need to treat. You had in mind probably - a more short way? Not that I know. The only approach that seems me reasonable is to help Mma a bit. For example, as a first step calculate the derivative of a single term on the screen prior of doing the summation (*step1*) expr2=D[expr1,f[i]]. As a second one, make a summation on the screen.(*step2*) Sum[expr2,{i,1,100}]] $\endgroup$ Commented Nov 26, 2020 at 12:23
  • $\begingroup$ (A corrected comment. Discard the previous one) This way seems to be (1) quite direct and (2) rather flexible. Another thing that from your example I cannot imagine other terms that you need to treat. You had in mind probably - a more short way? Not that I know. The only approach that seems me reasonable is to help Mma a bit. For example, calculate the derivative of a single term on the screen without doing the summation expr2=D[expr1,f[i]]. Then see, if the summation is still necessary, and do it, if yes. $\endgroup$ Commented Nov 26, 2020 at 12:30
  • 1
    $\begingroup$ Exactly this I tried to propose. Instead of differentiating Sum[expr[f[i]],{i,1,100}]], you can differentiate the term of the sum: D[expr[f[i]], f[i]]. This yields the desired result in your case. In your example expr[f[i]]=f[i] but in general, it may be any function of f[i]. If you have a double summation or another more complex expression, you may need to later still calculate a sum. But do it after you have already calculated the derivative. $\endgroup$ Commented Nov 26, 2020 at 13:43
  • 1
    $\begingroup$ No this is impossible. The reason is simple. In your last code you have Solve[f[x[1],x[2] ]==0,x[i] ]. That is, the left-hand part of the equation, f[x[1],x[2] ] depends on x[1] and x[2] . Mma, however, will look for its dependence on x[i] in the explicit form. And it will not find such. Hence, the result will be "No solution". If instead you try Solve[f[x[i],x[j] ]==0,x[i] ] it will work. $\endgroup$ Commented Nov 26, 2020 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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