Skip to content

Latest commit

 

History

History
87 lines (63 loc) · 6.6 KB

curve_fitting.md

File metadata and controls

87 lines (63 loc) · 6.6 KB

Curve fitting with polynomials

Task1: Curve fitting introduction

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 $N$ points with a polynomial of order $n<N$:

$$
p(x) = c_n x^n + c_{n-1} x^{n-1} + \dots + c_2 x^2 + c_1 x + c_0
$$

consists of finding the optimal combination of coefficients $c_n,c_{n-1},\dots,c_2,c_1,c_0$ such that the curve $p(x)$ fits the data overall. The function polyfit in Matlab generates these coefficients, with the principle of the least square.

  1. 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.

  2. 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.

  3. Evaluate and plot the three polynomials $p(x)$ 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.

Task2: Measuring how good a fit is

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:

$$
r_i = yexp_i - p(xexp_i)
$$

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 $E$ is the sum of the squares of the residuals:

$$
E = \sum_i^N r_i^2
$$

  1. Calculate and display the total error $E$ for each polynomial fit done in the first curve fitting task. For any number of $N$ points it is possible to fit them with a polynomial of order $N-1$ 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.

  2. 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].

  3. Compute the total error $E$. How does the error $E$ compare against the fitting with lower order polynomials?

Task 3: Write your own complete function for curve fitting

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.

  1. 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 $E$. You may test the function with the data of Tasks 1 and 2.

  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).

Task4: Analyse Real Data

A spring manufacturer measures the force-extension characteristics,$F=kx$, 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 $k$.

  1. 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)