Often data are available from experiments as set of points, and it would be useful to describe these data with a known mathematical function. This is known as curve fitting. Commonly used functions for fitting are polynomials. Fitting a set of points with a polynomial of order :
consists of finding the optimal combination of coefficients such that the curve fits the data overall. The function polyfit in Matlab generates these coefficients, with the principle of the least square.
-
Use dlmread to read in the first 2 columns of the file experiment.txt and store them in a matrix. Extract the first and second columns of the matrix into two vectors xexp and yexp, respectively. Plot yexp vs xexp as points.
-
Fit the points plotted in step 1 with three different polynomials, of order 1, 2 and 3 respectively. Call the coefficients of these fits cn1, cn2 and cn3.
-
Evaluate and plot the three polynomials with coefficients found in step 2 at points xf = [1:0.1:10]. Call them p1, p2, p3, and plot them vs xf in the same graph of step 1 (i.e. together with yexp vs xexp). Use different data symbols and/or line types for the 3 different data sets, use legend to give each curve a name. Turn the grid on.
The accuracy of the fit between a set of points and a polynomial is determined by first calculating the error, also known as the residual, which is the difference between a point and the value of the polynomial, at each point:
Then the residuals are used to determine the total error. The total error E can be evaluated in different ways: in the least square methods is the sum of the squares of the residuals:
-
Calculate and display the total error for each polynomial fit done in the first curve fitting task. For any number of points it is possible to fit them with a polynomial of order that passes exactly for all the points. However, when many points are involved, the use of high-order polynomials generates significant differences between some of the points.
-
Approximate the points given in Task 1 with a polynomial of order 9. Plot this fit at points xf = [1:0.1:10]. [Matlab may give you a warning: this is because it knows already that it is not a good fitting, and in its kind generosity, warns you about it].
-
Compute the total error . How does the error compare against the fitting with lower order polynomials?
Since you may need to fit experimental data often during the rest of your studies, it is convenient to write a function, once forever, that you may reuse at any time in the future.
-
Write a function, mypolyfit, that receives a set of x and y data points and the order desired for the fitting polynomial, and outputs the fitting polynomial at points x and the total error . You may test the function with the data of Tasks 1 and 2.
-
Read the file wave.txt, containing experimental data points. Fit these points with polynomials of order 1 up to 8. Within the same figure, but in two different tiled axes (to do so explore and use the command subplot) plot:
- The data points and the various fits together.
- The total error vs the order of the fitting polynomials (as points).
A spring manufacturer measures the force-extension characteristics,, of all the manufactured tension springs. The data are automatically measured by a machine and recorded in a data file. The file contains the spring serial number and the measured extension (in mm) at 10 predefined loads of 10N to 100N in increments of 10 N, for a batch of 100 springs. Write a Matlab script that fits a linear curve to the measurements for each spring and deduces its spring constant .
- Read in the measured data from springdata.csv and calculate the spring stiffness for each spring. Save the spring serial number followed by the computed spring stiffness in a separate file called stiffness.csv.
(Back to README.md)