5
$\begingroup$

I want to create an array of numbers, but i want them to be more dense according to a sine function (more density of points near $0$, $\pi$, $2\pi$ and so on. I would like something like a logarithmically spaced array, but with more density of points not just near $0$ but also near those points $x$ where $sin(x)=0$. Is there any way in which I can do that in Mathematica?

$\endgroup$
2
  • 4
    $\begingroup$ There is a built-in distribution named ArcSinDistribution. But you define it for a specific range (e.g. you could define it on (0,Pi). If you want something cyclic, you'd have to splice together several contiguous ranges and apply the distribution to each. $\endgroup$
    – lericr
    Apr 18, 2022 at 23:15
  • 1
    $\begingroup$ Or if you don't want a probability distribution, but just a fixed set of points, you could create an array of regularly spaced values over some range and do the appropriate scaling and application of Sin or Cos or whatever exact function you want. $\endgroup$
    – lericr
    Apr 18, 2022 at 23:24

2 Answers 2

10
$\begingroup$

Suppose we create an evenly distributed list of numbers.

a = Range[-10, 10, .001];

Now let's move each number a bit toward the nearest multiple of pi. The closer they are to a multiple of pi already, the less we should move them.

a2 = # - Sin[2 #]/2 & /@ a;

We can see that there are now more numbers near the multiples of pi.

Histogram[a2, {\[Pi]/100}, Ticks -> {Range[-3 \[Pi], 3 \[Pi], \[Pi]], Automatic}]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ That's exactly what I was looking for, thank you very much! $\endgroup$
    – TopoLynch
    Apr 19, 2022 at 17:56
5
$\begingroup$

This method constructs a set points with a periodic logarithmic spacing that is symmetric about the midpoint of each period. The method is implemented in the following function

ClearAll[symPeriodicLogSpacing]
symPeriodicLogSpacing[nIntervals_, period_, nPeriods_, scale_] :=
  Block[{pts},
    pts = Exp[scale*Subdivide[nIntervals]] (* 1 to E^scale *);
    pts = (pts - 1)/(Exp[scale] - 1) (* 0 to 1 *);
    pts = Flatten[{-Reverse[Rest@pts], pts}] (* -1 to 1 *);
    pts = period/2*pts (* -period/2 to period/2 *);
    pts = Flatten[{First[pts],
      Table[Rest@pts + (k - 1)*period, {k, nPeriods}]}];
    pts[[nIntervals + 1 ;; -nIntervals - 1]]
  ]

The function parameters are

nIntervals -- the number of intervals in a 1/2 period, a positive integer.
period -- the spacing is repeated over this period, a positive real number.
nPeriods -- the points are generated for this number of periods, a positive integer.
scale -- a factor that determines how fast the spacing changes. A non-zero real number. Greater positive values create shorter intervals at the beginning and end of each period. Negative values create shorter intervals in the middle of the period.

The total number of generated points is (2 * nIntervals * nPeriods + 1).

Example Usage

A simple example that generates 7 points over 1 period. The number of intervals (spacings) in each 1/2-period is 3. The period is $\pi$. The spacing factor is 1, a standard value.

symPeriodicLogSpacing[3, π, 1, 1] // N
NumberLinePlot[%]

(*  {0., 0.361656, 0.866387, 1.5708, 2.27521, 2.77994, 3.14159}  *)

enter image description here

A second example generates 7 intervals in each 1/2-period over a range of 4 periods. The period is $\pi$. A scale factor of 3 creates a wider spacing in the middle of each period.

symPeriodicLogSpacing[7, \[Pi], 4, 3] // N;
NumberLinePlot[%]

enter image description here

Density of Points

The effect of the scale factor parameter on the density of the points can be visualized with a histogram like this one.

scaleFactors = {-3, -2, -1, -1/2, -1/3, 1/3, 1/2, 1, 2, 3};
Manipulate[Histogram[Mod[symPeriodicLogSpacing[350, π, 10, σ], π], {π/17}],
  {{σ, 1}, scaleFactors}]

The scale factor can be changed by choosing an alternative value of $\sigma$ in the following dialog. enter image description here

$\endgroup$
1
  • $\begingroup$ This is of great help, I really appreciate the effort. $\endgroup$
    – TopoLynch
    Apr 19, 2022 at 21:09

Your Answer

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

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