ABSTRACT

We are now familiar with some of the most essential

elements of MATLAB® and Octave, namely, vectors and

matrices. We have learned how to manipulate them and

carry out important operations with them such as addition,

substraction, multiplication and, within the definition in the

software, even division. Similarly, we know how to address

and extract individual elements as well as sequences of

In this chapter we turn our attention to one of the distin-

guishing features of MATLAB and Octave, i.e. the plotting

and visualisation capabilities integrated with the devel-

opment environment itself. Whereas other programming

environments do not include a way to produce plots and

graphs, MATLAB and Octave enable us to merge data visu-

a

and Octave. In this chapter we are going to see how to

y(x) = cos(4x), (4.1)

and imagine that we are interested in creating a graph of

this function in the interval 0 ≤ x ≤ pi. This can be easily done by

A common and simple approach is to take equally spaced

points along the x values. We can thus have n + 1 points a distance h apart from each other; for example, in the case of n = 20 we can write:

This will therefore create a set of points stored in the vari-

able x. We could also use the command linspace, whose

which generates a vector with n points at an equal distance

and the case

It is now possible to evaluate y at the various points given by the values stored in the vector x:

Finally the plot of the points calculated above can be ob-

The result of the commands above is shown in Figure 4.1,

where it is clear that the number of points used is small

Note that we need to specify N − 1 so that the colon notation generates a vector with N elements. All we need to do now is repeat the calculations and recreate the plot with the

As we can see, the curve is now much smoother and closer

to what we imagine as a plot for the cosine function. The

number of points can further be increased, but care must

be taken when dealing with larger and more complicated

problems, as an increased number of points may result in a

Although we have now been able to create a plot for the

desired function, we know that appropriate labels and

deal with

The plots shown in Figures 4.1 and 4.2 do show the

main characteristics of the function depicted, but they can

be made more useful by adding further information such as

a helpful and explanatory title, labels for the axes used and

perhaps even a legend for the functions plotted. Further-

more, we can increase or decrease the fonts, manipulate the

this can be

4.2.1 Titles and Labels

It is very useful to add a title that describes the plot as

well as information about what it is that is being plotted

along each of the axes. In order to add a title and label

the axes in the plot, we use the commands title, xlabel

and ylabel. Try out the following commands for the plot

generated in the previous section; the result can be seen in

The strings enclosed in single quotes can be (almost) any-

thing we choose. Some simple LATEX commands are avail-

able for formatting mathematical expressions and Greek

characters. More information about this can be seen in

4.2.2 Grids

Sometimes it is useful to show a grid that helps guide

the eye when looking at a plot. For example, a dotted grid

removed

with a blue solid line. A more general command to create a

From the discussion in the previous sections we know the

meaning of the first two arguments passed to the plot com-

is a

tation marks): the first character of the string specifies the

colour of the line to be plotted, and the second corresponds

to the type of line style. In the example above, b stands for

blue, and - represents a solid line. The options for colours

and styles are shown in Table 4.1. Please note that we can

In some cases it may be desirable to present several

plots in the same figure, provided that there is enough

space to show the plots. We can easily achieve this with the

Notice that the single plot command is taking two plot

specifications, i.e. six arguments in total, three per plot. In

the example above we are asking MATLAB and Octave

to plot the y = x function in a blue solid line and the y = sin(4x) in a black dashed line.