2
$\begingroup$

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:

enter image description here

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:

enter image description here

however, if I try to give it a list of coefficients it tries to evaluate the vector :/

enter image description here

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 :(

enter image description here

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?

$\endgroup$
4
  • 1
    $\begingroup$ All your questions waltz around the same issues, indicating that you simply haven't achieved a proper mental model of how Mathematica works. The best advice I can give you at the moment is to take the time to read through the answers to this question, which is expressly designed to help newcomers to get grounded in Mathematica fundamentals. In particular, this answer relevant to what you ask here. $\endgroup$
    – m_goldberg
    Aug 8, 2015 at 18:34
  • $\begingroup$ Differential operators in Mathematica differentiate with respect to variables, which are symbols. 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$
    – m_goldberg
    Aug 8, 2015 at 18:42
  • $\begingroup$ Note that it actually was runtime when the errors were thrown because you used Set instead of SetDelayed in defining f. $\endgroup$
    – Michael E2
    Aug 8, 2015 at 19:07
  • $\begingroup$ @m_goldberg thanks for those links. I am very happy your teaching me how to fish rather than giving me the fish. I will study this up and try to be as good as I can from the foundations up. I knew I had some fundamental misunderstanding but didn't quite know how to deal with it (hence the question). Thanks btw :) $\endgroup$ Aug 8, 2015 at 20:14

2 Answers 2

2
$\begingroup$

Here is some food for thought

f[a_, t_List, c_List] /; Length @ t == Length @ c :=
   Plus @@ MapThread[(#1 Exp[-(a - #2)^2]) &, {c, t}]

f can be used as a numerical function or to generate symbolic expressions. The clause /; Length @ t == Length @ c enforces the constraint that the vectors t and c must have the same length.

f[a, {t1, t2, t3}, {c1, c2, c3}]
c1 E^-(a - t1)^2 + c2 E^-(a - t2)^2 + c3 E^-(a - t3)^2
D[f[a, {t1, t2, t3}, {c1, c2, c3}], c1]
E^-(a - t1)^2

This may leave you wondering how to generate lists of variables. This has been asked before and is answered here.

$\endgroup$
2
  • $\begingroup$ @MichaelE2. Thanks for pointing out the typo. $\endgroup$
    – m_goldberg
    Aug 8, 2015 at 19:12
  • $\begingroup$ You're welcome. $\endgroup$
    – Michael E2
    Aug 8, 2015 at 19:17
1
$\begingroup$

Another thing to look at is the difference between = and :=. For example, if you define your first function as

f[a_, t_, c_] := Sum[c[[i]] Exp[-(a - t[[i]])^2], {i, 1, 3}]

then you get no warnings and you can evaluate

f[a, {t1, t2, t3}, {c1, c2, c3}]

as you wish. You can also take derivatives

D[f[a, {t1, t2, t3}, {c1, c2, c3}], a]

without any problem.

$\endgroup$

Your Answer

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

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