I'm working on a model that requires a sum over a function that uses various coefficients. In order to make the sum easier to perform, I am trying to keep things general through the use of a coefficient array. Consider the set-up:
c={1,2,3,4},
f1[x_,n_]:=x^2*c[[n]]
f2[x_,n_]=D[f1[x,n],x]
I need to be able to define the derivative of f1
as a new function f2
. I have two issues. First, I cannot figure out how to specify that c[[n]]
is a constant. The second is, I think that Mathematica is evaluating the differential each time f2
is called. It would be better if Mathematica would evaluate the differential and then declare f2
as the resulting derivative.
Note that f2
is not a function declaration using ":=
". The result is very different if that notation is used.
How do I specify elements obtained from a given array are constants? Can I evaluate a differential and then declare a function as the differential's result?
c
as a function over $1,\dots,4$.c[1] = 1; c[2] = 2; ...
$\endgroup$c
as functionc[n_?NumericQ] := {1, 2, 3, 4}[[n]]
$\endgroup$ClearAll@c; Evaluate[Array[c, 4]] = {1, 2, 3, 4}
and usec[n]
instead ofc[[n]]
. I prefer that one. If you're going to use @swish's answer, I would recommendc[n_Integer]
in place ofc[n_?NumericQ]
. $\endgroup$