E5 MATLAB Assignment 3

This exercise builds upon the previous one.  Upon completion of the exercise you should be able to use MATLAB to

• store and access information stored in matrices,
• perform simple numerical manipulations on matrices,
• manipulate the data in matrices,
• import data from an Excel spreadsheet.

As you go through this document, you should enter the commands at the MATLAB prompt, and try variations on the commands to make sure that you fully understand.

You may want to refer to chapters 1,  2 and 5 of your text.

Matrices

(§2.1 in your text - relevant textbook sections will be bold purple text)  You have learned to store and manipulate individual numbers (scalars) as well as lists of numbers (vectors).  The notion of a list of numbers can be extended to multiple dimensions, giving rise to "arrays" of numbers.  In E5 the most common type of array is a two dimensional array.  To create an array with 2 rows and 3 columns (called a two-by-three, or 2x3 array), array we enter one row at a time, separating the rows by a semicolon.

`>> a = [1 4 5; 8 -1 6]`
```a =
1     4     5
8    -1     6```

(Note: you can cut the code from this document and past it directly into MATLAB; MATLAB will ignore any prompt characters, ">>" that are pasted.)

Sometimes it can be useful to enter the rows on separate lines

```>> a = [1 4 5;
8 -1 6]```
```a =
1     4     5
8    -1     6```

The size of an array can be determined

`>> size(a)`
```ans =
2     3```

Note that when refering to arrays as n-by-m, this always means n rows my m columns.  So the array "a" is 2x3 or 2 rows by 3 columns.  The row dimension always comes first.  To access the element in the 1st row and 2nd column, use two "indices."  Note: "indices" is the plural of "index" (and "matrices" is the plural of "matrix").

`>> a(1,2)`
```ans =
4```

For the next several examples define a bigger array, a 3x6 array of random numbers using MATLAB's "rand" function.

`>> myMat = rand(4,7) % Define a 4x7 array of random numbers`
```myMat =
0.7922 0.8491 0.7431 0.7060 0.0971 0.9502 0.7655
0.9595 0.9340 0.3922 0.0318 0.8235 0.0344 0.7952
0.6557 0.6787 0.6555 0.2769 0.6948 0.4387 0.1869
0.0357 0.7577 0.1712 0.0462 0.3171 0.3816 0.4898```

To define a new matrix that consists of rows 2 to 4 and columns 3 to 6, use a range of numbers for the indices (note: make sure you understand this step)

`>> newMat = myMat(2:4,3:6)`
```newMat =
0.3922    0.0318    0.8235    0.0344
0.6555    0.2769    0.6948    0.4387
0.1712    0.0462    0.3171    0.3816```

To pick rows 2 and 3, but all of the columns use a colon, ":", in the second index (the column index).

```>> row2_3 = myMat (2:3,:)
row2_3 =
0.9595    0.9340    0.3922    0.0318    0.8235    0.0344    0.7952
0.6557    0.6787    0.6555    0.2769    0.6948    0.4387    0.1869
```

Picking all of row 4 results in a 1x4 array, which is the same as the vectors (technically "row vectors") that you worked with last week.

```>> row4 = myMat(4,:)
row4 =
0.0357    0.7577    0.1712    0.0462    0.3171    0.3816    0.4898```

To pick the first column (rows=all, column=1), and a "column vector" results

`>> col1 = myMat(:,1) `
```col1 =    0.7922
0.9595    0.6557    0.0357```

Plotting and matrices

Consider the matrix created earlier.

`>> newMat`
```newMat =
0.3922    0.0318    0.8235    0.0344
0.6555    0.2769    0.6948    0.4387
0.1712    0.0462    0.3171    0.3816```

If we would like plot the individual rows we can create a vector for the x values of the plot (1 through 4, since there are four values), and plot each of the three the rows individually.

`>> x=1:4`
```x =
1     2     3     4```
```>> plot(x,newMat(1,:), x,newMat(2,:), x,newMat(3,:))  % Plot multiple lines
>> legend('Row 1','Row 2','Row 3')```

There is also a quicker way to do this.  It is described in the MATLAB help documentation.  It describes how you can use the "plot" command to plot all of the rows (or columns) of a matrix individually.  Note: This can also be found in the documentation browser (accessed through the "doc" command) by going through the documentation tree .

"If Y is a matrix, and x is a vector, plot(x,Y) successively plots the rows or columns of Y versus vector x, using different colors or line types for each. The row or column orientation varies depending on whether the number of elements in x matches the number of rows in Y or the number of columns. If Y is square, its columns are used."

In our case the vector "x" is of length 4, and the matrix "newMat" has four columns.  Using this description the three separate rows can be plotted with one short line (plus one line for legends).

```>> plot(x,newMat)  % Plot multiple lines (using more compact code)
>> legend('Row 1','Row 2','Row 3')```

This generates the same graph as the one shown above.

Importing Data

For the assignment this week you will need to import some data from an Excel spreadsheet and save it in MATLAB fomat.  Start by downloading the file "INTERVALDATA_6197101104_19081.xlsx" to your MATLAB directory.  The file  was provided by the electrical utility and it contains data, in half hour intervals, of the college's electric use (in a unit of kiloWatt-hours - a measure of energy.  You should see the file in the "Current Folder" pane of the MATLAB window (see below).

Right click on the file and choose "Import Data..." as shown above.

An "Import" window will open.  The columns in excel are enumerated alphabetically.  Column A is the college's account number, B is the meter number, C is the date, and rows D through AY are 48 half hour intervals throughout the day.  Scroll down to row 106 in the spreadsheet (the date in the third column will read "NAN" (or Not A Number), or show the date).  We want to import the data from the month of February, and this will consist of 29 rows and 48 columns. So, for the import range put in "D106:AY134" as shown below.

Hit the import button (the green arrow in the right of the "Import" pane).  This will create a variable called "untitled" that is 29x48  give it a more meaningful name and save it.

```>> febElecUse = untitled;
>> save febElecUse```

To recap: the file "febElecUse.mat" that was created has in it a variable called "febElecUse" that is a 29x48 matrix representing the college's electric use in February of 2012.  The columns represent 1/2 hour intervals starting at midnight of each day, and each row represents a day.  The first row represents February 29th.

Reversing elements in a vector

This may be a useful hint for completing the assignment

Hint: to create a vector and then reverse the elements you can use the following code (make sure you understand this):

`>> x = 0:2:10		% Create a vector with even numbers from 0 to 10.`
```x =
0     2     4     6     8    10```
`>> y = x(6:-1:1)	% Reverse the vector.`
```y =
10     8     6     4     2     0```

Recall that using the colon operator with three arguments, "x0:xinc:xf", creates a vector starting at x0, incrementing by xi and finishing at x0.  If there are only two arguments then the value xinc defaults to one.

To do

The task this week is similar to the previous one, however the code will be much shorter because you will use matrix structures instead of just vectors.  The task assumes you have created a file called "febElecUse.mat" as described in the previous section.

2. The matrix "febElecUse" has the last day of the month as the first row, and the first day of the month as the last row.  Reverse that so the first row is the first day.  You may want to use the hint above.
3. Create a graph showing energy usage for the first seven days of February.
• The vertical axis should have units of kiloWatt-hours, and the horizontal axis should be hours from midnight .
• The graph should have axis labels, a title and a legend and should look similar to the one below.  The legend should list the days of the week; February 1st, 2012 was a Wednesday.
• Move I have moved the legend from its default location so it doesn't obscure the data (using the "legend" command's  optional arguments that determines placement of the legend).  This is described in MATLAB's help documentation.
• Make the horizontal axis from 0 to 23.5.  This can be done with the "axis" command.

Note that:
• Energy use is lowest in the early morning.
• Energy use stays low until almost 10:00 a.m. on the weekend).  Energy use increases, unsurprisingly, much earlier on a weekday.
• Energy use peaks at about 18:00 hours = 6:00 p.m. on weekdays and drops rapidly as midnight approaches.
• Energy use drops off much more slowly on a weekend night.
5. When you get your code working, publish it to a Word document.

Hints/techniques to consider while debugging your script:

• To execute a single cell select "" at the MATLAB menu  at the top of the screen (or type "Ctrl-Enter").
• To execute a single cell and move on to the next cell select "" (or "Ctrl-Shift-Enter").
• To execute the entire script go to "" (or "" if the file is already saved).  Hitting the "F5" key also works.
• Before you can "publish" the file to a Word document, the publishing tool needs to be configured.  From the editor window choose .  Change the file type to "doc" and the directory to a convenient place to store the file (perhaps the desktop, or your "H:" drive) as shown below.  You can also review "publishing" in the first lab.
• If you have to go through the publish process more than once, be sure to close the Word document so MATLAB can overwrite it.

To turn in

(via moodle)

• 5 pts for clear and thorough commenting of your code.
• 5 pts for clearly written and concise MATLAB code
• 3 pts for clear and appropriate formatting of the Word document
• 3 pts for a correct result (if the code is clearly thought out and presented, the result should be correct)
• 2 pts for turning in the MATLAB (.m) file
• 2 pts for turning in the Word or pdf (.doc or .pdf) file.

Arrays with more than two dimensions

This topic may be skipped - read only if you want to.

You can define matrices with more than 2 dimensions.  For example, a 2x5x4 matrix of random numbers.

`>> mat3D = rand(2,5,4)`
```mat3D(:,:,1) =
0.6020    0.6541    0.7482    0.0838    0.9133
0.2630    0.6892    0.4505    0.2290    0.1524
mat3D(:,:,2) =
0.8258    0.9961    0.4427    0.9619    0.7749
0.5383    0.0782    0.1067    0.0046    0.8173
mat3D(:,:,3) =
0.8687    0.3998    0.8001    0.9106    0.2638
0.0844    0.2599    0.4314    0.1818    0.1455
mat3D(:,:,4) =
0.1361    0.5797    0.1450    0.6221    0.5132
0.8693    0.5499    0.8530    0.3510    0.4018```

Call the third dimension the "level."  The element in the 2nd row, 4th column and 3rd level is 0.1818.  This is depicted below followed by MATLAB code.

`>> mat3D(2,4,3)`
```ans =
0.1818```