How to use linspace in Matlab

The linspace function allows the programmer to produce an array, matrix, or vector of numbers that are linearly spaced. Suppose you want to create an array A of size 8 with values of 1, 2, 3, 4, 5, 6, and 7. This can be done by using following command “A= {0; 1; 2; 3; 4; 5; 6; 7} ” . Then what is the purpose of the Linspace MATLAB function?

It allows you to bypass the old time-consuming method where we had to plug in all the numbers. As in the above example, to create an array of size 8, we had to plug in all the numbers from 0 to 7 and this will give us the output of 8 by 1 array. An easier way to do this is to use Linspace which will give you linearly spaced values starting from a given starting point to the specified ending point. It is similar to the colon operator “:”, but gives direct control over the number of points.

Syntax of Linspace function
Method 1:

Here x and y are the two numbers specified by the user. These numbers can be real or complex. The function will return equally spaced 100 points by default between the numbers x and y. If x is smaller than y, the vector will be of ascending values whereas if x is greater than y, then it will generate values in descending order.

Let’s understand this through an example.

Example 1:

This command will return a row vector M of size 100 with numbers ranging from 0 to 10 in ascending order.

Example 2:

Let’s swap x and y in example 1 to check what happens. Then the above command becomes:

After executing this command, you will observe that the numbers are now printed in descending order from 10 to 0. So, you can also use linspace command to create a vector of numbers in descending order.

Method 2:

The above linspace matlab command returns 100 equally spaced points. Suppose we want an array of size n instead of 100. The following command allows you to create a row vector of size ‘n’ with numbers in an interval [x, y].

Example 3:

Let’s say you want 5 values in an interval of [0,10]. Then, set x=0, y=10, and n=5 and run the following command in your MATLAB.

Output:

You can also obtain a column vector by taking a transpose of M.

Example 4:

Let’s consider another example in which you are asked to create a vector of complex numbers of size 7 where x= 2+6i and y=3+4i.

Output:

Example 5:

In addition to the above mentioned uses, it can be used to create plots. Suppose you want to plot a function y = sin(2*pi*x) for x in the interval [0, 1] using 100 equally spaced points.

Output:

The Linspace Matlab command is almost similar to the colon operator “:” but the main difference is that with the colon operator, you can define the step size between the successive elements, and the length of the vector is calculated by the function based on starting point, ending point and step size. On the other hand, linspace allows you to define the length of the vector and return a vector with a step size equal to the total length of an array divided by a specified length.

Contact Us for queries or tasks related to Matlab.

Leave a Comment

Your email address will not be published.