# How can I define a function which is the sum of other functions?

I have a function which is summation of another function over different i,j iterators.

U[r] = Sum[V[r, i, j], {i, 1, 5}, {j, 6, 10}]


and V[r, i, j] function depends on the values of i and j (has different parameters depending on value of i & j)

If[2 <= i <= 5 &&  7 <= j <= 10,
Evaluate[vhh[i_,j_,r_] = -ehh (1 - {1 - Exp[-Ahh (ra[i, j, r] - rshh)]}^2)],
vhh = 0];
If[i == 1 &&  7 <= j <= 10,
Evaluate[vch[i_, j_, r_] = -ech (1 - {1 - Exp[-Ach (ra[i, j, r] - rsch)]}^2)],
vch = 0];
If[2 <= i <= 5 &&  j == 6,
Evaluate[vch[i_, j_, r_] = -ech (1 - {1 - Exp[-Ach (ra[i, j, r] - rsch)]}^2)],
vch = 0];
v[1, 6, r_] := -ecc (1 - {1 - Exp[-Acc (ra[1, 6, k, o] - rscc)]}^2);


And v[i,j,r] is sum of vch, vhh and vcc. so U[r] will be

U[r_]:=Sum[vhh[i_,j_,r_] + vch[i_,j_,r_]+ vcc[i_,j_,r_], {i,1,5},{j,6,10}]


(not defined Vhh or Vcc values are considered 0) The above code isn't intended to work in Mathematica; it is just pseudo-code for sake of defining my problem.

I was thinking of defining three different functions, Vhh[r], Vch[r] and Vcc[r], and summing the three functions into the final function, U, but it doesn't work (see below error). Mathematica doesn't allow me to add 2 or more functions to a third one. Do you have any ideas how to define U function in Mathematica?

U[r_] := Vhh[r] + Vch[r] + Vcc[r]


SetDelayed::write: Tag C in U[r_] is Protected.

-
Since you don't describe the failure mode when your say " ... but it doesn't work", it's hard to diagnose you problem, but here is a piece of general advice: use U[r_] := Vhh[r] + Vch[r] + Vcc[r]. Note the := in place of =, it makes an important difference. And use := when you define Vhh, Vch and Vcc, as well. – m_goldberg May 8 '13 at 0:44
Sometimes you can get the Tag...Protected error when you define a symbol in multiple ways in the same session and they happen to be incompatible. If this is the case, simply Clear[U] before evaluating the one you really want. – bill s May 8 '13 at 6:54

## 2 Answers

Mathematica can not Sum functions

simply

Clear["Global*"]
A[x_] := X + 1;
B[y_] := y + 3;
C[r_] := A[r] + B[r]


Will give you error of

SetDelayed::write: Tag C in C[r_] is Protected. >>

Try making the function out summations directly, your function should look like

U[r_] := Sum[-ehh (1 - (1 - Exp[-Ahh (ra[i, j, r] - rshh)])^2), {i, 2,
5}, {j, 7, 10}] +
Sum[-ech (1 - (1 - Exp[-Ach (ra[i, j, r] - rsch)])^2), {i, 1,
1}, {j, 7, 10}] +
Sum[-ech (1 - (1 - Exp[-Ach (ra[i, j, r] - rsch)])^2), {i, 2,
5}, {j, 6, 6}] +
ecc (1 - (1 - Exp[-Acc (ra[1, 6, r] - rscc)])^2);


And avoid { in definition of your function.

-
The error here is because C is a protected symbol. Try using lowercase c instead. – Simon Woods May 8 '13 at 8:36

You did not provide a definition of v or ra which would be helpful, but I suspect this may at least help you move in the right direction:

vhh[i_, j_, r_] /; 2 <= i <= 5 && 7 <= j <= 10 :=
-ehh (1 - {1 - Exp[-Ahh (ra[i, j, r] - rshh)]}^2)
vhh[__] := 0

vch[i_, j_, r_] /; i == 1 && 7 <= j <= 10 :=
-ech (1 - {1 - Exp[-Ach (ra[i, j, r] - rsch)]}^2)
vch[__] := 0

vcr[i_, j_, r_] /; 2 <= i <= 5 && j == 6 :=
-ech (1 - {1 - Exp[-Ach (ra[i, j, r] - rsch)]}^2)
vcr[__] := 0


These functions are defined for three parameters, while your apparent uses in U[r_] = Vhh[r] + Vch[r] + Vcc[r]` has only one, therefore I do not know how to proceed.

-