Exploring Functions

Introduction

Plotting graphs is indispensable in science and engineering as a tool for communicating information and results. MATLAB provides an extensive set of commands that facilitate the creation of high-quality plots.
In this activity we will explore the basic commands used to create and manipulate two-dimensional plots.

Before starting

Use the MATLAB Live Editor to edit and run this Live Script in your browser or your desktop.
  1. Read each section carefully.
  2. Run the code within a section before starting to read the next.
  3. To run the code from each section, click into the code section and then click on the Run Section button (from the toolstrip) or click on the blue stripe on left side of that section as shown below:
runsect.png
Remark: Run the code of each section from top to bottom, otherwise you may get an error.
- The end of a section is indicated with a thin line, like the next one -

1. How to create a plot from a set of points

The plot command is used to create two-dimensional line plots. To plot a set of points , we need to write plot(x,y) where is a vector containing the x-values of the points and is a vector containing their corresponding y-values.
Vectors are entered in MATLAB by typing the elements within square brackets []. There are two types, row and column vectors. In this activity we just need row vectors which can be written using commas or spaces to separate the elements. For example, to plot the set of points , we need the code:
x = [0,1,2,3,4] % Defines vector x = (0, 1, 2, 3, 4), domain
y = [0,1,4,9,16] % Defines vector y = (1, 1, 4, 9, 16), range
plot(x,y);
Once you run the previous code, you will notice that MATLAB has joined the points with straight lines and has selected a sensible set of axes.
Looking at the graph, you will see that MATLAB has plotted each element of the vector y versus its corresponding element in the vector x. Accordingly, it is important that these two vectors have the same length, otherwise MATLAB returns an error. For example, if we had omitted the last element of "x" in the above example, then MATLAB would have returned the error message:
error-vec-size.png
Try it yourself! Modify the previous code and re-run this section to see happens when you delete 4, the last term from x = [0,1,2,3,4].

1.1 The colon operator ":" to create vectors

The colon operator ":" is one of the most useful operators in MATLAB. It is used to create vectors, subscript arrays, and specify for iterations. In this case, we are interested in creating row vectors.
For example, if you want to create a row vector, containing integers from 1 to 10 with an increment of one, then you write:
1:10
However, if you want to specify an increment value other than one, you can write for instance:
1:2:10 % Increment of 2
1:1/2:10 % Increment of one half
0:pi/4:pi % You can use real numbers as well
100:-5:50 % This one is decreasing 5 units from 100 to 50
Run this section to see and analyse the different outputs.

2. How to plot functions y = f(x)

To plot the graph of a function, you need to take the following steps:
The following examples would demonstrate the concept.

2.1 Example 1 - Plotting one graph

Let us plot the simple function for the range of values for x from 0 to 50, with an increment of 5. Run this section to see the plot:
x = 0:5:50 % Defines the domain as a row vector
y = x; % Defines the range
plot(x, y)
The colon operator ":" is used here to create row vectors of evenly-spaced numbers. The simplest use of the colon operator is to create a row vector of consecutive integers. You can also type
x = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
but here it is more convenient to use the colon symbol. What do you think?

2.2 Example 2 - Plotting two graphs

Let us plot the function . In this example, we will draw two graphs of the same function, with different values of increment. Note that as we decrease the increment, the graph becomes smoother.
x1 = -100:50:100; % Domain with big increment
x2 = -100:25:100; % Domain with small increment
y1 = x1.^2; % Range of x1
y2 = x2.^2; % Range of x2
plot(x1, y1, x2, y2)
Remark: Here we are using row vectors (also known as arrays) which means that the operations multiplication, division and exponentiation must have the period "." before each operator: *, /, ^. Learn more about it here: Arithmetic operations.
Modify the first two lines, in the previous code, so both plots are smooth. Then re-run this section to see the result.
If you prefer to have two plots separately then replace plot(x1, y1, x2, y2) with the following two lines:
plot(x1, y1)
plot(x2, y2)
Try it yourself and re-run this section to see the result.

2.3 Example 3 - Plots using the command linspace()

To create row vectors, we can also use the command
linspace(x1, x2, n)
which generates n points between x1 and x2 (including the end points). The spacing between them is
(x2-x1)/(n-1).
This command is similar to the colon operator ":", but gives direct control over the number of points and always includes the endpoints.
In the following example we will draw three different functions with the same domain using this command:
x = linspace(0, 2*pi, 1000); % 1000 points between 0 and 2pi. What if n = 10?
y1 = sin(x);
y2 = sin(x - 0.25);
y3 = sin(x - 0.5);
plot(x, y1, '-.r', x, y2, '--g', x, y3, '-k')
legend('sin(x)', 'sin(x-0.25)', 'sin(x-0.5)')
For each pair of vectors in a plot command, we can add an additional argument to specify the line style, marker symbol, and colour used to plot the pair. To modify the presentation of a single graph, we use the command
plot(x, y, LineSpec)
where LineSpec is a string (i.e., some text surrounded by apostrophes (')) that specifies the line style, marker symbol and colour of the graph. Some other values you can use are the following:
line-style.png marker-code.png code-color.png
Finally, in order to identify each graph we added the command legend(), which creates a descriptive label for every graph.
Modify the plot command, in the previous code, to change the line style or colour. Also try to add a marker. Re-run your code to see the changes.

2.4 Example 4 - Adding Title, Labels, Grid Lines and Setting Axes

To provide a better presentation of your plots, MATLAB allows you to add title, labels along the x-axis and y-axis, grid lines and also to adjust the axes.
For example, run the following code:
x = linspace(0, 5, 1000);
y = exp(-1.5 * x).* sin(10 * x);
plot(x, y)
title('A model for a damped harmonic oscillator')
xlabel('x')
ylabel('exp(–1.5x)*sin(10x)')
grid on
axis([0 5 -1 1])

3. Hands on Practice

Let's practice what we just learned.

3.1 Inverse functions

Activity 1:

Consider the function
Notice that is not a one-to-one function on . Plot on an closed interval where is one-to-one.
Write your code here:
 
 
 
 

Activity 2:

Plot the functions
and
on the same axes, with the x-axis ranging from -4 to 4 and the y-axis raging from -10 to 10.
Write your code here:
 
 
 
 
Hint: Recall that the operation of division must have the period before the operator, that is "./". Learn more about it here: Arithmetic operations.
What do you notice? What do you wonder? Is g the inverse of f on the interval ? How can you prove it?

3.2 Floor and Ceil functions

In mathematics and computer science, the floor function is the one that returns the greatest integer less than or equal to x. On the other hand, the ceiling function returns the least integer greater than or equal to x.

Activity 3:

Use the built-in functions floor() and ceil() to evaluate the following values:
  1. , , ,
  2. , , ,
Compare the results. What do you notice?
Write your code here:
 
 
 
 

Activity 4:

Plot together the graphs of and on the same axes, with the x-axis ranging from -5 to 5 and the y-axis raging from -1.5 to 1.5. Use the commands from sections 2.3 and 2.4 to identify each graph in the same plot.
Write your code here:
 
 
 
 
Hint: Recall that the operation of exponentiation must have the period before the operator, that is ".^". Learn more about it here: Arithmetic operations.