# Function does not take many inputs, and cannot plot correctly using Listplot

I am a beginner with Mathematica.

I have a function which is defined as follows:

$$h(x) = \begin{cases} \hfill \frac{1}{x}\sin(x) & \text{if x\neq 0}\\ 1 & \text{if x=0} \end{cases}$$

I want to produce a list plot of this function using n=100 equally spaced points for $x \in [0,2\pi]$.

Here is my code for the fuction:

 h[x0_] :=
Module[{x = x0},
If[x != 2 \[Pi] != 0, x = Sin[x]/x, x = 1];
x
]


and I have made my inputs as: Range[0, 2 \[Pi], 2 \[Pi]/100]

But I cannot evaluate the function at every 100 points separately, it returns me the inputs.

I want to plot this using "Listplot" and display it with a dotted Magenta line.

• have you tried ListPlot[h /@ Range[0, 2 \[Pi], 2 \[Pi]/100]] or ListPlot[Maph]@ Range[0, 2 \[Pi], 2 \[Pi]/100]]?
– kglr
Nov 1 '20 at 3:07
• Note that your h[x] is just Sinc[x] Nov 1 '20 at 3:35
• ListPlot[h /@ Subdivide[0, 2 Pi, 100], PlotStyle -> Magenta] ? Nov 1 '20 at 3:58
• @cvgmt - You should include the option DataRange -> {0, 2 Pi} Nov 1 '20 at 4:46
• Set attribute Listable  to your function h. h[x0_] := Module[{x = x0}, If[x != 2 \[Pi] != 0, x = Sin[x]/x, x = 1]; x]; SetAttributes[h, Listable]; h[Range[0, 2 \[Pi], 2 \[Pi]/100]]  Nov 1 '20 at 13:16

As Bob already pointed out, the function h[x] you're interested in is already available as Sinc. However, if you do want to define it yourself, you could have created a piecewise function like that:

h[x_] := Piecewise[{{1/x*Sin[x], x != 0}}, 1];


Now, you need a list of 100 equidistant points in the interval [0, 2Pi] and there are many ways to achieve this. If you look closely at your Range call, you'll note that you're not creating exactly 100 points but 101. More intuitive is to use

Subdivide[0, 2 Pi, 100]


because you can input the number of points you want to have instead of the stepsize. Having said all this, here are some ways to create a list of data values you want to plot

xvals = Subdivide[0, 2 Pi, 100];
data1 = Map[h, xvals];
data2 = h /@ xvals;
data3 = Sinc /@ xvals;
data4 = Sinc[xvals];


To visualize this with a "dotted magenta _line" you could use ListLinePlot and display the mesh points:

ListLinePlot[data1,
PlotStyle -> Magenta,
Mesh -> All,
MeshStyle -> {PointSize[0.011]}
]