Ecor 2606 lab 8

download Ecor 2606 lab 8

of 2

Transcript of Ecor 2606 lab 8

  • 8/8/2019 Ecor 2606 lab 8

    1/2

    ECO R 2606 - Lab 8

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

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

    2/. Fit a straight line to the data (using polyfit) and create a plot showing the data points and the fittedline. The plot should have the same general appearance as the one shown below (though your valueswill 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 combinationsx = x markerso = circle markers- = solid line-- = dashed line. = dotted linek = black

    b = blueexample: plot(x, y, '.k'); % use dotted black line

    To get larger than default markers: end argument list with 'MarkerSize', 10For more details, search for "plot" in the Matlab help facility.

  • 8/8/2019 Ecor 2606 lab 8

    2/2

    3/. (Optional) Suppose we would like a numerical measure of how well our line fits the data (i.e. wewould 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:

    ( ) ( )

    =

    2222

    iiii

    iiii

    y yn x xn

    y x y xnr

    Obtain the correlation coefficient for your line fit by implementing this formula in Matlab. By takingadvantage of the function and operators described in the note below, the correlation coefficient can becalculated 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 firstelement 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 theselected 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.