What is a linspace in MatLab?
A linspace is a special array or linear vector in MatLab that is used to generate evenly spaced numbers in a specific range. It is also a type of colon operator. It is not a colon operator but more than a colon operator because it provides direct control of the specified range of numbers.
Suppose, you want to generate 1 to 10 numbers in MatLab. You can do this by linspace easily. 1 is the start number and 10 is the end number. Put that two numbers in the linspace function in MatLab and tell the function you want 10 numbers in the third parameter.
% y = linspace(start_number, end_number, how_many_numbers_you_want)
y = linspace(1,10,10)
Generated numbers are,
1 2 3 4 5 6 7 8 9 10
Or, you want to create five evenly spaced numbers from -5 to 5. This also can be done easily in MatLab using the linspace function.
% y = linspace(start_number, end_number, evenly_spaced_numbers)
y = linspace(-5,5,5)
Five evenly spaced numbers are,
-5.0 -2.5 0 2.5 5.0
How to use linspace in MatLab?
Linspace is a built-in function in the MatLab programming language. We have to know about the syntax of the linspace function to use it properly. This function takes two or three parameters as the user requirement in MatLab. The uses of this function have already been discussed.
% two parameter linspace()
y = linspace(start_num,end_num)
% three parameter linspace()
y = linspace(start_num,end_num,number_of_elements_you_want)
The number of elements in two parameters linspace function is set at 100. That means if you do not define the third parameter of the linspace function in MatLab, it will generate 100 elements in the defined interval by default. The two-parameter linspace functionality is shown below.
linspace in MatLab generates 100 elements in a two-parameter function |
Make your custom linspace() function in MatLab
You are thinking that how is the linspace function works in MatLab? The calculation of linspace should be known for further purposes. Linspace function maintains a particular formula to give the generated array as output. As we mentioned before, the linspace() function is a type of colon operator and the basic foundation of linspace() is built by the colon operator.
step_size = (end_num - start_num )/(num_elements-1)
linspace_function = start: step_size :end_num
Create five evenly spaced numbers from -5 to 5 using the custom linspace function in MatLab
% parameter description
start_num = -5;
end_num = 5;
num_elements = 5;
% find the step size
step_size = (end_num - start_num )/(num_elements-1);
% step size in the range of start number to end number
linspace_function = start_num: step_size :end_num
Express the sine, cosine, and tangent function using linspace() in MatLab
sine
% generating x vector array using linspace
x = linspace(1,10,100);
% assigning x array in y
y = sin(x)
% plot the x and y values in the 'plot()'
plot(x, y)
output figure of sine
sine curve using linspace() in MatLab |
cosine
x = linspace(1,10,100);
y = cos(x)
plot(x, y)
Output figure of cosine
cosine curve using linspace() in MatLab |
tangent
x = linspace(1,10,100);
y = tan(x)
plot(x, y)
Output figure of tangent
Tangent curve using linspace() in MatLab |
Some applications of linspace function with others function in MatLab
Plot a circle in MatLab using the circle's parametric equation. Plot a vertical line in MatLab using the 'line()' or 'plot()' function.
Read also other important functions in MatLab
sym, syms, and symsum in MatLab