0
$\begingroup$

How to plot this function in Mathematica?

Dirichlet function

$\endgroup$
  • $\begingroup$ Look at Rationalize $\endgroup$ – Sumit Dec 19 '17 at 14:41
  • 1
    $\begingroup$ I think this is impossible. The Dirichlet function is discontinuous at each point of $\mathbb R$. $\endgroup$ – user64494 Dec 19 '17 at 18:14
  • 2
    $\begingroup$ @MariuszIwaniuk Or, simpler, Plot[{0, 1}, {x, -1, 2}, PlotStyle -> Black, PlotRange -> {Automatic, {-1/2, 3/2}}]. Anyway, you can't really plot Dirichlet function... $\endgroup$ – anderstood Jan 18 '18 at 16:15
  • $\begingroup$ A more interesting question would be to plot Thomae's function. $\endgroup$ – Rahul Jan 19 '18 at 9:05
  • $\begingroup$ @Rahul . You can download MMA notebook in Thomae's function click here: mathworld.wolfram.com/notebooks/NumberTheory/… $\endgroup$ – Mariusz Iwaniuk Jan 19 '18 at 16:47
1
$\begingroup$
dirichlet[x_] := If[IntegerQ[Numerator[Rationalize[x]]], 1, 0]

dirichlet[1.24898]
dirichlet[Pi]

1

0

$\endgroup$
  • $\begingroup$ How about dirichlet[N[Sqrt[Pi/Exp[1]], 10]]? $\endgroup$ – user64494 Dec 19 '17 at 18:10
  • $\begingroup$ @user64494 gives me 0 in MMA11.1 - is that wrong? $\endgroup$ – Sumit Dec 20 '17 at 7:16
  • $\begingroup$ Where is the plot? :) $\endgroup$ – Kuba Jan 19 '18 at 8:14
  • $\begingroup$ @Sumit: Up to the definition, it should result 1. $\endgroup$ – user64494 Jan 19 '18 at 8:19
  • $\begingroup$ @Kuba, I don't have a rational answer for that. $\endgroup$ – Sumit Jan 19 '18 at 9:00
0
$\begingroup$

Of course you can't really plot this function, since it is discontinuous everywhere. But you can fake it:

d[x_] := Piecewise[{{1, Abs[Rationalize[x, 0.01] - x] > 0.004}, {0, True}}]; 
DiscretePlot[d[x], {x, 0, 1, 0.001}]

enter image description here

By playing with the second argument of the Rationalize and the value in the inequality, you can change the detailed appearnence of the function.

$\endgroup$
0
$\begingroup$

Faked by ListLinePlot and improved code borrowed from bill s

d[x_] := Piecewise[{{1, Abs[Rationalize[x, 0.0003] - x] > 0.0002}, {0,True}}];
ListLinePlot[Table[{x, d[x]}, {x, -1/2, 1, 0.0001}], 
MeshStyle -> PointSize[Small], Mesh -> {Range[-1/2, 1, 0.0001]}, 
MeshFunctions -> {#1 &}, MeshShading -> {White, White}, 
PlotRange -> {Automatic, {-1/2, 3/2}}, 
PlotLegends -> {"Dirichlet function"}]

enter image description here

$\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.