Introduction to Numerical Methods and MATLAB
Programming for Engineers
Lecture 6: Polynomial Interpolation and Least Square
Fitting
Claudia Zoccarato
E-mail: [Link]@[Link]
June, 12 2017
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Introduction
Aim: MATLAB implementation of polynomial interpolation and least square
fitting.
Given a set of n + 1 data points (xi , yi ) with i = 0, 1, ..., n, where yi is the
value of the function f in xi or the value of an experimental data, the aim
is at:
determining the interpolating polynomial P (x) such that P (xi ) = yi
for i = 0, 1, ..., n (polynomial interpolation).
determining the straight line φ(x) = ax + b where the coefficients a
and b are given by least square approximation (curve fitting).
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
3 Evaluate pn (x) in the requested data points.
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
1. Enter the input data
1 Enter the values xi as a vecor:
>> x = [1 2 3 4 5]
2 Enter the values yi as a vecor:
>> y = [17.2 15.0 13.2 17.9 15.0]
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
The function polyval (Polynomial Evaluation) returns the value of a
polynomial pm (x) of degree m evaluated at x.
MATLAB syntax:
>> y = polyval(p,x)
The input argument p is a vector of length m + 1 whose elements are
the coefficients in descending powers of the polynomial to be evaluated.
The input argument x is a vector whose elements are the x points where
the polynomial is evaluated.
Example:
xval = [1 2 3];
yval = polyval(p,xval);
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 :
>> xx = load(’data [Link]’);
>> yy = load(’data [Link]’);
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
Find the coefficients a and b of the straight line y = ax + b which mini-
mizes the y deviations (vertical differences). Then, find the coefficients
a and b of approximation curves y = aebx and y = axb .
3 Of an unknown function f (x), the following information are known:
xi 1 2 2.5 4
f (xi ) 2 2.5 3.8 5
(a) Compute the interpolating polynomial P (x) which interpolates the
given data set. (b) Compute the straight line r(x) which fit the data
set in a least square sense. (c) Plot in the same figure, the data set,
the polynomial P (x) and the straight line r(x).
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Exercises
4 Compute the third degree polynomial to interpolate the following values
of the function f (x): f (0) = 3, f (1) = −3, f (2) = 1 e f (3) = 33.
Estimate the values f (0) and f 0 (0).
1
5 Interpolate the Runge function f (x) = 1+x 2 in the interval [−5, 5]
starting from 3 evenly spaced points in the interval. Then, build the
interpolating polynomials of degrees n = 4,6,8,12 and evenly spaced
points. Plot in the same figure the function f and the interpolating
polynomials. Comment the results.
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017