2
$\begingroup$

I have two criteria for the domain of $n\in\mathbb{N}$:

For all even $n$, I want to use the constant function $f(n)=5$

For odd $n$, I want to use the function $g(n)=9 \sin \left(\frac{\pi n^2}{5}\right)$

And then I want to have one plot for all $n\in\mathbb{N}$, and if possible, to join the adjacent numbers.

Any comments are welcome.

$\endgroup$
4
$\begingroup$
f[n_] := (1 - Mod[n, 2]) 9 Sin[Pi n^2/5] + Mod[n, 2] 5

DiscretePlot[f[n], {n, 1, 20}, Joined -> True, Filling -> None, 
 PlotMarkers -> {"Point", .02}]

enter image description here

DiscretePlot[{f[n], f[n]}, {n, 1, 20}, Joined -> {False, True}, 
 Filling -> 1 -> Axis, PlotMarkers -> {"Point", .02}]

enter image description here

DiscretePlot[{f[n], f[n]}, {n, 1, 20}, Joined -> {False, True}, 
 Filling -> 1 -> Axis, PlotMarkers -> {"Point", .02}, 
 FillingStyle -> Directive[Dashed, Opacity[1]], 
 ColorFunctionScaling -> False, 
 ColorFunction -> (If[OddQ[Round[#]], Red, Blue] &)]

enter image description here

| improve this answer | |
$\endgroup$
6
$\begingroup$
f[n_?EvenQ] := 5;
f[n_?OddQ] := 9*Sin[(Pi*n^2)/5];
ListPlot[Table[f[n], {n, 1, 20, 1}], Joined -> True, 
 MeshStyle -> {Red}, Mesh -> {Range[1, 20, 2]}]

To distinguish two types of the points, one way is

f[n_?EvenQ] := 5;
f[n_?OddQ] := 9*Sin[(Pi*n^2)/5];
ListPlot[Table[f[n], {n, 1, 20, 1}], 
 Mesh -> {Range[1, 21, 2], Range[2, 22, 2]}, 
 MeshFunctions -> {#1 &, #1 &}, 
 MeshStyle -> {Directive[PointSize[Medium], Red], Blue}, 
 Joined -> True]

enter image description here

Another way is

evenColor = 
  Table[{n, Directive[Red, PointSize[Large]]}, {n, 1, 21, 2}];
oddColor = 
  Table[{n, Directive[Blue, PointSize[Large]]}, {n, 2, 22, 2}];
meshColor = Join[evenColor, oddColor];
f[n_?EvenQ] := 5;
f[n_?OddQ] := 9*Sin[(Pi*n^2)/5];
ListPlot[Table[f[n], {n, 1, 20, 1}], Joined -> True, 
 Mesh -> {meshColor}]

enter image description here

| improve this answer | |
$\endgroup$
2
$\begingroup$

How about this?

Show[
 DiscretePlot[5, {n, 0, 20, 2}, PlotStyle -> Blue],
 DiscretePlot[9 Sin[Pi n^2/5], {n, 1, 20, 2}, PlotStyle -> Red],
 ListLinePlot[
  Table[
   {n, Piecewise[{{5, EvenQ[n]}, {9 Sin[Pi n^2/5], True}}]}, {n, 0, 20}], 
  PlotStyle -> Directive[Dashed, Gray]],
 PlotRange -> Full
 ]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks. But unfortunately, I get an error. The plot is not shown. $\endgroup$ – charmin Oct 27 at 23:32
  • $\begingroup$ @charmin what is the error? I don't get any error and I'm on 12.1.1.0 on Windows10 $\endgroup$ – flinty Oct 27 at 23:33
  • $\begingroup$ A red box containing "The specified setting for the option GraphicsBoxOptions, PlotRange cannot be used." $\endgroup$ – charmin Oct 27 at 23:35
  • 1
    $\begingroup$ @charmin must be a bug in your version. Try this instead: Show[DiscretePlot[5, {n, 0, 20, 2}, PlotStyle -> Blue, PlotRange -> {-6, 6}], DiscretePlot[9 Sin[Pi n^2/5], {n, 1, 20, 2}, PlotStyle -> Red], ListLinePlot[ Table[{n, Piecewise[{{5, EvenQ[n]}, {9 Sin[Pi n^2/5], True}}]}, {n, 0, 20}], PlotStyle -> Directive[Dashed, Gray]]] $\endgroup$ – flinty Oct 27 at 23:37
  • $\begingroup$ Thank you very much. Now, it works :) $\endgroup$ – charmin Oct 27 at 23:40

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