I wanted to verify the derivative of the following equation:

$$ f(x) = \sum^{K_2}_{k_2=1} c_{k_2} \exp\left(- \big(a^{(2)} - t^{(2)}_{k_2}\big)^2\right)$$

for that I wanted to write a symbolic equation/representation of it so that I could manipulate it but I wasn't sure how to do that in mathematica. Ideally I'd like to get the following functionality:

$$ \frac{df(x)}{dt^{(2)}_{k_2}} = c_{k_2} \left(- \exp\big(z^{(2)}_{k_2}\big)\cdot2\big(a^{(2)}_{k_2} - t^{(2)}\big)\right)(-1)$$

so that it gave me the actual equation back symbolically (if that happens to be the correct one).

Ideally I'd like to extend it to accept vector inputs to as in:

$$ f(x) = \sum^{K_2}_{k_2=1} c_{k_2} \exp\left(- \big\| a^{(2)} - t^{(2)}_{k_2}\big\|^2\right)$$

but one thing at a time.

This is what I have tried so far. So before I try to me the upper limit of the sum symbolic, I am going to try to try it out a fixed $K_2$.

f[a_, t_, c_] = Sum[c[k2] * exp[a, t[k2]], {k2, 1, 3}]

which outputs:

enter image description here

which seems exactly correct. However, when I actually try to evaluate it and give it a vector it doesn't work as I expect:

enter image description here

instead of indexing at every location it just inserts the list for the variable, which isn't exactly what I had in mind. I was expecting something like:

$$ f = c_1 e^{-(a - t_1)^2} + c_2 e^{-(a - t_2)^2} + c_1 e^{-(a - t_1)^2} $$

$$ f = 5 e^{-(a - 1)^2} + 6 e^{-(a - 2)^2} + 7 e^{-(a - 3)^2} $$

I also tried a simple example with K2 fixed and it didn't quite work:

enter image description here

why does it return zero :/

  • $\begingroup$ @Jens I'll post that in a bit. patience. :) $\endgroup$ Aug 7, 2015 at 5:58
  • $\begingroup$ Writing your code in Mathematica format as a replacement for or in addition to the traditional format makes it easier for the community to help. $\endgroup$ Aug 7, 2015 at 12:54

2 Answers 2


If I understand the question properly, you would like to differentiate the expression

f = Sum[c[k] Exp[-(a - t[k])^2], {k, kmax}]

with respect to t[n], where n is an integer between 1 and kmax, to obtain

(* 2 E^-(a - t[n])^2 c[n] (a - t[n]) *)

Almost certainly, a similar question has been raised before, but I cannot find it now. In any case, the obvious D[f, t[n]] with n a given integer, returns 0. For instance,

Assuming [kmax > 3, D[f, t[3]]]
(* 0 *)


D[f, t[k]]
(* Sum[(2*c[k]*(a - t[k]))/E^(a - t[k])^2, {k, kmax}] *)

in effect treats all t[k] as the same variable. An inelegant and, from my perspective, unsatisfying approach does, however, produce the desired output.

D[f[[1]] /. k -> n, t[n]]    
(* 2 E^-(a - t[n])^2 c[n] (a - t[n]) *)

Of course, this works only because any given t[n] occurs only in one term of the Sum.

  • $\begingroup$ sorry if this a newbie question, but what does D[f[[1]] /. k -> n, t[n]] do? In particular, what does D[f[[1]] do? there were no parameters given to your definition of f, so how does it know what 1 and all the square brackets mean? $\endgroup$ Aug 7, 2015 at 19:43
  • 2
    $\begingroup$ @Pinocchio Please do not apologize for asking questions. No one knows everything, and we all learn by asking. f[[1]] extracts the summand, c[k] Exp[-(a - t[k])^2], from f. Try it. ` /. k -> n` then replaces the index k by a particular value n. (Actually, there is no need to do this replacement, but I did so for consistency with the explanation in the early part of the answer.) Finally, D[..., t[n]] does the actual differentiation. $\endgroup$
    – bbgodfrey
    Aug 7, 2015 at 19:59

In M11.2, the direct approach now works:

f = Sum[c[k] Exp[-(a-t[k])^2], {k, kmax}];

Assuming[i ∈ Integers && 1<=i<=kmax, Simplify @ D[f, t[i]]]

2 E^-(a - t[i])^2 c[i] (a - t[i])


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.