The steps of drawing function graphs in MATLAB are as follows:

  1. The function is plotted by specifying a range of values for the variable x.
  2. And then I define the function, y = f(x);
  3. Finally, the plot command is invoked, such as plot(x, y).

Let’s draw a simple function y = x by example, with x values ranging from 0 to 100 in increments of 5.

To create a script file in MATLAB, enter the following code:

x = [0:5:100]; y = x; plot(x, y)Copy the code

Run this file, MATLAB will display the following graph:

Let’s take an example, draw the function y = x2.

In this example, we will draw two graphs with the same function, but on the second time, we will reduce the increment of the value.

Notice that the graph is smoothed out because the increments are reduced.

To create a script file in MATLAB, enter the following code:

x = [1 2 3 4 5 6 7 8 9 10]; x = [-100:20:100]; y = x.^2; plot(x, y)Copy the code

After running the file, MATLAB displays the following graph:

Change code file to small, reduce increment by 5:

x = [-100:5:100]; y = x.^2; plot(x, y)Copy the code

MATLAB draws a smooth curve:

MATLAB adds titles, labels, gridlines and zoomed graphics

We can add titles in MATLAB, adjust x and y axes, grid lines, and beautify graphics along labels.

  • The xlabel and ylabel directives produce labels along the X and y axes.
  • The title command allows you to generate a title on a chart.
  • The grid command allows you to generate grid lines on a graph.
  • The axis equals command allows the generation of points on both axes with the same scale factor and space.
  • The axis square command generates a square point.

A detailed example

To create a script file in MATLAB, enter the following code:

X = [0-0 01:10]; y = sin(x); plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'), grid on, axis equalCopy the code

The following figure is generated in MATLAB:

MATLAB draws multiple functions on the same graph

In MATLAB you can draw more than one graph the same points. The following example demonstrates this concept:

Concrete example

Create a script file in MATLAB and enter the following code

​​​​​​​

X = [0:0.01:10]; y = sin(x); g = cos(x); plot(x, y, x, g, '.-')legend('Sin(x)','Cos(x)')Copy the code

MATLAB generates the following image:

MATLAB color setting on the chart

MATLAB includes eight basic color options for drawing graphics. The following table provides the colors displayed and the corresponding codes:

Concrete example

Let’s plot two polynomials:

  1. f(x) = 3×4 + 2×3+ 7×2 + 2x + 9 and
  2. g(x) = 5×3 + 9x + 2

Create a script file in MATLAB and enter the following code:

​​​​​​​

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')Copy the code

Run this file and MATLAB will generate the following image:

MATLAB set the axis calibration

The axis command allows you to set the axis scale, you can provide the minimum and maximum values for x and Y axes, using the axis command in the following way:

  •  
axis ( [xmin xmax ymin ymax] )
Copy the code

Concrete example

Create a script file in MATLAB and enter the following code:

​​​​​​​

X = [0:0.01:10]; y = exp(-x).* sin(2*x + 3); plot(x, y), axis([0 10 -1 1])Copy the code

Run this file and MATLAB will generate the following image:

MATLAB generates subgraphs

When creating an array of blocks in the same number, these blocks are called subgraphs.

Use the subplot command to create a subplot in MATLAB.

The syntax of the subplot command is as follows:

  •  
subplot(m, n, p)
Copy the code

Where m and n are the number of rows and columns in the product array, and p specifies the number of rows and columns to place a particular product.

Each plot created by the subplot command can have its own characteristics.

Concrete example

Let’s generate the following two diagrams:

  • Y = e – 1.5 xsin (10 x)
  • Y = e – 2 xsin (10 x)

Create a script file in MATLAB and enter the following code:

​​​​​​​​​​​​​​

X = [0-0 01:5]; Y = exp (1.5 * x). * sin (10 * x); Subplot (1, 2, 1) the plot (x, y), xlabel (' x '), ylabel (' exp (1.5 x) * sin (10 x) '), the axis ([1] 0 5-1) y = exp (2 * x). * sin (10 * x); ,2,2 subplot (1) the plot (x, y), xlabel (' x '), ylabel (' exp (2 x) * sin (10 x) '), the axis ([1] 0 5-1)Copy the code

Run this file and MATLAB will generate the following image: