# How to plot a function with different conditions (if or which or piecewise)

The given function is This function is plotted to be V[x_, r_] :=
Piecewise[{{-Sqrt r + Sqrt[r^2 - x^2],
Abs[x] <= Sqrt/2 r}, {-1 + Sqrt x - Sqrt[
r^2 - (Abs[x] - 1)^2], Abs[x] >= 1 - Sqrt/2 r}, {-Abs[x],}}]


My problem is how to write the otherwise condition as given in the paper for the Mathematica and plot the function for different values of r.

• -1 + Sqrt x - Sqrt[ r^2 - (Abs[x] - 1) should be -1 + Sqrt r - Sqrt[r^2 - (Abs[x] - 1)^2]
• The otherwise condition -Abs[x] is at the end of all List.
expr = Piecewise[{{-Sqrt r + Sqrt[r^2 - x^2],
Abs[x] <= Sqrt/2 r}, {-1 + Sqrt r -
Sqrt[r^2 - (Abs[x] - 1)^2], Abs[x] >= 1 - Sqrt/2 r}}, -Abs[
x]]
Plot[Table[expr, {r, {.01, .1, .2}}] // Evaluate, {x, -1, 1},
PlotRange -> {-1, 0}, PlotStyle -> {Black, Red, Blue}, Frame -> True,
Axes -> False,
PlotLegends ->
Placed[LineLegend[{Black, Red, Blue}, {"r=0.2", "r=0.1", "r=0.01"},
LegendMarkers -> "OpenMarkers"], {.2, .8}]] • It is not necessary to repeat the styles in the PlotLegends, e.g., PlotLegends -> Placed[(HoldForm[r = #] & /@ {0.2, 0.1, 0.01}), {.175, .8}] I also recommend including PlotRangePadding -> Scaled[.02] Sep 8, 2022 at 13:48
• @BobHanlon Thanks, I also don't know how to add LegendMarkers since I found that LegendMarkers->Automatic can not work. Sep 8, 2022 at 14:03