ENGG1811 Lab 09: Matlab Functions and vectorisation
Update notes:
 Updated on 19/09/16 to include instructions on how to download the
file. Updated text is in magenta.
 Updated on 18/09/16 on "Organising your work" and making sure the data
file in a particular directory. Updated text is shown in red.
 First released on 15/08/16
Objectives
After completing this lab, students should be able to
 Use vectorisation to solve some data analysis problems
 Use vectorisation to do calculations
Assessment
This lab consists of 2 parts: Part A and Part B. Both parts are
assessable, including documentation. The online assessment question is
related to the data set in Part A, so you may want to work on Part A
first.
Organising your work
After starting up matlab, you will be working
in the default folder MATLAB, We recommend that you organise your work
by creating folders under MATLAB. Create a folder called ENGG1811 if you
haven't done it yet. (You probably have done this because you were asked
to do that in Lab08.) Change into ENGG1811 folder and create a folder
called lab09. Change into the directory lab09 and you can store all your
files for this week in this directory.
Part A: Data analysis using vectorisation
Among the many data sets used in climate change research, the Sea
Ice Index, or the area covered by sea ice in the northern hemisphere
shows particularly marked changes. There are normal variations across each
year, and trends over the span of years.
The file NHsea_ice_extents.txt
(You can download this file by right clicking
on the link, and choose "Download Linked File") contains northern
hemisphere total sea ice data, in units of millions of square km, between
1979 and 2013 (35 years in total). There are 24 measurements per year,
measured once every half a month.
The data in NHsea_ice_extents.txt has
35 rows (one row per year) and 25 columns because the first column
contains the year in which the measurements in that row were taken. After
you have downloaded the file NHsea_ice_extents.txt, you need to
make sure that it is in the directory lab09 that you had created
earlier.
In order to work with the data, you need to remove the years from the
matrix. The steps are as follows. The first step of data analysis is always
to plot the data, so you will also do that.
You are asked to run these lines one by one and observe the results. The
comments will also help you to understand what those commands are doing.
%
the data is tabseparated, load assigns it to a variable named
after the file
load NHsea_ice_extents.txt
% rows = number of years, columns =
number of elements in a year = 1 element for the year (in 1st
column) + 24 measurements per year
size(NHsea_ice_extents)
% what's in the first row? Note: First
column is not a measurement but the year in which the measurements
in that row were taken
NHsea_ice_extents(1,:)
% the first column has the year, the rest is area data. We need to
move the years into a different variable
years = NHsea_ice_extents(:,1); % first column, easy
NHsea_ice_extents(:,1) = []; % remove
the first column by assigning nothing
size(NHsea_ice_extents)
% should be 35 (years) x 24 (halfmonthly samples)
% the month range should be 1 to
12 as usual, but we have 24 samples because samples were taken
twice a month
months = linspace(1,12,24);
% plot the data  you should add the
xlabel, ylabel, title
% You will see 35 curves, each curve depicting the variation of
sea ice extents in one year
% The seasonal variation should be obvious and don't forget, the
data came from the northern hemisphere
figure(1) % You
will need to plot more figures later on, so put it in Figure
1https://www.cse.unsw.edu.au/~en1811/16s2/labs/lab09/CTpic.png
plot(months,NHsea_ice_extents) 
The following is a number of questions on the data set which you should
answer using Matlab vectorisation method. You should not
use any loops. Most questions can be answered with just
one line of Matlab code. There is no restriction that you must use
as few lines of code as possible. You can break the steps up into multiple
lines of code if you want. There is only one restriction: no
loops.
You may want to copy your Matlab commands to a script so that you can show
your tutor later on. If you want to check your answers, some of them are on
this page.
 Determine the annual average sea ice extent, i.e. for each year,
compute the average of all the halfmonthly measurements over that year.
Store the answer in the variable avgSeaIceExtentAnnual
which you will need later. You should plot the annual average sea ice
extent in Figure 2 to have a look.
 Determine the average sea ice extent for each halfmonth, i.e. average
over the years for each halfmonth.
 Determine the average sea ice extent over the entire data collection.
Store the answer in the variable avgSeaIceExtent
which you will need later.
 For each year, determine the number of halfmonths that exceed
the overall average calculated in Question 3.
 Determine the number of years whose annual average is less
than the overall average calculated in Question 3.
 Determine the number of years, within the last 10 years of data, whose
annual average is less than the overall average calculated in Question
3. (Note: Questions 5 and 6 together tell you something important about
the status of ice extents.) Hint:
end is useful
here.
 Determine the 10 years that have the lowest 10 annual average sea ice
extents. You should arrange the 10 years such that their corresponding
annual averages are sorted in ascending order. That is, the first year
in the list is the year that has the lowest annual average sea ice
extent, the second year has the second lowest annual average and so on.
Hint: Use the sort
function with two outputs. You will also need the vector years.
 What is the largest decrease
in sea ice extent between two consecutive halfmonthly measurements?
 It is not possible to use the matrix NHsea_ice_extents
and vectorisation to answer this question. This is because it
is not easy to compute the difference between the sea ice extents in
the second half of December (last column of data) and first half of
January (first column of data). It is easier if the halfmonthly data
are arranged chronologically in a vector; we will call this vector NHsea_ice_extents_one_column.
You can convert NHsea_ice_extents
into NHsea_ice_extents_one_column
using one of the following two vectorisation methods. After that you
can use NHsea_ice_extents_one_column
to answer this question. Hint:
You may also need some of these functions: diff,
max, min,
abs.

%
Method 1. There are two steps.
% Step 1. Compute the transpose.
NHsea_ice_extents_tranpose = NHsea_ice_extents.';
% After this, the first column
contains data for the first year, the second column contains
data for the second year and so on.
% What we want to do is to create a vector by stacking the
2nd column after the 1st, 3rd column after the 2nd, then
% this tall column vector is what we want. Luckily, Matlab
can do this easily with its colon : notation.
% Example: m(:) is to stack the columns of the matrix m, one
by one from the first column to the last, into one tall
vector
NHSea_ice_extents_half_monthly =
NHsea_ice_extents_transpose(:);
% Method 2. The first step is to
compute the transpose. The second step is to use the
builtin function reshape to stack the columns up.
% We can merge the two steps and write it as one line, in
the following.
% The code asks Matlab to reshape the transpose of
NHsea_ice_extents into a matrix with
numel(NHsea_ice_extents) rows and 1 column
% The builtin function numel returns the number of elements
in a matrix
NHSea_ice_extents_half_monthly =
reshape(NHsea_ice_extents.',numel(NHsea_ice_extents),1); 
 Each year, the sea ice extent peaked in a certain halfmonth. Which
halfmonth was the annual peak most frequently found? Hint:
If you use max
with two outputs and multiple inputs, you can determine in which
halfmonth the peak was for each year. After that, the function
mode comes in
handy.
 The matrix NHsea_ice_extents
contains halfmonthly data, but you want to work with monthly data. You
want to obtain a matrix NHsea_ice_extents_monthly
which contains monthly data. The matrix should have 35 rows and
12 columns. Column 1 contains the average of the two measurements in
January, Column 2 for February, etc. The problem is to compute NHsea_ice_extents_monthly
from NHsea_ice_extents
using only vectorisation. You may find the reshape
function and transpose useful. If you want some hint, click here.
 (Forum question) if want to compute the mean of the entire data set,
then you can use any one of the following three commands: mean(mean(NHsea_ice_extents,1)),
mean(mean(NHsea_ice_extents,2)),
and mean(NHsea_ice_extents_one_column).
They all give you the same answer. However, median is different. If you
want to compute the median of the entire data set, which of the
following three Matlab commands would give you the correct answer? You
can check that these three commands return different numerical values.
 median(median(NHsea_ice_extents,1))
 median(median(NHsea_ice_extents,2))
 median(NHsea_ice_extents_one_column)
Part B: Plotting a function
An object of mass m is suspended from
the end of a rigid horizontal beam of length b
that is attached to a wall by a pivot. The beam is supported by a cable of
the same length as the beam attached to the beam at a distance d
from the wall. The tension on this cable is given by the equation
 Write a function that calculates the tension in Newtons, given the
mass m in kg, and lengths b
and d in metres as parameters. You need
to make sure your function should work even the inputs are vectors. You
can assume the acceleration due to gravity g is 9.81.
 Test your program with 100kg, 2m and 1.2m respectively. The answer
should be about 2043N.
 We want to plot tension as a function of d.
Generate a vector (say d) of distances from 0.4 to 1.95, with
150 values.
 Now generate a vector T of corresponding tension values,
using your function and the fixed values for m
and b. It should require nothing more
than passing the vector as the last argument.
If you get a message like Error using
__*__ and you don't understand why, click
here.
 Now plot T against d and note the shape, something
like a skewed parabola.
 Determine the length d that
gives the minimum tension T.
 You want to use the function to compute the tension of the cable for
the following three cases. The restriction
is that you are only allowed to call the function only once.
Hint: You need to define three
vectors of m, b
and d values.
 m = 100, b = 2, d = 1
 m = 200, b = 3, d = 2
 m = 300, b = 4, d = 3
References: Part B is based on Chapman (2012),
Exercise 5.27.
Part A data:
Fetterer, F., K. Knowles, W. Meier, and M. Savoie. (2002, updated daily) Sea
Ice Index, Northern Hemisphere data set. Boulder, Colorado USA:
National Snow and Ice Data Center. http://dx.doi.org/10.7265/N5QJ7F7W.