## ENGG1811 Lab 11: Matlab Functions and vectorisation

### 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.

### 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 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.

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 tab-separated, 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 (half-monthly 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 1 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.
1. Determine the annual average sea ice extent, i.e. for each year, compute the average of all the half-monthly 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.
2. Determine the average sea ice extent for each half-month, i.e. average over the years for each half-month.
3. Determine the average sea ice extent over the entire data collection. Store the answer in the variable avgSeaIceExtent which you will need later.
4. For each year, determine the number of half-months that exceed the overall average calculated in Question 3.
5. Determine the number of years whose annual average is less than the overall average calculated in Question 3.
6. 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.
7. 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.
8. What is the largest decrease in sea ice extent between two consecutive half-monthly 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 half-monthly 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 built-in 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 built-in function numel returns the number of elements in a matrix NHSea_ice_extents_half_monthly = reshape(NHsea_ice_extents.',numel(NHsea_ice_extents),1);
9. Each year, the sea ice extent peaked in a certain half-month. Which half-month was the annual peak most frequently found? Hint: If you use max with two outputs and multiple inputs, you can determine in which half-month the peak was for each year. After that, the function mode comes in handy.
10. (Optional) 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.
1. median(median(NHsea_ice_extents,1))
2. median(median(NHsea_ice_extents,2))
3. median(NHsea_ice_extents_one_column)
11. (Optional and challenging) The matrix NHsea_ice_extents contains half-monthly 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 useful.

We will give you answers to Questions 10 and 11 here in Week 12.

### 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

1. 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.
3. 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.
4. 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.

5. Now plot T against d and note the shape, something like a skewed parabola.
6. Determine the length d that gives the minimum tension T.
7. 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.
1. m = 100, b = 2, d = 1
2. m = 200, b = 3, d = 2
3. 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.