In MATLAB, many numerical tasks begin with creating a clean, predictable set of values. Whether you are plotting a mathematical function, preparing simulation time steps, or sampling a range of parameters, the linspace function is one of the most reliable tools for generating evenly spaced numbers.
TLDR: linspace creates a row vector of evenly spaced values between a starting point and an ending point. For example, linspace(0,10,6) returns [0 2 4 6 8 10]. In a typical engineering plotting task, using 1,000 points instead of MATLAB’s default 100 can make a curve appear smoother and reduce visual sampling artifacts by a noticeable margin. A user modeling temperature over a 24-hour period might use linspace(0,24,145) to create measurements every 10 minutes.
What Is the MATLAB linspace Function?
The MATLAB linspace function generates linearly spaced values between two endpoints. The name comes from “linear space,” meaning that the distance between each consecutive value is constant.
The basic syntax is:
y = linspace(x1, x2)
This creates 100 evenly spaced points between x1 and x2, including both endpoints.
You can also specify the exact number of points:
y = linspace(x1, x2, n)
Here, n is the number of values you want MATLAB to return. This makes linspace especially useful when you need precise control over the size of a vector.
For example:
x = linspace(1, 5, 5)
The result is:
x =
1 2 3 4 5
Each value is separated by 1, and both 1 and 5 are included.
Why Use linspace Instead of the Colon Operator?
MATLAB users often compare linspace with the colon operator. For example, this colon expression:
x = 0:2:10
produces:
0 2 4 6 8 10
The equivalent linspace expression is:
x = linspace(0, 10, 6)
Both results are the same in this case. However, the key difference is in how you define the sequence:
- Colon operator: You specify the step size.
linspace: You specify the number of points.
This distinction matters. If you know you need exactly 500 data points for a graph, linspace is usually the clearer and safer choice. If you know the exact increment, such as every 0.25 units, the colon operator may be more natural.
Basic Examples of linspace
Consider a simple range from 0 to 1:
x = linspace(0, 1, 6)
This returns:
0 0.2 0.4 0.6 0.8 1.0
The function divides the interval into equal parts so that the requested number of values is produced. Since there are 6 points, there are 5 intervals between them.
You can also create decreasing sequences:
x = linspace(10, 0, 5)
The result is:
10 7.5 5 2.5 0
This is useful when modeling decay, reverse indexing, or any case where values move from a larger number to a smaller one.
Using linspace for Plotting
One of the most common uses of linspace is preparing x-values for a plot. Suppose you want to plot the sine function from 0 to 2*pi:
x = linspace(0, 2*pi, 100);
y = sin(x);
plot(x, y)
This creates 100 evenly spaced x-values and calculates the sine value at each point. The result is a smooth sine curve.
If the number of points is too small, the plot may look rough or angular:
x = linspace(0, 2*pi, 10);
y = sin(x);
plot(x, y)
With only 10 points, MATLAB connects fewer samples, so the shape is less accurate visually. In technical reports, academic work, or engineering analysis, using enough points is important because a poorly sampled plot can misrepresent the underlying function.
Common Use Cases
The linspace function appears frequently in numerical computing because it is simple, predictable, and flexible. Common uses include:
- Plotting functions: Creating x-values for graphs of equations, signals, and statistical models.
- Simulation time vectors: Defining time points from a start time to an end time.
- Parameter sweeps: Testing a model across a controlled range of input values.
- Signal processing: Creating frequency or time axes for sampled signals.
- Numerical experiments: Generating predictable input arrays for testing algorithms.
For example, an analyst studying how air resistance affects projectile distance might test 50 drag coefficient values between 0.1 and 1.0:
c = linspace(0.1, 1.0, 50);
This gives a controlled set of values for running repeated simulations. Instead of manually typing numbers or relying on imprecise increments, the analyst can generate a consistent input vector in one line.
Understanding the Number of Points
A common mistake is confusing the number of points with the number of intervals. In linspace(a, b, n), MATLAB returns n points, not n intervals.
For example:
x = linspace(0, 10, 6)
The output contains 6 values:
0 2 4 6 8 10
But there are only 5 intervals between them. The spacing is calculated as:
spacing = (x2 - x1) / (n - 1)
So in this case:
spacing = (10 - 0) / (6 - 1) = 2
This formula explains why both endpoints are included and why the distance between points depends on n - 1, not n.
Using linspace with Non-Integer Values
linspace is not limited to whole numbers. It is often more valuable when working with decimals, scientific constants, or ranges that do not divide cleanly.
x = linspace(0, 1, 7)
This produces values approximately equal to:
0 0.1667 0.3333 0.5000 0.6667 0.8333 1.0000
Because computers use floating-point arithmetic, some decimal values may be displayed with small rounding differences. This is normal and should not be mistaken for an error in linspace.
Working with Complex Numbers
MATLAB’s linspace can also work with complex numbers. For example:
z = linspace(1+1i, 5+5i, 5)
This creates evenly spaced complex values between 1+1i and 5+5i. MATLAB interpolates both the real and imaginary parts linearly. This can be useful in signal processing, control systems, and complex-plane visualization.
Best Practices
To use linspace effectively, consider these practical guidelines:
- Choose enough points for accuracy: A plot with 20 points may be too coarse, while 1,000 points is often smooth for many continuous functions.
- Avoid unnecessary excess: Very large vectors can slow calculations and use more memory.
- Use clear variable names: Names such as
time,freq, orthetamake code easier to read. - Remember that endpoints are included: This is important when combining multiple ranges or avoiding duplicate boundary values.
- Use semicolons when appropriate: Adding a semicolon prevents MATLAB from printing large vectors to the Command Window.
Practical Example: Modeling Time
Suppose you are modeling a sensor that records data over 60 seconds. You want 301 evenly spaced time points:
t = linspace(0, 60, 301);
This creates a time vector from 0 to 60 seconds. Since there are 301 points, there are 300 intervals, so the time step is:
60 / 300 = 0.2 seconds
You can then compute a simulated signal:
signal = cos(2*pi*0.5*t);
plot(t, signal)
This example shows why linspace is useful in real analytical work: it gives you direct control over the number of samples while keeping the range exact.
Conclusion
The MATLAB linspace function is a fundamental tool for creating evenly spaced numeric vectors. It is especially valuable when you know the desired number of points rather than the exact step size. From plotting smooth curves to preparing simulation inputs, linspace helps make MATLAB code clearer, more reliable, and easier to maintain. Used carefully, it provides a precise foundation for numerical analysis, visualization, and scientific computing.

