3
$\begingroup$

I have some lines of code that generate numerical solutions to equations. Then I want to combine two of these in a piecewise function. The way I did it is the following -lf1[r] and lf4[r] are the aforementioned numerical solutions

test[r_] := 
 Piecewise[{{lf1[r], 0.688199 <= r <= 10}, {lf4[r], 
    0.687159 <= r <= 0.688199}}]
Show[Plot[test[r], {r, 0.676319, rmax}, PlotStyle -> {Thick}, 
  BaseStyle -> {18, FontFamily -> "Times New Roman"}, 
  AxesLabel -> {"\[Rho]", "L(\[Rho])"}, 
  PlotRange -> {{0, rmax}, {0, 1.4}}], 
 Plot[x, {x, 0, 1.4}, PlotStyle -> {Thick, Black}]]

The plot is the following

enter image description here

Then I would like to have different colours in the different sectors of the piecewise function. I found some excellent answers here and I tried to adopt them - particularly in this link. However, I am facing some difficulties that I do not understand.

Example 1: Different colours, wrong plot.

pwSplit[_[pairs : {{_, _} ..}]] := 
 Piecewise[{#}, Indeterminate] & /@ pairs

pwSplit[_[pairs : {{_, _} ..}, expr_]] := 
 Append[pwSplit@{pairs}, pwSplit@{{{expr, Nor @@ pairs[[All, 2]]}}}]
pw = Piecewise[{{lf4[r], 0.687159 <= r <= 0.688199}, {lf1[r], 
     0.688199 <= r <= 10}}];

Plot[Evaluate[pwSplit@pw], {r, 0, 1}, PlotStyle -> Thick, 
 Axes -> True]

The plot is enter image description here

Example 2: This time I don't get many colours and I also get a wrong plot -if you see there is a black flat line in the bottom that should not be there

f = Piecewise[{{lf1[#], 0.688199 <= # <= 10}, {lf4[#], 
      0.687159 <= # <= 0.688199}}] &;
colorFunction = f;
piecewiseParts = Length@colorFunction[[1, 1]];
colors = ColorData[1][#] & /@ Range@piecewiseParts;
colorFunction[[1, 1, All, 1]] = colors;
Show[Plot[f[x], {x, 0, 10}, ColorFunction -> colorFunction, 
  ColorFunctionScaling -> False, PlotRange -> {{0, rmax}, {0, 1.4}}, 
  PlotStyle -> {Thick}], 
 Plot[x, {x, 0, 1.4}, PlotStyle -> {Thick, Black}]]

enter image description here

I don't understand what I am doing wrong in either case and it is not clear if I should modify something due to the fact that I have numerical functions and not analytic.

$\endgroup$
5
$\begingroup$

You can use a single plot if you separate the Piecewise conditions into separate ConditionalExpression objects.

To do this, you can use an internal function to determine the intervals of validity for each piecewise condition, and then create a list of ConditionalExpression objects. The internal, undocumented (and hence subject to change) function to use is PiecewiseDump`PWIntCases:

test[r_] := Piecewise[
    {
    {Exp[r], r<-1},
    {1-r^2, -2<r<1}, (* notice change in left endpoint! *)
    {Sin[Pi r], r>1}
    }
];
PiecewiseDump`PWIntCases[test[r],{{r,-3,3}},{}]

{{-3 < r < -1, -1 <= r < 1, 1 < r < 3, r >= 3 || r <= -3}, {E^r, 1 - r^2, Sin[π r], 0}}

Even though I changed the conditions so that they overlap, PiecewiseDump`PWIntCases is smart enough to accommodate this change (the first condition took precedence over the second). We can use the above to create a list of ConditionalExpression objects:

conditionals = MapThread[
    ConditionalExpression[#2, #1]&,
    PiecewiseDump`PWIntCases[test[r], {{r,-3,3}}, {}]
]

{ConditionalExpression[E^r, -3 < r < -1], ConditionalExpression[1 - r^2, -1 <= r < 1], ConditionalExpression[Sin[π r], 1 < r < 3], ConditionalExpression[0, r >= 3 || r <= -3]}

Each ConditionalExpression object is only valid over the given condition.

Visualization:

Plot[conditionals, {r, -3, 3}]

enter image description here

$\endgroup$
4
$\begingroup$
Clear["Global`*"]

test[r_] := 
  Piecewise[{{Exp[r], r < -1}, {1 - r^2, -1 < r < 1}, {Sin[Pi r], 
     r > 1}}];

plotRng = {-3, 3};

Extract plot intervals from Piecewise and the specified pltRng

intervals = {Cases[test[r][[1, All, -1]], _?NumericQ, 2], plotRng} // 
     Flatten // Union // Partition[#, 2, 1] &;

Plot each interval separately and combine with Show

Module[{n = 4},
 Show[Plot[test[r], {r, Sequence @@ #}, 
     PlotStyle -> ColorData[97][n++]] & /@ intervals, 
  PlotRange -> {plotRng, Automatic}]]

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.