Function to subdivide interval into n evenly-spaced points

The three argument form of Array is convenient and I've used this a few times in different answers (example). The definition is quite simple:

linearmesh[a_, b_, n_Integer] := Array[# &, n, {a, b}]

and it gives you the same answers as your first example. Note that in your second example (with spacing $(b-a)/n$), you have 301 samples. This definition is used when you want 300 partitions, not 300 points.


In version 10.1 the function Subdivide was introduced which does precisely that.

Subdivide[10]
(* {0, 1/10, 1/5, 3/10, 2/5, 1/2, 3/5, 7/10, 4/5, 9/10, 1} *)

Subdivide[10, 5]
(* {0, 2, 4, 6, 8, 10} *)

Note that the number 5 equals the number of intervals not the amount of entries in the list (which is higher by one).


This function works exactly like MATLAB's linspace as it gives you n points (rather than n+1):

linspace[x0_, x1_, n_] := Range[x0, x1, (x1 - x0)/(n - 1)];