You are on page 1of 4

# Matlab exercises, 1st session

Instructions: Create a new directory to the Z-drive of the Computing Centres Windows system and save all files you write during the course into it. Write the code of each exercise into its own mfile as a script. Write all functions into separate files (remember: name of a function = filename!!!). Name the script files like d1e1.m (day 1, exercise 1). To be able to run your own m-files directly from the command window, you must add your directory to Matlabs path or change current working directory.

Data types
1. Structures and cell arrays a) The table below contains some measurements Measurement Filename Time Millisecond Serial Volume Length Value XML2003-01-10.xml 10.1.2003 13:13:46 474 731590.551232337962963 0.662 18.13

Store the measurements into a structure and into a cell array variable and print the variables to the command window. How do these data types differ from each other? b) How will the situation change (or will it?) if there are a lot of measurements (same fields but many values from different time instants)? Save also e.g. the following values in a sensible way to the same structure (a table?) and to a cell array. Below the table there is an example of how the variables cellArray and structure would be like if the data were stored in them. Compare the amounts of memory the variables reserve. Measurement Filename Time Millisecond Serial Volume Length Measurement Filename Time Millisecond Serial Volume Length Value XML2003-01-10.xml 10.1.2003 13:15:42 225 731590.551232337973454 0.552 12.63 Value XML2003-01-11.xml 11.1.2003 08:43:27 912 731590.551232338072548 0.264 15.16

>> cellArray cellArray = {1x3 cell} {1x3 cell} double] [1x3 double] >> structure structure = 1x3 struct array with fields: Filename Time Millisecond Serial Volume Length [1x3 double] [1x3 double] [1x3

2. Write functions for calculating the following statistical quantities. Implement each of the given formulas in its own function without using Matlabs built-in functions. You may, however, use the function sum for adding as well as the functions length, size and sqrt. As an example, apply the functions to the data in the file testData.mat. In the formulas N stands for the number of measurements. a) Average
N

x=

x
i =1

## b) Standard deviation and variance of a sample

N 1 =

(x x )
i i =1

N 1

(standard deviation)

2 N 1 =

(x x )
i i =1

N 1

(variance)

Compute the average present in the formulas using the function you wrote in item a). Note that in item b) your function must return two values. c) Correlation coefficient
N

r=

( x x )( y y )
i i i =1

(x x ) ( y y )
2 i i i =1 i =1

.
2

In item c) study the correlation of the measurement first qualitatively by plotting the measurements using scatter. See the example figure below.

Interesting data 30

25

20 Second column

15

10

0.5

## 1.5 First column

2.5

d) Write a script that calls all the functions that were written above and prints the results to the command window. Example output:
Measurement 1: average 0.42478, deviation 0.30788, variance 0.094792. Measurement 2: average 13.7338, deviation 5.1712, variance 26.7417. Correlation coefficient of the measurements is 0.84035.

Compare the results given by your functions to those given by Matlabs built-in functions mean, std, var and corrcoef. 3. Write the function xycorr, where you create the variables x and y. The variables are vectors of length 1000. Variable x contains normally distributed random numbers and variable y the sine wave sin(2*pi*50*t), where t is the time vector between 0 1 seconds (choose a suitable sample interval). Add to y also noise that is uniformly distributed on the interval [0, 1]. After creating the variables the function computes the correlation coefficient of variables x and y using the function created in item c) of the previous exercise. Write also a script that calls xycorr 10 times and prints the values returned by the function to the command window. Why are the values that appear to the command window different? 4. Study the forces that affect a body in the origin of a coordinate system (figure below). The magnitude and direction of each distinct force is known, and the resulting total force affecting the body (magnitude and angle) should be computed. The forces are:

F1 F2 F3 F4

F4 F2 F3 F1

## The angles are given with respect to the positive x-axis.

Draw the forces F1 F4 affecting the body using the same color (blue) and the total force with a different color (red dashed line) to the same figure. The title of the figure must contain the magnitude and angle of the total force (cf. example figure below).
Hint: You can use the function line to draw the forces to the coordinate axes (i.e. there is no need to draw the arrowheads, a line is sufficient). For drawing and for computing the total force you need to know the x and y components of each force. In the case of the figure below the x and y components are defined as follows:

cos ( ) = sin ( ) =

x x = F cos ( ) F y y = F sin ( ) F

F x

The angles must be converted to radians before using sin or cos because in Matlab angles are given in radians. However, the angle of the total force must be given finally in degrees.
3

Example figure:
Total force: magnitude 0.99912 N, angle 73.8697 Total force: 0.99912 N, direction: 73.8697

-1

-2

-3 -3

-2

-1

0 x