I'm having a problem concerning function addition. The following code shows an example that illustrates my problem. I had this list of functions that have the same variable. I had them by using the Total function and declared a function of that variable equal to the Total[list], like in this example:

f1[x_] := 2 x + 1;
f2[x_] := x;
f3[x_] = 4 x;
l1 = List[f1[x], f2[x], f3[x]];
o[x_] := Total[l1];

The output of o[1] was 1+7x, instead of 8, which is what I want. Any suggestion on how to approach this problem? Thank you

  • $\begingroup$ If you do this, you'll get 8 f1[x_] := 2 x + 1; f2[x_] := x; f3[x_] = 4 x; l1[x_] := List[f1[x], f2[x], f3[x]]; o[x_] := Total[l1[x]]; o[1] $\endgroup$ – Nasser Oct 7 '17 at 19:32
  • 4
    $\begingroup$ On top of what @Nasser gave you, the issue with your code has to do with how variables are scoped. The x_ parameter is scoped differently from the x symbol you provide in l1. So you could also mostly keep your code but replace o with this: o[v_] := Total[l1] /. x -> v $\endgroup$ – b3m2a1 Oct 7 '17 at 19:33
  • 1
    $\begingroup$ Or include Evaluate in definition of o[x_], i.e., o[x_] := Evaluate@Total[l1]; $\endgroup$ – Bob Hanlon Oct 7 '17 at 20:03

Try this (as a side note, when defining functions using SetDelayed one doesn't have to include the semi-colon at the end of the line).

f1[x_] := 2 x + 1
f2[x_] := x
f3[x_] := 4 x

Use SetDelayed (i.e., :=) again to defrine l1

l1[x_] := List[f1[x], f2[x], f3[x]]

Inside the defintion for o use l1 with an argument

o[x_] := Total[l1[x]]

Then test

(* 8 *)

(* 15 *)

This is similar but not quite identical to what Nasser wrote in his comment.

  • $\begingroup$ Is it possible to get a specific element of the l1 list, now that it is a list of functions? $\endgroup$ – RicardoP Oct 7 '17 at 23:30
  • $\begingroup$ l1[x][[2]] would, for example, get element 2 $\endgroup$ – Jack LaVigne Oct 8 '17 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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