# How can I plot this piecewise function? [closed]

How can I plot function $$g(x)$$ given below:

The following attempt I have tried does not work and returns errors:

Clear["Global*"]
f[x_] := Piecewise[{{1, x > 0}, {0, x = =0}, {-1, x < 0}}]
g[x_?NumericQ] := x^2 f[x - 1]
Plot[g[x], {x, -10, 10}]


I expect the following output:

• Clear[f];f[x_] = Piecewise[{{1, x > 0}, {0, x == 0}, {-1, x < 0}}] == instead of =. Or using Sign instead. Commented Jun 15, 2023 at 2:30
• @cvgmt The function image drawn has errors. When x=1, the image is a segment perpendicular to the x-axis. Isn't the correct image at x=1 and y=0? Is there only one point {1,0} and a dashed line here? Commented Jun 15, 2023 at 2:41
• Use Plot[g[x], {x, -2, 2}, Exclusions -> x == 1], if you do not want a vertical line. You should not call a result wrong simply because you would like the answer in a different format. Commented Jun 18, 2023 at 4:39

Clear["Global*"]
f[x_] := Piecewise[{{1, x > 0}, {0, x == 0}, {-1, x < 0}}];
g[x_] := x^2 f[x - 1]
Plot[g[x], {x, .5, 1.5}, ExclusionsStyle -> Dashed,
Epilog -> {AbsolutePointSize[10], Point[{{1, 0}, {1, 1}, {1, -1}}],
White, AbsolutePointSize[5], Point[{{1, 1}, {1, -1}}]}]

Clear[g];
g[x_] = x^2*Sign[x - 1];
Plot[g[x], {x, .5, 1.5}, ExclusionsStyle -> Dashed,
Epilog -> {AbsolutePointSize[10], Point[{{1, 0}, {1, 1}, {1, -1}}],
White, AbsolutePointSize[5], Point[{{1, 1}, {1, -1}}]}]


We can use ExclusionsStyle -> {Dashed, AbsolutePointSize[10]} to style the excluded line and its endpoints without having to know where the discontinuities are.

Examples:

ClearAll[g, h, s]

g[x_] := x^2 Sign[x - 1]

pg = Plot[g[x], {x, 0, 1.5}, ImageSize -> 400,
ExclusionsStyle -> {Dashed, AbsolutePointSize[10]}]


We can post-process to modify/add Points:

addPoints = ReplaceAll[p_Point :>
{Point@({1, 0} # & /@ First[p]), p, White, AbsolutePointSize[7], p}];



h[x_] := x^2*Sign[x - 1/2] Sign[1 - x];

addPoints @ Plot[h[x], {x, 0, 1.5}, ImageSize -> 400,
ExclusionsStyle -> {Dashed, AbsolutePointSize[10]}]


s[x_] := Sin[2 Pi x] Sign[1/2 - Sin[2 2 Pi x]];

addPoints @ Plot[s[x], {x, 0, 1.5}, ImageSize -> 400,
ExclusionsStyle -> {Dashed, AbsolutePointSize[10]}]


Update: An alternative way to use addPoints is to make it the setting for DisplayFunction in Plot[...]:

Row[
Plot[#[x], {x, 0, 1.5}, ImageSize -> 400,
ExclusionsStyle -> {Dashed, AbsolutePointSize[10]},