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:
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.
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.
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.
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.
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.