# How to Use Matlab Function Plot? A Guide With Examples

Matlab function plot plays an important role in executing the Matlab file name, and the name of the functions must be similar. There are various kinds of function plot in Matlab, that can be used for various purposes. Developers might get puzzled because of the availability of these function plots, but this blog can help you to understand different function plot with its syntax and example so that one can use them as per their preferences. Before proceeding to the Matlab function plot, get some details of steps for plotting the graph of the functions.

### Steps to write the graph of the function

There are certain steps that you need to follow for Matlab function plot, and these are:

• Define the variable x, by highlighting the range of the values for x variable, for which the functions can be plotted.
• Define the function, y = f(x).
• Call the command for function plot with the file name plot(x,y).

There is an example that can be used to demonstrate the basic concept of these functions. Let’s plot the graph function of the function y = x that has the range of values from 0 to 100 for variable x that is incremented with 5.

Generate a script file and write the following programming:

x = [0:5:100];

y = x;

plot(x, y)

When a programmer executes this program, MATLAB will display the plot as given above −

We will show you one more example of the Matlab function plot y = x2. This example can draw two graphs of similar functions, but for the second time, it will decrease the increment’s value. Please take note that as the value of increment decreases, the plotted graph will become smoother.

Generate a script file and write the following program −

x = [1 2 3 4 5 6 7 8 9 10];

x = [-100:20:100];

y = x.^2;

plot(x, y)

When a programmer executes this program, MATLAB will display the plot as given below−

Modify the program file a little bit, decrease the increment to 5 −

x = [-100:5:100];

y = x.^2;

plot(x, y)

MATLAB will draw a smoother plot−

Drawing several functions on a single graph

There is a feature in the Matlab function plot by which you can draw the multiple graphs on a single graph. The given example can show the concept –

### Example

Write a script file and write the following program−

x = [0 : 0.01: 10];

y = sin(x);

g = cos(x);

plot(x, y, x, g, ‘.-‘), legend(‘Sin(x)’, ‘Cos(x)’)

MATLAB creates the following plot−

### Adding Labels, Grid Lines, Title, and Scaling of Matlab function plot

Matlab programming allows you to add labels, titles along with the graph of the x-axis and y-axis. You can adjust the axes and gridlines to spruce up the graphs.

• For the x-axis and y-axis, the command “xlabel” and “ylabel” can be used.
• To put a title on a graph, the “title” command can be used.
• To allow the gridline to be plotted on the graph, the “grid” command can be used.
• A square plot can plot by using the “axis square” command.
• For creating the plot with space and the same scales parameters, the “axis equal” command use.

Example

Generate a script file and write the following program −

x = [0:0.01:10];

y = sin(x);

plot(x, y), xlabel(‘x’), ylabel(‘Sin(x)’), title(‘Sin(x) Graph’),

grid on, axis equal

MATLAB draws the following plot−

### Defining axis scales

To use the axis scales, the Matlab function plot will allow it to use the “axis” command. One can give the minimum and maximum values using the axis command for x and y-axis in the following method:

axis ( [xmin xmax ymin ymax] )

Let’s take an example to show this −

### Example

Generate a script file and write the following program −

x = [0 : 0.01: 10];

y = exp(-x).* sin(2*x + 3);

plot(x, y), axis([0 10 1 1])

When a programmer execute the file, MATLAB will create the following plot −

### Defining the colors on a single graph

Matlab has eight different colors that use for plotting the graphs. The table will provide you the color along with their codes:

### Example

Let take an example of two polynomials that can be drawn on a single graph:

• f(x) = 3x4 + 2x3+ 7x2 + 2x + 9 and
• g(x) = 5x3 + 9x + 2

Generate a script file and write the following program−

x = [-10 : 0.01: 10];

y = 3*x.^4 + 2 * x.^3 + 7 * x.^2 + 2 * x + 9;

g = 5 * x.^3 + 9 * x + 2;

plot(x, y, ‘r’, x, g, ‘g’)

When a programmer executes the program, MATLAB creates the following plot −

#### Creating sub-plots

When a programmer generates an array of plots in a single figure, each of them is known as the subplot. The “subplot command” can use for drawing subplots on the graphs.

The syntax command for plotting the sub-plot is −

#### subplot(m, n, p)

where, m and n are the numeric value of rows and columns to define the plot array and p implies the place where to put a specific plot.

The plot that are created with the Matlab function plot “subplot command” would have their own characteristics. The example demonstrates below has the concept of subplot function−

### Example

Let take two plot examples to generate two different plots −

y = e−1.5xsin(10x)

y = e−2xsin(10x)

Generate a script file and write the following program −

x = [0:0.01:5];

y = exp(-1.5*x).*sin(10*x);

subplot(1,2,1)

plot(x,y), xlabel(‘x’),ylabel(‘exp(–1.5x)*sin(10x)’),axis([0 5 1 1])

y = exp(-2*x).*sin(10*x);

subplot(1,2,2)

plot(x,y),xlabel(‘x’),ylabel(‘exp(–2x)*sin(10x)’),axis([0 5 1 1])

When a programmer executes the file, MATLAB will create the following plot −

### Conclusion

This blog has provided all the details on the advanced topic of the Matlab assignment that is a Matlab function plot. This post has described different function plot with its syntax as well as the example for it. This includes steps to write the graph of the function, drawing several functions on a single graph, defining axis scales, and much more. All this information can use to understanding the advanced use of Matlab programming, and then a programmer can use it for their Matlab projects.

If you are facing any difficulty related to the Matlab assignment, then you can take our experts’ help for Matlab Assignment Help, which is available 24*7. Our experts can provide you high-quality content that drafts with well-syntax and well-formated data. Our services are accessible at reasonable prices, so avail of our services and relaxed from these hectic Matlab assignments.