Summary:
tldr; I just want to implement a version of the following mathematics function:
$$ f(a) = c_1 e^{-(a - t_1)^2} + c_2 e^{-(a - t_2)^2} + c_3 e^{-(a - t_3)^2}$$
in mathematica, and be able to take derivatives of it in a correct manner and supply list of coefficients (and $t$'s) to it so that it evaluates it correctly.
Full question:
I was trying to create a function, that took as an input a vector and given that vector performed operations on it. In particular I wanted to give a vector to specify the exact coefficients to evaluate for a linear combination of decaying exponential functions as in:
f[a_, t_, c_] = Sum[ c[[i]] Exp[-(a - t[[i]])^2], {i, 1, 3}]
the first issue that I had was that when I defined the above, it immediately threw me warnings:
this seams counter intuitive to me because, in other languages, if the indices depend on length of the object...it doesn't actually throw the error messages until run time. Is it possible to stop mathematica from throwing me obvious error messages? Or am I defining my function incorrectly?
Furthermore, something that I also noticed is that I might not be aware what the difference between the following two:
c[i]
and
c[[i]]
is. For example, when I define the version of the function f I gave above with both c[i]
and c[[i]]
they behave differently and I sort of understand the difference but I can't get the functionality I want, which is be able to give lists as inputs AND also take derivatives freely as I wish. For example if I define:
f[a_, t_, c_] = Sum[ c[i] Exp[-(a - t[i])^2], {i, 1, 3}]
then I am able to take derivatives of the above expression freely and correctly as it should:
however, if I try to give it a list of coefficients it tries to evaluate the vector :/
which clearly doesn't make sense (what does it even mean to evaluate a vector/list anyway :/ ).
But if I use the other definition f[a_, t_, c_] = Sum[ c[[i]] Exp[-(a - t[[i]])^2], {i, 1, 3}]
then I can't take derivatives :(
So basically, I concluded that even though I've done research to find what the different brackets mean, I don't quite understand this behaviour of mathematica. As far as I understand c[i]
means to evaluate the function c with input i and f[[i]]
means "indexing" using the Part
function. Though, thats all I know and can't connect what the issue is.
Can someone explain to me how I can create a function that takes vectors of coefficients but also takes derivatives with respect to its variables in a normal way?
c[[1]]
is not a symbol; it is the shortcut form of the expression,Part[c, 1]
, a call to a built-in function. You can not differentiate with respect to that. $\endgroup$Set
instead ofSetDelayed
in definingf
. $\endgroup$