What is a line?
In Geometry, a line is a connection between two continuous points. So, a line is a connective path of two points. There should be a connection between two points and the two points should be continuous to plot a line. In graphs, a point can have two or three axes depending on its dimension.
Suppose, you have two points and they are (x1, y1) = (2, 1) and (x2, y2) = (2, 5). If we continuously connect these two points corresponding to the x-axis and y-axis, then we will get a line. The line plotting has been animated in the below figure.
Plot a line in Graph |
Parametric equation of a line
The parametric equation to plot a circle in MatLab is also discussed on this site. The parametric equation of a line is used to generate continuous points between two initial points.
2D parametric equation
x = x0 + m1t
y = y0 + m2t
3D parametric equation
x = x0 + m1t
y = y0 + m2t
z = z0 + m3t
Slope intercept form of a line
The most non-vertical line generates by the slope-intercept form. In geometry, we are used this line form to draw a line. It is mostly used in the linear regression algorithm.
y = mx + c
This is the slope-intercept equation of a line. Where x is an independent variable, y is a dependent variable, m is the slope, and c is a constant value.
What is a vertical line?
A vertical line is a directed line where the value of x is constant. That means the value of the x-axis is the same while the value of the y-axis will be incremented in a given range.
What is a horizontal line?
A horizontal line is a directed line where the value of y is constant.
What is a straight line?
A straight line is a one-way directed line that is not curved at any point in a line. A straight line can be vertical or horizontal, or diagonal with respect to the x-axis and y-axis.
vertical, horizontal, and diagonal straight line (left to right) |
How to plot a line in MatLab?
Plotting a line in MatLab is simple. MatLab provides a pre-defined function to plot a line.
# to plot a 2D line
line(x, y)
# to plot a 3D line
line(x, y, z)
Code to plot a line in MatLab
% point1 = [x1, y1]
point1 = [1, 2];
% point2 = [x2, y2]
point2 = [5, 10];
% ploting the line using the 'line' function of matlab
line(point1,point2)
Output line
Output figure of the above code |
How to plot a vertical straight line in MatLab?
You can plot a vertical line in MatLab in three ways.
1. Plot a vertical line in MatLab using the 'line()' function
code
% x value is constant
x = linspace(2,2);
% y vlaues are independent to increase
y = linspace(3,10);
% plot the x and y values in the 'line()'
line(x,y)
output figure
2. Plot a vertical line in MatLab using 'plot()' function
code
% x value is constant
x = linspace(2,2);
% y vlaues are independent to increase
y = linspace(3,10);
% plot the x and y values in the 'line()'
plot(x,y)
% turn on the grid mode for a clear view
grid on
output figure
The code can be made easier if we write it like this,% plot([x1, y1], [x2, y2])
plot([1,1], [5,4])
3. Plot a vertical line in MatLab using 'xline()' function
The 'xline()' function of MatLab needs a constant x-axis value to plot a vertical line automatically.
code
xline(2);
output figure
How to plot a horizontal straight line in MatLab?
It is easy to plot a horizontal line in MatLab with a few lines of code like the plot of a vertical line. You need to exchange the x-axis value with the y-axis value. Now, the value of y should be constant to plot a horizontal line in MatLab.
1. Plot a horizontal line in MatLab using the 'line()' function
% x value is independent to increase
x = linspace(2,10);
% y vlaue is constant
y = linspace(3,3);
% plot the x and y values in the 'line()'
line(x,y)
2. Plot a horizontal line in MatLab using 'plot()' function
code% plot([x1, y1], [x2, y2])
plot([1,4], [5,5])
output figure
% plot([x1, y1], [x2, y2])
plot([1,4], [5,5])
3. Plot a horizontal line in MatLab using 'yline()' function
yline(5)