English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Plotting (Plot)

To plot the graph of the function, the following steps need to be executed:

  • By specifying the value range of the variable x, define x, so that this function will be plotted

  • Define the function, y = f(x)

  • call plot The command, as follows plot(x, y)

The following example will demonstrate this concept. Let's draw a simple function y = x, with x ranging from 0 to100, increment of5.

Create a script file and enter the following code-

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

When running the file, MATLAB displays the following graph-

Let's take another example to draw the function y = x 2. In this example, we will draw two graphs with the same function, but the second time, we will reduce the increment value. Note that as we reduce the increment, the graph becomes smoother.

Create a script file and 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)

When running the file, MATLAB displays the following graph-

稍微更改代码文件,将增量减少到5-

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

MATLAB draws smoother graphs-

Add a title, labels, grid lines, and scaling to the graph

MATLAB allows you to add a title, labels along the x-axis and y-axis, grid lines, and can also adjust the axes to make the graph more beautiful.

  • xlabel and ylabel The command generates labels along the x-axis and y-axis.

  • title The command allows you to place a title on the graph.

  • grid on The command allows you to place grid lines on the graph.

  • axis equal The command allows you to generate a plot with the same scale factor and spacing on both axes.

  • axis square The command generates a square plot.

Example

Create a script file and 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 equal

MATLAB generates the following graph-

Plot multiple functions on the same graph

You can plot multiple graphs on the same graph. The following example demonstrates the concept-

Example

Create a script file 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)')

MATLAB generates the following graph-

Set the color on the graph

MATLAB provides eight basic color options for plotting graphs. The table below shows the colors and their codes-

CodeColor
wWhite
kBlack
bBlue
rRed
cCyan
gGreen
mMagenta
yYellow

Example

Let's plot the graphs of two polynomials

  • f(x) = 3x 4 + 2x 3 + 7x 2 + 2x + 9and

  • g(x) = 5x 3 + 9x + 2

Create a script file 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')

When running the file, MATLAB generates the following graphics-

Set axis proportions

axisThis command allows you to set the axis scales. You can use the axis command to provide the minimum and maximum values of x and y axes as follows:

axis(xmin xmax ymin ymax)

The following example illustrates this-

Example

Create a script file 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])

When running the file, MATLAB generates the following graphics-

Create subplots

When creating a plotting array in the same graph, each plot is called a subplot.subplot This command is used to create subplots.

The syntax of this command is-

subplot(m, n, p)

where,mandnis the number of rows and columns of the plotting array, andpSpecify the position of a specific plot.

Each plot created using the subplot command can have its own characteristics. The following example demonstrates the concept-

Example

Let's generate two plots-

y = e -1.5x sin(10x)

y = e -2x sin(10x)

Create a script file and enter the following code-

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 running the file, MATLAB generates the following graphics-