Lab IAT 06
Lab IAT 06
Claudia Zoccarato
E-mail: [email protected]
June, 12 2017
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Introduction
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Example - Interpolation vs Curve fitting
Given the set of data points (xi , yi ) with i = 0, .., 4, determine the
interpolating polynomial p4 (x) and the least square fitting line p1 (x).
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Solution with MATLAB
1 Enter the input data (xi , yi ) as vectors x and y using the values given
in the table.
2 Compute the n + 1 coefficients of the interpolating polynomial or the
the least square fitting line of degree n:
pn (x) = a0 + a1 x + a2 x2 + ... + an xn
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
1. Enter the input data
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
2. Compute the n + 1 coefficients
The function polyfit (Polynomial Curve Fitting) returns the coef-
ficients for a polynomial pm (x) of degree m that is a best fit (in a
least-squares sense) for the data. The coefficients will be m + 1.
MATLAB syntax:
>> p = polyfit(x,y,m)
The vectors x e y are the points of the data set xi , yi . If m = 1 the
coefficients of the straigth line curve fitting is computed.
If m = n the coefficients of the interpolating polynomial is computed.
The function output os the vector p of the m + 1 coefficients.
The coefficients in p are in descending powers:
pm (x) = p(1)xm + p(2)x(m−1) + ... + p(m)x + p(m + 1)
Example:
x = linspace(0,4*pi,10)
y = sin(x);
p = polyfit(x,y,7);
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
3. Evaluate pn (x) in requested data points
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Load data in MATLAB
Load the data from a text file to avoid typing the data in the script.
Load of two files. The components of the vectors x e y are the data
points xi and yi :
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Exercises
1 The following table reports the some experimental data linking stresses, σ,
and deformations, :
σi 720 750 800 520 1000 180
i 0.0020 0.0045 0.0060 0.0013 0.0085 0.0005
Interpolate the data set with an interpolating polynomial of fifth degree and
compute the deformation p corresponding to the design stress σp =735. Sub-
stitute the interpolating polynomial with a straight line to fit the data (least
square approximation):
= a + bσ
and evaluate the new value of p . Then, build the least square approximation
using the following fitting equation:
= aσ b
such that (0) = 0 (HINT: use a logarithmic tranformation). Plot in the same
figure the data set, the iinterpolating polynomial, the straight line and power
law fitting curves. Discuss the result.
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Exercises
2 Given the following data set:
xi 4.0 4.2 4.5 4.7 5.1 5.5 5.9 6.3 6.8 7.1
f (xi ) 102.56 120.84 131.23 142.00 168.95 196.12 225.00 259.47 293.29 342.71
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017