# Piecewise plot does not show all components

I have a function SA[zs,zh] where I want to do a ParametricPlot of a Piecewise function with the x-axis being tzs[zs,zh]. I have tried using ConditionalExpression, it produces the plot but there is a "gap" between the two piecewise components. I also tried to use Piecewise directly but it won't plot the second component (a constant) of the piecewise.

I want to know,

1. Why there is a "gap" between the two piecewise components when using ConditionalExpression and how to resolve it?
2. Why the second component of the piecewise function won't plot when using Piecewise?

I have checked my code and there seem to be no problem at all.

d = 3;
ag = 10;
pg = 10;
wp = 20;
f[z_, zh_] := 1 - (z/zh)^(d + 1);
tzsint[z_?NumericQ, zs_?NumericQ, zh_?NumericQ] := z^d/Sqrt[f[z, zh] (zs^(2 d) - z^(2 d))]
tzs[zs_?NumericQ, zh_?NumericQ] := Module[{zsr, zhr}, {zsr, zhr} = Rationalize[{zs, zh}, 0]; NIntegrate[tzsint[z, zsr, zhr], {z, 0, zsr}, AccuracyGoal -> ag, PrecisionGoal -> pg, WorkingPrecision -> wp, MaxRecursion -> 100]]
SAint[z_?NumericQ, zs_?NumericQ, zh_?NumericQ] := (zs^d/(z^d Sqrt[f[z, zh] (zs^(2 d) - z^(2 d))]))
SAintreg[z_?NumericQ, zs_?NumericQ] := (zs^d/(z^d Sqrt[(zs^(2 d) - z^(2 d))]))
SA[zs_?NumericQ, zh_?NumericQ] := Module[{zsr, zhr}, {zsr, zhr} = Rationalize[{zs, zh}, 0]; NIntegrate[SAint[z, zsr, zhr] - SAintreg[z, zsr], {z, 0, zsr}, AccuracyGoal -> ag, PrecisionGoal -> pg, WorkingPrecision -> wp, MaxRecursion -> 100]]

ParametricPlot[{ConditionalExpression[{tzs[zs, 0.1355], SA[zs, 0.1355]}, zs <= 0.1354688], ConditionalExpression[{tzs[zs, 0.1355], SA[0.1354688, 0.1355]}, zs >= 0.1354688]}, {zs, 0, 0.9999943 0.1355}, Frame -> True, FrameStyle -> Directive[Black, 20], PlotStyle -> {{Blue, Thick}, {Blue, Thick}}, PlotRange -> Full, AspectRatio -> 3/4, ImageSize -> Large]

ParametricPlot[{tzs[zs, 0.1355], Piecewise[{{SA[zs, 0.1355], zs < 0.1354688}, {SA[0.1354688, 0.1355], 0.1354688 < zs}}]}, {zs, 0, 0.9999943 0.1355}, Frame -> True, FrameStyle -> Directive[Black, 20], PlotStyle -> {{Blue, Thick}, {Blue, Thick}}, PlotRange -> Full, AspectRatio -> 3/4, ImageSize -> Large]


Using ConditionalExpression,

Using Piecewise,

\$Version

(* "12.3.1 for Mac OS X x86 (64-bit) (June 19, 2021)" *)

Clear["Global*"]

d = 3;
ag = 10;
pg = 10;
wp = 20;

SetOptions[NIntegrate,
AccuracyGoal -> ag,
PrecisionGoal -> pg,
WorkingPrecision -> wp,
MaxRecursion -> 100];

f[z_, zh_] := 1 - (z/zh)^(d + 1);
tzsint[z_?NumericQ, zs_?NumericQ, zh_?NumericQ] :=
z^d/Sqrt[f[z, zh] (zs^(2 d) - z^(2 d))]
tzs[zs_?NumericQ, zh_?NumericQ] :=
Module[{zsr, zhr},
{zsr, zhr} = Rationalize[{zs, zh}, 0];
NIntegrate[tzsint[z, zsr, zhr], {z, 0, zsr}]]
SAint[z_, zs_, zh_] :=
(zs^d/(z^d Sqrt[f[z, zh] (zs^(2 d) - z^(2 d))]));
SAintreg[z_, zs_] :=
(zs^d/(z^d Sqrt[(zs^(2 d) - z^(2 d))]))
SA[zs_?NumericQ, zh_?NumericQ] :=
Module[{zsr, zhr},
{zsr, zhr} = Rationalize[{zs, zh}, 0];
NIntegrate[SAint[z, zsr, zhr] - SAintreg[z, zsr], {z, 0, zsr}]]


Using Piecewise requires values for PlotPoints and MaxRecursion that are larger than their defaults

ParametricPlot[{tzs[zs, 0.1355],
Piecewise[{
{SA[zs, 0.1355], zs < 0.1354688},
{SA[0.1354688, 0.1355], 0.1354688 < zs}}]},
{zs, 0, 0.9999943 0.1355},
Frame -> True,
FrameStyle -> Directive[Black, 20],
PlotStyle -> {{Blue, Thick}, {Blue, Thick}},
PlotRange -> Full,
AspectRatio -> 3/4,
ImageSize -> Large,
PlotPoints -> 125,
MaxRecursion -> 10]


• Oh, but even if it can plot successfully, there is still a gap, why is it? Also, why does Piecewise require PlotPoints and MaxRecursion? Commented Sep 10, 2021 at 16:34
• To eliminate the gap increase the PlotPoints more (e.g., 200); however, there is a tradeoff between quality and speed. The greater PlotPoints and/or MaxRecursion the slower the plotting. You must decide what quality is acceptable given the complexity of the calculations involved. In any of the plotting functions the adaptive sampling can miss features. From the documentation for Plot: "Since only a finite number of sample points are used, it is possible for Plot to miss features of f. Increasing the settings for PlotPoints and MaxRecursion will often catch such features." Commented Sep 10, 2021 at 16:58
• While you provide a solution, it is not clear why Piecewise behaves differently from ConditionalExpression. Commented Sep 11, 2021 at 9:25
• @yarchik - With ConditionalExpression there are two distinct functions and the adaptive sampling is applied to each separately. With Piecewise` there is only one function (which happens to have two distinct regions) so the adaptive sampling is only applied once. Commented Sep 11, 2021 at 13:43