MATLAB Lesson 5 - Plotting

Plot command

In MATLAB you create a two dimensional plot using the plot command. The most basic form is

plot(x, y)

where x and y are vectors of the same length containing the data to be plotted.

Plot the function y = sin(2 pi x) for x in the interval [0, 1] using 401 equally spaced points.

Create a vector x of 401 equally spaced points on [0, 1]. Don't forget the final semicolon to suppress output!

Create a vector y of function values.

Plot the points, producing the figure below.

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


Plot of sin(2 pi x)

MATLAB's ability to evaluate functions of vectors elementwise, for example sin(2*pi*x), is incredibly useful for plotting functions.

MATLAB actually plots the points (x(i), y(i)) joined by straight line segments. This becomes obvious if you do not use enough sample points when plotting a smooth function.

Plot the function y = sin(2 pi x) for x in the interval [0, 1] using 11 points.

Create a vector x of 11 equally spaced points in [0, 1].

Create a vector y of function values.

Plot the points, producing the figure below.

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


Plot of sin(2 pi x) using only 11 points

 

Title and labels

Every plot should have

The argument to these commands is a string which is just a sequence of characters starting and finishing with a single quote ', for example 'This is my title'. Strings will be covered in more detail in module 7.

Add a title and axis labels to the previous plot.

Add a title. Note the use of \pi to get the mathematical symbol for pi in the title.

Add a label on the x-axis.

Add a label on the y-axis.

>>  title('sin(2\pi x) with 11 points')
>>  xlabel('x')
>>  ylabel('y')

Plot including title and axis labels

 

Line type and symbols

It is easy to change the line type by adding one of the following strings after the two vectors to be plotted: '-' (solid line), '--' (dashed line), ':' (dotted line), or '-.' (dash-dots).

It is also easy to plot a variety of symbols in a variety of colours at each of the data points. This will be very useful when plotting more than one set of data in the next module.

Create a plot of y = t3 - t using 21 equally spaced points on [-2, 2] with a red '*' at each point and points joined by a dashed line.

Create a vector of 21 points on [-2, 2].

Create a vector of polynomial values. Note use of .^ to get elementwise powers.

Plot with red stars joined by dashed lines, giving the figure below.

>>  t = linspace(-2, 2, 21);
>>  y = t.^3 - t;
>>  plot(t, y, 'r*--')

Plot with different symbols and line type

As always, more information can be obtained by typing

help plot

in the MATLAB command window, or using

doc plot

or the Help menu to open the MATLAB help browser.

Grids

Frequently it is useful to have a background grid, particularly when trying to read off values. This is achieved with the command

grid on

to turn the grid on, or

grid off

to turn the grid off.

Add a grid to the previous plot.

Turn the grid on.

>>  grid on
Plot with grid

This plot should still have a title and axis labels added.

 

Warnings

Self-test Exercise

Create a plot of the function f(x) = x e-x2 using 1001 equally spaced points on the interval [-5, 5]. Add a grid, title and axis labels.

Answer:
x = linspace(-5, 5, 1001);
f = x .* exp(-x.^2);
plot(x, f)
grid on
xlabel('x')
ylabel('f')
title('f(x) = x exp(-x^2)')
Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

Two dimensional plots are created using the plot command.

Plots should always have a title (the title command), and axis labels (the xlabel and ylabel) and possibly a grid (use grid on)

A variety of line types, symbols and colours can be used -- see help plot.