Sunteți pe pagina 1din 2

ECOR 2606 - Lab 8

An experiment has produced some data. This data can be found in file results.m. The data is in the form
of a two dimensional matrix. The first row of this array contains the x values and the second row
contains the corresponding y values.

1/. It would be more convenient if the x and y data were in separate vectors. Create a vector x containing
the x values and a vector y containing the y values. This can be done easily by using appropriate matrix
indices.

2/. Fit a straight line to the data (using polyfit) and create a plot showing the data points and the fitted
line. The plot should have the same general appearance as the one shown below (though your values
will be different). What is the equation for your line?

Matlab Notes:

To plot multiple lines on the same plot: plot (x, y, xx, yy, xxx, yyy, ...);
To select plot options: plot (x, y, 'opts', xx, yy, 'opts'....);
opts is one or more of the following letters or letter combinations
x = x markers
o = circle markers
- = solid line
-- = dashed line
. = dotted line
k = black
b = blue
example: plot(x, y, '.k'); % use dotted black line
To get larger than default markers: end argument list with 'MarkerSize', 10
For more details, search for "plot" in the Matlab help facility.
3/. (Optional) Suppose we would like a numerical measure of how well our line fits the data (i.e. we
would like to know the correlation coefficient). This quantity is unfortunately not provided by polyfit
but it can be calculated (assuming a straight line fit) using the formula below:

n∑ xi yi − ∑ x∑
i yi
r=
n∑ xi − ( ∑ x ) n∑ y (∑ y )
2 2 2 2
i i − i

Obtain the correlation coefficient for your line fit by implementing this formula in Matlab. By taking
advantage of the function and operators described in the note below, the correlation coefficient can be
calculated without using any loops.

Matlab Notes:

The elements of a vector or array can be added up by using "sum": sum(x) = x(1)+x(2) +x(3) ....
The .^ operator creates a new vector by raising every element of a vector to some power.
example: [1 2 3].^2 is [1 4 9]
The .* operator creates a new vector by multiplying the elements of two vectors.
example: [1 2 3] .* [2 4 6] is [2 8 16]

4/. Create two new vectors (xx and yy) containing every fourth element of x and y (starting with the first
element in each case). This can be done without listing all of the desired points, without using a .loop,
and in a way that will work regardless of how long the vectors are. Once you have created the vectors
(either cleverly or otherwise) use polyfit to find the n-1th order polynomial that passes through all of the
selected points. Create a plot showing all of the original data points and the interpolating polynomial. If
the polynomial does not pass through all of the selected points you've done something wrong.

Submit a script file that demonstrates that you attempted to complete this lab.

S-ar putea să vă placă și