I am facing the following problem:

Given is a line of length $L$ which I want to split into $N$ segments. The lengths of the first $(s_1)$ and last segment $(s_N)$ are given. You can assume that the first and the last segment are shorter than the average length $s_{ave} = \frac{L}{N}$ but they are not required to be equal. There should be a smooth transition, no sudden jump. The segment length should therefore increase at the beginning and decrease again towards the end of the line.

I am looking for a simple algorithm that could do this for me. I was trying to use (one or multiple) geometric series: $s_i = \alpha s_{i-1}$. If you specify the growth factor $\alpha$, the length (partial sum) is given by $L = s_1 \frac{\alpha^N -1}{\alpha -1}$ and if you specify the first and last segment length, $\alpha$ must satisfy $\alpha = (\frac{s_N}{s_1})^{\frac{1}{N}}$. Thus the system is overspecified if you fix $N, L, s_1, s_N$. Even if you split the problem, say into an increasing section, one where the length is constant and another one wehere the length decreases. Eventually you have to specify everything in one section.

  • $\begingroup$ What is your question specifically? You can use logarithmic sampling and change the base for different spacing. $\endgroup$
    – nicoguaro
    Commented May 22, 2017 at 13:55
  • $\begingroup$ I want the distribution to match the spacing at both ends, where the spacing at the left end is not necessarily the same as at the right end. At the same time the overall length and the number of points is fixed. Logarithmic spacing would be fine if the spacing only had to match one end. $\endgroup$
    – benno
    Commented May 22, 2017 at 16:50
  • $\begingroup$ I just noticed your answer. If you use the handle of the person, e.g. @benno, she will be notified. $\endgroup$
    – nicoguaro
    Commented Jun 2, 2017 at 4:48

1 Answer 1


You can uniformly distribute points and apply transformation, $$ N_s(\zeta)=1-\zeta\\ N_e(\zeta)=\zeta\\ \phi(\zeta)=x_sN_s(\zeta)+x_eN_e(\zeta)+ \sum_i \alpha_i L_i(2\zeta-1) N_s(\zeta)N_e(\zeta) $$ where $L_i$ is Legendre polynomial. So if you set for example $\alpha_0=0$ and $\alpha_1=1$, you will get your distribution, when you apply function $\phi(\zeta)$ to points uniformly distributed between $(0,1)$.

  • $\begingroup$ What are the alphas representing? $\endgroup$
    – benno
    Commented May 22, 2017 at 17:13
  • $\begingroup$ It represents how strongly you like to change distance between points. If is equal to 0, it means that distances are equal. $\endgroup$
    – likask
    Commented May 22, 2017 at 17:14
  • $\begingroup$ Ok. Am I right about the fact that you use the first two Legendre Polynomials, which simply are the constant 1 and x? $\endgroup$
    – benno
    Commented May 22, 2017 at 17:21
  • $\begingroup$ You can only have second one. You can add more if you like as well. This give you some flexibility to change density of the points. Density is derivative of $\phi(ksi)$. $\endgroup$
    – likask
    Commented May 22, 2017 at 17:38
  • $\begingroup$ Now i got it! This is really smart. Is there any reason why you would chose Legendre polynomials instead of any other family of polynomials? $\endgroup$
    – benno
    Commented May 22, 2017 at 19:38

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.