Matlab

Different Functions of linspace in MATLAB with Examples

The linear-spaced vectors have values with equal differences in the given linear domain. In simple words, if we have a domain [1, 10] and we want to divide it into 5 interval points then we will obtain a resultant vector [1, 3.25, 5.50, 7.75, 10]. This resultant vector is called the linearly spaced vector. In MATLAB, linearly spaced vectors are created by utilizing a linspace() function.

This article is going to teach us how to implement the MATLAB linspace() function.

How to Implement the linspace() function in MATLAB?

MATLAB library includes a built-in linspace() function that allows you to create linearly spaced values between two specific points. This function requires two values as the input and another optional input that is used to specify the number of equally spaced values. This function follows a simple syntax given:

linspace(<start_point>, <end_point>, (optional)<number_of_points>)

This function accepts three arguments:

  • start_point: A mandatory argument used to specify the starting value of the interval.
  • end_point: A required argument used to specify the end value of the interval.
  • number_of_points: An option argument is used to describe a number of values with equal spacing. If this argument is not specified, the function by default creates a vector of 100 equally spaced elements lying between the given points.

Let’s consider some practical examples to illustrate the working of the linspace() function.

Example 1

This is a simple MATLAB code that uses the linspace() function to print equally spaced values starting from 1 and ending at 10.

vect=linspace(1, 10)

In the above code, we did not specify the number of elements, so it created a vector having 100 elements.

Example 2

The given MATLAB code will create a linearly spaced vector using the linspace() function by specifying the points and number of equally spaced values.

vect=linspace(1, 10, 5)

Example 3

When plotting a function, it is important to provide equally spaced values as the domain. Instead of manually entering each value, you can use the linspace() function in MATLAB that allows for the generation of a set of equally spaced values within a specified range. Thus, help you in simplifying the process of creating a domain for plotting functions.

x = linspace(1,10);
y = sin(x);
stem(x,y)

Example 4

In this example, we are going to create a vector of linearly spaced complex numbers using the linspace() function.

vect = linspace(-1-i, 1+i, 5)

Conclusion

The linear-spaced vectors have values that are equally spaced within a given linear domain. The MATLAB linspace() function is designed to generate such linearly spaced values between two specified points. The function takes two values as mandatory inputs and provides the flexibility to specify the number of equally spaced values within that interval using an optional argument. This tutorial demonstrates the usage of the linspace() function through practical examples.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.