Polynomials, Curve Fitting, and
Interpolation
MATLAB An Introduction With Applications, 6th Edition
1
8.0
In this chapter will study
• Polynomials – functions of a special form
that arise often in science and engineering
• Curve fitting – finding exact function of a
specified form that represents the data with
the least amount of error
• Interpolation – estimating values
between data points
2
8.1 POLYNOMIALS
A polynomial is a function of the form
𝑓𝑓 𝑥𝑥 = 𝑎𝑎𝑛𝑛 𝑥𝑥 𝑛𝑛 + 𝑎𝑎𝑛𝑛−1 𝑥𝑥 𝑛𝑛−1 + ⋯ + 𝑎𝑎1 𝑥𝑥+𝑎𝑎0
where 𝑎𝑎𝑛𝑛 , 𝑎𝑎𝑛𝑛−1 , … , 𝑎𝑎1 , 𝑎𝑎0 are real and
n is a non-negative integer
• n is called the degree or order of the
polynomial
• A constant is a polynomial of degree zero
3
8.1 POLYNOMIALS
MATLAB represents a polynomial by a
row vector
• First vector element is coefficient of x to
highest power, i.e., an
• Second element is an-1
• nth element is a1
• Element n+1 is a0
MATLAB represents a polynomial of
degree n by a vector of length n+1
4
8.1 POLYNOMIALS
Vector must include all polynomial
coefficients, even those that are zero
5
8.1.1 Value of a Polynomial
MATLAB function polyval computes
the value of a polynomial
• p is vector of polynomial coefficients
• x is scalar, vector, or matrix of points at
which polynomial should be evaluated
For vector or matrix, polyval performs
elementwise evaluation
6
8.1.2 Roots of a Polynomial
The roots of a polynomial are the
values of the independent variable
that make the polynomial be zero
• A polynomial of degree n has exactly n
roots, though some may be repeated
MATLAB function roots finds all of
the roots of a polynomial
7
8.1.2 Roots of a Polynomial
EXAMPLE
Find all three roots of the polynomial
31
2
𝑓𝑓 𝑥𝑥 = 𝑥𝑥 3 − 23
𝑥𝑥
+
𝑥𝑥
−
5
2
2
>> p = [ 1 -23/2 31/2 -5 ];
>> roots( p )
ans =
10.0000
1.0000
0.5000
8
8.1.2 Roots of a Polynomial
If you know the roots of a polynomial,
you can get the polynomial's
coefficients with p = poly( r )
where r is a vector of roots
>> r = [ -1 -1 2 3 ];
>> p = poly( r )
p =
1
-3
-3
7
6
>> roots( p ) % verify that get roots
ans = 3.0000
2.0000
-1.0000
-1.0000
9
8.2 CURVE FITTING
Curve fitting is the process of
adjusting a mathematical function so
that it lays as closely as possible to a
set of data points
• Can then use function as a mathematical
model of data
In this section will study basic curvefitting techniques and related
MATLAB tools
10
8.2.1 Curve Fitting with Polynomials; The polyfit Function
Two general ways to fit a polynomial
to data points
1. Polynomial must pass through
every data point
2. Polynomial does not need to pass
through every data point
11
8.2.1 Curve Fitting with Polynomials; The polyfit Function
Polynomials that pass through all the data points:
Given n data points (xi,yi), can make a
polynomial of degree n-1 that will pass
through all n points. For example,
• Given two points, can write a linear equation
(polynomial of degree one)
y = mx + b that passes through both points
• Given three points, can write a quadratic
equation (polynomial of degree two)
y = ax2 + bx + c that goes through all three
points
12
8.2.1 Curve Fitting with Polynomials; The polyfit Function
Polynomials that do not necessarily pass through any of the points:
Given a set of n data points (xi,yi), can
often make a polynomial of degree less
than n-1 that may not pass through any
of the points but still approximates the
set overall
Most common method of doing this is
called the least-squares fit
13
8.2.1 Curve Fitting with Polynomials; The polyfit Function
To make a least-squares fit of a
polynomial p(x) of degree n to a set of
data points (xi,yi)
1. Compute the difference p(xi)-yi at each
data point
Difference is often called the residual or error
2. Square each difference
3. Sum the squares
4. Find the values of the n+1 coefficients of
p(x) that minimize this sum
14
8.2.1 Curve Fitting with Polynomials; The polyfit Function
EXAMPLE
15
8.2.1 Curve Fitting with Polynomials; The polyfit Function
MATLAB function polyfit computes
least-squares best fit of data points to a
polynomial
16
8.2.1 Curve Fitting with Polynomials; The polyfit Function
When use polyfit on a set of m data
points
• Can use any polynomial degree n such
that n ≤ m-1
If n = m-1 the polynomial will go through all of
the points
A polynomial of degree m-1, or similar
high degree, may not necessarily provide
the best overall fit because it may deviate
substantially between data points
17
8.2.2 Curve Fitting with Functions Other than Polynomials
Often need to fit functions that are not
polynomials. The four functions below are
commonly used and can be converted to
polynomials (in fact linear polynomials)
through mathematical tricks. Can then use
polyfit to fit them
18
8.2.2 Curve Fitting with Functions Other than Polynomials
19
8.2.2 Curve Fitting with Functions Other than Polynomials
Call polyfit for the functions as shown
The output p has two elements: p(1) is
the coefficient m above and p(2) is b
20
8.2.2 Curve Fitting with Functions Other than Polynomials
A good way to tell if any of the four
functions will be a good fit is to plot
them with the axes indicated. If the
data looks linear, use the
corresponding function in polyfit
21
8.2.2 Curve Fitting with Functions Other than Polynomials
Here are some MATLAB functions that
plot the axes different ways
• plot – x linear, y linear
• semilogx – x logarithmic, y linear
• semilogy – x linear, y logarithmic
• loglog – x logarithmic, y logarithmic
22
8.3 INTERPOLATION
Interpolation is estimation of values
between data points. MATLAB can
do interpolation with polynomials
or the Fourier transform
Won't discuss Fourier-transform
interpolation in this book
23
8.3 INTERPOLATION
One-dimensional interpolation:
linear interpolation is estimating
value between two data points by
connecting points with a straight
line and then using value on line
as estimated value
24
8.3 INTERPOLATION
Suppose have data
points (xi,yi) and
(xi+1,yi+1) and want
estimate of value y at x, with xi < x < xi+1
Figure shows (x,y) on straight line
between two data points, i.e., the
linearly-interpolated point. Equation for
y is
𝑦𝑦𝑖𝑖+1 − 𝑦𝑦𝑖𝑖
𝑦𝑦𝑖𝑖 𝑥𝑥𝑖𝑖+1 − 𝑦𝑦𝑖𝑖+1 𝑥𝑥𝑖𝑖
𝑦𝑦 =
𝑥𝑥 +
𝑥𝑥𝑖𝑖+1 − 𝑥𝑥𝑖𝑖
𝑥𝑥𝑖𝑖+1 − 𝑥𝑥𝑖𝑖
25
8.3 INTERPOLATION
In linear interpolation
• Curve between two data
points has constant slope
• In general, slope of curve changes at every
data point
Can get smoother interpolations by
using quadratic or cubic splines, which
are polynomials whose coefficients are
based only on data points near
interpolated point
26
8.3 INTERPOLATION
MATLAB function interp1() does
"one"
one-dimensional interpolation
27
8.3 INTERPOLATION
28