# How to plot weighted sum of functions?

Consider a series of functions like

Table[Cos[k t], {k, 1, 2, 1/10}]


which are summed up together with some weights

x(t) = Sum[E^-k Cos[k t], {k, 1, 2,1/10}]/Sum[E^k, {k, 1, 2, 1/10}]


I'm interested in a density plot where the t/x axes are as natural, but with the color reflecting the probability of being in that point as given by the weights of the sum.

e.g. imagine a monte-carlo simulation with lots of trajectories. One can plot a density histogram from binning space and counting how many trajectories pass in that spatial region. How is this done for analytical functions, without first evaluating each individual function on the grid? Might it be more convenient to plot mean/standard deviation?

• I'm having a hard tine understanding what you mean. What does the Cos[k t] represent? E^-k is weight, and you seem to be saying that it's the probability of being at a certain (t,x)? How? Can you please give more details as to what your equation means? Jun 12 '19 at 15:50

One possible approach:

weights = Table[E^-k / Sum[E^-k, {k, 1, 2, 1/10}], {k, 1, 2, 1/10}];


Define each term in the sum as a function:

flist[t_] := Table[E^-k Cos[k t], {k, 1, 2, 1/10}]/Sum[E^-k, {k, 1, 2, 1/10}];


Use Accumulate to get partial sums and color code each partial sum with the weight associated with its last term:

Show[Plot[Evaluate @ Accumulate[flist[t]], {t, 0, 6 Pi},
PlotPoints -> 100,
PlotStyle -> ( ColorData["Rainbow"] /@ Rescale[weights]),
ColorFunctionScaling -> False,
Filling -> Table[i -> {i + 1}, {i, 1, 10}], PlotRange -> All,
PlotLegends -> LineLegend[Automatic, Round[N@weights, .01],
LegendLayout -> {"Column", 1}]],
Plot[Sum[E^-k Cos[k t], {k, 1, 2, 1/10}]/Sum[E^-k, {k, 1, 2, 1/10}], {t, 0, 6 Pi},
PlotStyle -> Black, PlotRange -> All], ImageSize -> Large] 