2
$\begingroup$

I want to reproduce these plots for further development in Mathematica:

enter image description here

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}}]

$\endgroup$
  • 1
    $\begingroup$ ……Where's your "graph below"? $\endgroup$ – xzczd Jan 23 '14 at 11:20
  • $\begingroup$ typo...Corrected!! $\endgroup$ – Morry Jan 23 '14 at 12:21
  • $\begingroup$ 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. $\endgroup$ – Coolwater Jan 23 '14 at 12:53
  • $\begingroup$ I would like to have a solution which elaborates on general concepts of Mathematics, not graph analysis or other tricks! $\endgroup$ – Morry Jan 23 '14 at 13:03
  • 1
    $\begingroup$ 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. $\endgroup$ – bill s Jan 23 '14 at 17:29
3
$\begingroup$
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}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ What if the domains are different? Something like a[x_ /; 0 < x < 300] := Piecewise[{{65, 0 < x < 170}, {110, 170< x < 300}}] $\endgroup$ – Morry Jan 23 '14 at 12:25
  • $\begingroup$ @Morry We can't prescience your needs. Please try to state all your requirements in the question! $\endgroup$ – Dr. belisarius Jan 23 '14 at 12:36
  • $\begingroup$ This is the solution to my problem, but the solution is not a general one.... I think there should be a more general way! $\endgroup$ – Morry Jan 23 '14 at 12:39
  • $\begingroup$ @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 $\endgroup$ – Dr. belisarius Jan 23 '14 at 12:45
  • $\begingroup$ I will edit the question..... $\endgroup$ – Morry Jan 23 '14 at 13:00
0
$\begingroup$

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}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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