The linspace() Function in MATLAB
This tutorial will discuss generating linearly spaced vectors using the linspace()
function in Matlab.
Generate Linearly Spaced Vectors Using the linspace()
Function in MATLAB
The linspace()
function is used to generate linearly spaced vectors in Matlab. This function is used when we have to use a vector containing linearly spaced numbers. For example, consider, we have a function, and we want to evaluate and plot this function inside a certain range like 1 to 100. Creating a vector of a hundred values is a difficult task. We have to put each value manually. In this case, we can use the linspace()
function to generate the vector in just one line of code. For example, let’s use the linspace()
function to evaluate a sin(x)
at 100 points and plot it using the stem()
function. See the code below.
x = linspace(1,10,100);
y = sin(x);
stem(x,y)
Output:
In the above code, the variable x contains one hundred linearly spaced numbers between 1 to 10. The variable y contains values of the function sin(x)
. The stem()
function is used to plot the data as points on the graph, and as you can see, there are one hundred points on the graph. The linspace()
function excepts three arguments. The first two arguments are the range of the values, and the third argument is the number of points that we want to generate. In the above code, the range is 1 to 10, and the number of points is 100.