5
$\begingroup$

Suppose I have the plot of some function y = f(x):

Plot[f(x),{x, x_min, x_max}]

Is there any option to highlight all coordinates (x,y) when both of them are integer?

$\endgroup$
  • $\begingroup$ Please be more specific about what you mean by "integer coordinates". Integer x? Integer f(x)? Or both have to be integer? $\endgroup$ – Szabolcs Feb 15 at 21:01
5
$\begingroup$
f[x_] := 5 x^2/3

{xmin, xmax} = {0, 10};

pts = Select[
  Table[{x, f[x]}, {x, Ceiling[xmin], Floor[xmax]}],
  IntegerQ[#[[2]]] &]

(* {{0, 0}, {3, 15}, {6, 60}, {9, 135}} *)

Plot[f[x], {x, xmin, xmax}, Epilog -> {Red,
   AbsolutePointSize[4], Point[pts]}]

enter image description here

|improve this answer|||||
$\endgroup$
6
$\begingroup$
ClearAll[f]
f[x_] := .5 x + (x - 1)^2

DiscretePlot + ConditionalExpression + FractionalPart

Show[Plot[f[x], {x, 0, 6}, ImageSize -> Large, 
  Ticks -> {Range[6], Range[0, 30]}, 
  GridLines -> {Range[6], Full}], 
 DiscretePlot[ConditionalExpression[f[x], FractionalPart[f[x]] == 0.], {x, 0, 6}, 
  PlotStyle -> Red, Filling -> False]]

enter image description here

MeshFunctions + FractionalPart

Plot[f[x], {x, 0, 6}, 
 MeshFunctions -> {FractionalPart[f@#] &}, 
 Mesh -> {{0.}},
 MeshStyle -> Directive[Red, PointSize[Large]], 
 Ticks -> {Range[6], Range[0, 30]}, 
 GridLines -> {Range[6], Full}, 
 PlotPoints -> {100, Range[0, 6]}, 
 Method -> {"BoundaryOffset" -> False}, 
 ImageSize -> Large]

enter image description here

We get the same picture using the option settings:

MeshFunctions -> {# &} (* and *) 
Mesh -> {Select[FractionalPart[f@#] == 0. &]@Range[0, 6]}

or

MeshFunctions -> {Boole[FractionalPart[#2] == 0.] Boole[
      FractionalPart[#] == 0.] &}  (* and *)
Mesh -> {{1}}

Note: In the second approach, the option setting PlotPoints -> {100, Range[0, 6]} ensures that sampling includes points with integer horizontal coordinates and the setting Method -> {"BoundaryOffset" -> False} ensures that the end-points are included in mesh calculation.

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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