# Applying a function to a list (compounding) [closed]

I'd like to create a function where it's applied to sections of the listed ranges, while the portions between the "step up" maintain the last value. For example: If I were to want to apply f[x]=2x to the list {{1,3},{5,7},{9,10}} from 0 to 1 it would be y=0, 1 to 3 would have slope of 2, from region 3 to 5 would have zero slope but still holding y=4 (because (3-1)(2)=4), then 5 to 7 would have increased slope of 2 (y would go from 4 to 8, and then be held constant from 7 to 9) and so on.

(I'm keeping it with the list so that I can edit where I want the function to "step up" and for how long)

• Sorry, it is totally unclear for me want you want. Please try to give a precise mathematical description. – Henrik Schumacher Apr 24 '19 at 16:55

If I understand correctly, you are defining the slope of a function to be 2 when it is a member of your intervals, and 0 otherwise. This can be encoded as:

f[x_] := Piecewise[{{2, {x} ∈ Interval[{1, 3}, {5, 7}, {9, 10}]}}]


Then, you want to integrate this function from 0 to some value t:

int = Integrate[f[x], {x, 0, t}, Assumptions->t>0];
int //TeXForm


$$\begin{cases} 4 & 310 \\ 2 (t-5) & 9

Visualization:

Plot[int, {t, 0, 11}] • very helpful, what if I wanted to vary the rate of increase at each interval? – Sammy Jo Layko Apr 25 '19 at 16:55
• Use something like f[x_] := Piecewise[{{2, 1<x<=3}, {4, 5<x<=7}, {6, 9<x<=10}}] to have a slope of 2, 4, and 6. – Carl Woll Apr 25 '19 at 17:19
• I figured it out! Thanks for all the help! – Sammy Jo Layko Apr 25 '19 at 17:20
lst = {{1, 3}, {5, 7}, {9, 10}};
f[x_] = 2 x;

compound[fn_, list_, x_] :=
Block[{working = If[list[[1, 1]] == 0, {}, {{c, {0, list[[1, 1]]}}}],
constantsSet, c},
Do[Block[{},
AppendTo[working, {fn + c, {list[[i, 1]], list[[i, 2]]}}];
AppendTo[working, {c, {list[[i, 2]], list[[i + 1, 1]]}}]], {i,
Length[list] - 1}];
AppendTo[working, {fn + c, {list[[Length[list], 1]],
list[[Length[list], 2]]}}];
AppendTo[working, {c, {list[[Length[list], 2]], \[Infinity]}}];
constantsSet = {working[[1, 1]],
working[[1, 2, 1]] <= x <
working[[1, 2, 2]]} /. {Solve[(working[[1, 1]] /. x -> 0) == 0,
c][]};
Do[AppendTo[
constantsSet, {(working[[i,
1]] /. {Solve[(constantsSet[[i - 1, 1]] /. {x ->
working[[i, 2, 1]]}) == (working[[i, 1]] /. {x ->
working[[i, 2, 1]]}), c][]})[],
working[[i, 2, 1]] <= x < working[[i, 2, 2]]}], {i, 2,
Length[working]}];
Piecewise[constantsSet]]

result[x_] = compound[f[x], lst, x] Plot[result[x], {x, 0, 10}] Should work for any well-defined function and for any set of ranges that are in order and don't touch.