5
$\begingroup$

I have this function for $0<x<20$

f[x_] := Cos[x] + 2 Cos[2 x];

and I want to graphically represent the points where this function is zero, as a thin line stripe (not circles) at $y=4$ with the following PlotStyle (something like the attached picture. There I have added those black lines manually, so they may not be in the correct position)

PlotStyle -> Directive[Black, CapForm["Butt"], Opacity[1], Thickness[.001]]

I wonder if someone could please show code to do that.

plot with manually added root indicators

$\endgroup$

5 Answers 5

3
$\begingroup$

Define a function that draws short lines at the given positions p.

marker[p_] := Line[{p - {0, .1}, p + {0, .1}}]; 

f[x_] := Cos[x] + 2 Cos[2 x];
points = {#[[1, 2]], 4} & /@ NSolve[f[x] == 0 && 0 < x < 20, x];

Plot[f[x], {x, 0, 20}, 
 PlotStyle -> 
  Directive[Black, CapForm["Butt"], Opacity[1], Thickness[.001]], 
 Prolog -> marker /@ points, PlotRange -> {Automatic, {-2.5, 4.5}}]

enter image description here

$\endgroup$
3
$\begingroup$

Use NSolve to calculate f=0 points

f[x_] := Cos[x] + 2 Cos[2 x];

points = {#[[1, 2]], 4} & /@ NSolve[f[x] == 0 && 0 < x < 20, x];
(*Out: {{0.935929, 4}, {2.57376, 4}, ...} *)

Use Prolog to draw points on plot + increase the PlotRange to make sure you can see the points:

Plot[f[x], {x, 0, 20}, 
 PlotStyle -> Directive[Black, CapForm["Butt"], Opacity[1], Thickness[.001]],

 Prolog -> Point[points],
 PlotRange -> {Automatic, {-2.5, 4.5}}
 ]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks. But as I mentioned in the question, I need those points as thin lines, not circles. $\endgroup$
    – user76607
    Feb 4, 2021 at 15:35
  • 2
    $\begingroup$ @sara96 You can use any type of graphics you like in Prolog, they don’t have to be points. You could use Line or Rectangle to reproduce the shape you want. $\endgroup$
    – MassDefect
    Feb 4, 2021 at 18:20
2
$\begingroup$

My own preference is to use the MeshFunctions option of Plot[] to get the positions of the stripes (which avoids a separate use of Solve[]/NSolve[]), and then post-process accordingly:

Show[Normal[Plot[Cos[x] + 2 Cos[2 x], {x, 0, 20}, Mesh -> {{0}}, 
                 MeshFunctions -> {#2 &}, 
                 MeshStyle -> Directive[Black, CapForm["Butt"],
                                        Opacity[1], Thickness[0.001]]]] /. 
     Point[{x_, y_}] :> Line[{Scaled[{0., -0.01}, {x, 4.}], Scaled[{0., 0.01}, {x, 4.}]}], 
     PlotRange -> All]

zero-dashed plot

$\endgroup$
1
$\begingroup$
f[x_] := Cos[x] + 2 Cos[2 x]

Yet another purely graphical method to identity and mark the zeros (without the need to use Solve/NSolve or post-processing) is to use ContourPlot:

Plot[f[x], {x, 0, 20}, 
 Epilog -> ContourPlot[f[x] == 0, {x, 0, 20}, {y, 3.75, 4.25}, 
    ContourStyle -> Black][[1]], 
 PlotRange -> {-2.5, 4.5}]

enter image description here

We get the same picture using:

Show[Plot[f[x], {x, 0, 20}],
 ContourPlot[f[x] == 0, {x, 0, 20}, {y, 3.75, 4.25}, ContourStyle -> Black], 
 PlotRange -> All]
$\endgroup$
0
$\begingroup$
f[x_] := Cos[x] + 2 Cos[2 x]

You can use NumberLinePlot to identify and mark the zeros:

nlp = NumberLinePlot[f[x] == 0, {x, 0, 20}, Spacings -> 4, ImageSize -> Large,
   PlotStyle -> Directive[Black, CapForm["Butt"], Opacity[1], Thickness[.001]]] /. 
  Point[x_] :> Line[Offset[{0, #}, x] & /@ {-5, 5}]

enter image description here

You can use nlp with Show

Show[Plot[f[x], {x, 0, 20}], nlp, PlotRange -> All, ImageSize -> Large]

enter image description here

or use nlp[[1]] as Epilog or Prolog in Plot:

Plot[f[x], {x, 0, 20}, Epilog -> nlp[[1]], PlotRange -> {-2.5, 4.5}, ImageSize -> Large]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.