HomeLinuxHow one can Plot a Vector in MATLAB

How one can Plot a Vector in MATLAB


MATLAB is a instrument for performing numerical computations and writing code, created by MathWorks. It’s utilized in engineering and arithmetic to investigate knowledge, create fashions, and run simulations.

Vectors are an array of a quantity that shops completely different knowledge. MATLAB permits us to plot these vectors utilizing completely different strategies. This text covers alternative ways of plotting a MATLAB vector.

What’s a Vector

A vector is a single-dimension array of numbers organized in a single line or row. The sq. brackets [ ] can outline a brand new vector in MATLAB. For instance, to create a row vector with the weather 1, 2, and three, we will use the next command:

 

To create a column vector with the identical components, we will use a semicolon (;) to separate the weather:

 

How one can Create a Vector in MATLAB

Along with utilizing sq. brackets to create vectors manually, a number of built-in capabilities in MATLAB can be utilized to create vectors. Some widespread capabilities embrace:

    • linspace: creates a linearly spaced vector with a specified variety of components between two endpoints.
    • logspace: creates a logarithmically spaced vector with a specified variety of components between two endpoints.
    • colon: creates a usually spaced vector with a specified increment between two endpoints.

For instance, to create a vector with 10 components between 0 and 1, we will use the linspace operate:

 

Plotting a Vector in MATLAB

 

Fundamental Plotting

To plot a vector the plot operate is used. Syntax for plotting vector utilizing plot() is:

 
Right here each x and y signify two vectors of equal size. The plot operate will create a two-dimensional line plot of the info in y versus the info in x.

For instance, now we are going to plot sine operate between 0 and 2π:

x = linspace(0,2*pi);
y = sin(x);
plot(x,y)

 
It will create a plot of the sine operate with x on the x-axis and y on the y-axis.

Customizing the Plot

MATLAB supplies many choices for customizing plots. Some widespread choices embrace altering the road fashion, coloration, and marker kind. These choices could be specified utilizing name-value pairs when calling the plot operate.

For instance, now we are going to create a purple dashed line plot with circle markers:

x = linspace(0,2*pi);
y = sin(x);
plot(x,y,‘r–o’)

 
The [r–o] argument specifies that the road needs to be purple (r), dashed (–), and have circle markers (o).

Including Titles and Labels

So as to add titles and labels to plots in MATLAB, we will use the title, xlabel, and ylabel capabilities. These capabilities add textual content to the plot to supply particulars in regards to the knowledge being displayed.

For instance, now we are going to add a title and axis labels to our sine plot:

x = linspace(0,2*pi);
y = sin(x);
plot(x,y,‘r–o’)
title(‘Sine Operate’)
xlabel(‘x’)
ylabel(‘sin(x)’)

 
It will add a title above the plot and labels on the x- and y-axes.

A number of Plots on One Graph

MATLAB shows a number of plots on one graph utilizing the maintain on command. This command tells MATLAB to maintain the present plot when including new plots.

For instance, now we are going to add a cosine plot to our sine plot utilizing the maintain on command:

maintain on
y2 = cos(x);
plot(x,y2)

 
It will add a cosine plot to our present sine plot.

Saving and Exporting Plots

MATLAB supplies a number of choices for saving and exporting plots. To avoid wasting a plot as a picture file (equivalent to PNG or JPEG), we will use the saveas operate.

The fundamental syntax for this operate is:

 
the place gcf refers back to the present plot and filename represents the identify we wish to save this file with.

For instance, to avoid wasting sine plot as a PNG picture run the next command:

saveas(gcf,‘sine_plot.png’)

 

Conclusion

Vectors are an array of a quantity that shops completely different knowledge. MATLAB can plot these vectors utilizing completely different strategies. Right here we lined the plot() operate for plotting the MATLAB vectors. Utilizing this operate, we will even customise the MATLAB plot of vectors by including x and y axis labels. Additional, we lined how we will plot a number of vectors on a single plot utilizing the maintain command.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments