# Add Piecewise functions expanding domains

I want to reproduce these plots for further development in Mathematica: To do so I defined the functions A and B as following

A[x_ /; 0 < x < 200] := Piecewise[{{65, 0 < x < 100}, {110, 100 < x < 200}}]

B[x_ /; 0 < x < 200] := Piecewise[{{40, 0 < x < 100}, {90 , 100 < x < 200}}]

Now when I plot their sum the domain is fixed and the ranges are added together.

In the graph above the domains of A and B are added. Then in a try in each step of the new domain the minimum of A or B or A+b is returned. How one can make this graph below from A and B?

EDIT: The domain of the functions are not necessarily unique. Take this example:

A[x_ /; 0 < x < 200] := Piecewise[{{65, 0 < x < 100}, {110, 100 < x < 200}}]

B[x_ /; 0 < x < 300] := Piecewise[{{40, 0 < x < 170}, {90 , 170< x < 300}}]

• ……Where's your "graph below"? – xzczd Jan 23 '14 at 11:20
• typo...Corrected!! – Morry Jan 23 '14 at 12:21
• I think Piecewise[] is bad for this problem, because the arguments should be simpler. How about defining some sort of Line[] object. Like: line[points_,options_]:=Line[blah]. Then you can choose how points should be entered. – Coolwater Jan 23 '14 at 12:53
• I would like to have a solution which elaborates on general concepts of Mathematics, not graph analysis or other tricks! – Morry Jan 23 '14 at 13:03
• I don't understand the desired output. You state that the output is the Min of a[x/2], b[x/2] and a[x/2]+b[x/2], but this is inconsistent with the region between 300 and 400 where the output is shown as the same as a[x/2], even though b[x/2] is smaller. – bill s Jan 23 '14 at 17:29

a[x_ /; 0 < x < 200] := Piecewise[{{65, 0 < x < 100}, {110, 100 < x < 200}}]

b[x_ /; 0 < x < 200] := Piecewise[{{40, 0 < x < 100}, {90, 100 < x < 200}}]

c[x_ /; 0 < x < 400] := Switch[Mod[Quotient[x, 100], 2], 1, Max@#, 0, Min@#] &@{a[x/2], b[x/2]}

Plot[{a[x], b[x], c[x]}, {x, 0, 400},
PlotRange -> {{0, 400}, {0, 120}},
PlotStyle -> {{Dashed, Thick, Blue}, {Dashed, Thick, Magenta}, {Black}},
GridLines -> {None, Automatic}] • What if the domains are different? Something like a[x_ /; 0 < x < 300] := Piecewise[{{65, 0 < x < 170}, {110, 170< x < 300}}] – Morry Jan 23 '14 at 12:25
• @Morry We can't prescience your needs. Please try to state all your requirements in the question! – Dr. belisarius Jan 23 '14 at 12:36
• This is the solution to my problem, but the solution is not a general one.... I think there should be a more general way! – Morry Jan 23 '14 at 12:39
• @Morry I think it's as general as the question. It can be generalized in a large number of different ways (more intervals, more piecewise functions, more operations between the functions, etc)- Please state what kind of generalizations you need and we could try to fulfill those – Dr. belisarius Jan 23 '14 at 12:45
• I will edit the question..... – Morry Jan 23 '14 at 13:00

Another way to define your functions is via Interpolation. Since you want the functions to be flat, you can use the option InterpolationOrder->0 which simply connects the points with a horizontal line. Using belisarius' definition of the desired output function, this would be:

aa = Interpolation[{{0, 65}, {100, 65}, {200, 110}, {400, Infinity}}, InterpolationOrder -> 0];
bb = Interpolation[{{0, 40}, {100, 40}, {200, 90}, {400, Infinity}}, InterpolationOrder -> 0];
cc[x_] := Switch[Mod[Quotient[x, 100], 2], 1, Max@#, 0, Min@#] &@{a[x/2], b[x/2]};
Plot[{aa[x], bb[x], cc[x]}, {x, 0, 400}, PlotRange -> {0, 120}] 