0
$\begingroup$

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)

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

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 & 3<t\leq 5 \\ 8 & 7<t\leq 9 \\ 10 & t>10 \\ 2 (t-5) & 9<t\leq 10 \\ 2 (t-3) & 5<t\leq 7 \\ 2 (t-1) & 1<t\leq 3 \end{cases}$

Visualization:

Plot[int, {t, 0, 11}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ very helpful, what if I wanted to vary the rate of increase at each interval? $\endgroup$ – Sammy Jo Layko Apr 25 '19 at 16:55
  • $\begingroup$ 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. $\endgroup$ – Carl Woll Apr 25 '19 at 17:19
  • $\begingroup$ I figured it out! Thanks for all the help! $\endgroup$ – Sammy Jo Layko Apr 25 '19 at 17:20
0
$\begingroup$
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][[1]]}; 
    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][[1]]})[[1]], 
        working[[i, 2, 1]] <= x < working[[i, 2, 2]]}], {i, 2, 
        Length[working]}]; 
Piecewise[constantsSet]]

result[x_] = compound[f[x], lst, x]

enter image description here

Plot[result[x], {x, 0, 10}]

enter image description here

Should work for any well-defined function and for any set of ranges that are in order and don't touch.

| improve this answer | |
$\endgroup$

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