6
$\begingroup$

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

enter image description here

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

enter image description here

enter image description here

I expect the following output:

enter image description here

$\endgroup$
3
  • $\begingroup$ Clear[f];f[x_] = Piecewise[{{1, x > 0}, {0, x == 0}, {-1, x < 0}}] == instead of =. Or using Sign instead. $\endgroup$
    – cvgmt
    Jun 15, 2023 at 2:30
  • $\begingroup$ @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? $\endgroup$
    – csn899
    Jun 15, 2023 at 2:41
  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Jun 18, 2023 at 4:39

2 Answers 2

12
$\begingroup$
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}}]}]

enter image description here

$\endgroup$
10
$\begingroup$

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

enter image description here

We can post-process to modify/add Points:

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

addPoints @ pg

enter image description here

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

enter image description here

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

enter image description here

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]}, 
     DisplayFunction -> addPoints]& /@
   {g, h, s}, 
 Spacer[10]]

enter image description here

$\endgroup$

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