Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views15 pages

Fourth Lecture

Hgj

Uploaded by

Yousif Kawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views15 pages

Fourth Lecture

Hgj

Uploaded by

Yousif Kawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

2 Polynomials, Curve Fitting, and Interpolation

2.1 Polynomial

Polynomials are mathematical expressions that are frequently used for problem

solving and modeling in science and engineering. In many cases an equation that

is written in the process of solving a problem is a polynomial, and the solution

of the problem is the zero of the polynomial. Matlab has a wide selection of

functions that are specifically designed for handling polynomials.

Polynomials are functions that have the form:

n 1
f ( x ) = an x n + an 1x + ... + a1 x + a0

The coefficients an , an 1, ..., a1 , a0 are real numbers, and n, which is a nonnegative

integer, is the degree, or order, of the polynomial.

Examples of polynomials are:

f ( x ) = 5x5 + 6x2 + 7x + 3 polynomial of degree 5.

f ( x ) = 2x2 4x + 10 polynomial of degree 2.

f ( x ) = 11x 5 polynomial of degree 1.

A constant (e.g., f ( x ) = 6) is a polynomial of degree 0. In Matlab, polynomials

are represented by a row vector an , an 1, ..., a1 , a0 in which the elements are the

coefficients. The first element is the coefficient of the x with the highest power.

17
The vector has to include all the coefficients, including the ones that are equal to 0.

For example:

Polynomial Matlab representation

8x + 5 p=[8 5]

2x2 4x + 10 d=[2 –4 10]

6x2 150, Matlab form: 6x2 + 0x 150 h=[6 0 –150]

5x5 + 6x2 7x, Matlab form:5x5 + 0x4 + 0x3 + 6x2 7x + 0 c=[5 0 0 6 –7 0]

2.1.1 Value of a Polynomial

The value of a polynomial at a point x can be calculated with the function polyval

which has the form:

polyval(p,x)

Where p is a vector with the coefficients of the polynomial, and x is a number,

or a variable that has an assigned value, or a computable expression.

x can also be a vector. In such a case the polynomial is calculated for each element

(element-by-element), and the answer is a vector, with the corresponding values

of the polynomial.

18
Example *

Calculating polynomials with Matlab for the polynomial

f ( x ) = x5 12.1x4 + 40.59x3 17.015x2 71.95x + 35.88

(a) Calculate f(9) .

(b) Plot the polynomial for 1.5  x  6.7.

Solution

The problem is solved in the Command Window.

(a) The coefficients of the polynomials are assigned to vector p. The function

polyval is then used to calculate the value at x = 9.

>> p = [1 -12.1 40.59 -17.015 -71.95 35.88];

>> polyval(p,9)

ans = 7.2611e+003

(b) To plot the polynomial, a vector x is first defined with elements ranging from

–1.5 to 6.7. Then a vector y is created with the values of the polynomial for every

element of x. Finally, a plot of x vs. y is made, figure 1.

>> x=-1.5:0.1:6.7;

>> y=polyval(p,x);

>> plot(x, y)

19
Figure 1

2.1.2 Roots of a Polynomial

The roots of a polynomial are the values of the argument for which the value

of the polynomial is equal to zero. For example, the roots of the polynomial

f ( x ) = x2 2x 3 are the values of x for which x2 2x 3 = 0, which are

x= 1 and x = 3.

Matlab has a function, called roots, that determines the root, or roots, of a

polynomial. The form of the function is:

r = roots(p)

Where p is a row vector with the coefficients of the polynomial, and r is a

column vector with the roots of the polynomial.

20
For example, the roots of the polynomial in the example * can be determined

by:

>> p= [1 -12.1 40.59 -17.015 -71.95 35.88];

>> r=roots(p)

r=

6.5000

4.0000

2.3000

-1.2000

0.5000

When the roots are known, the polynomial can actually be written as:

f ( x ) = ( x + 1.2)( x 0.5)( x 2.3)( x 4)( x 6.5)

The roots command is very useful for finding the roots of a quadratic equation.

For example, to find the roots of f ( x ) = 4x2 + 10x 8, type:

>> roots([4 10 -8])

ans =

-3.1375

0.6375

21
When the roots of a polynomial are known, the poly command can be used for

determining the coefficients of the polynomial. The form of the poly command

is:

p = poly(r)

Where p is a row vector with the coefficients of the polynomial, and r is a

vector (row or column) with the roots of the polynomial.

For example, the coefficients of the polynomial in the example * can be obtained

from the roots of the polynomial (see above) by:

>> r=[6.5 4 2.3 -1.2 0.5];

>> p=poly(r)

p=

1.0000 -12.1000 40.5900 -17.0150 -71.9500 35.8800

22
2.2 Curve Fitting

Curve fitting, also called regression analysis, is a process of fitting a function to

a set of data points. The function can then be used as a mathematical model

of the data. Since there are many types of functions (linear, polynomial, power,

exponential, etc.), curve fitting can be a complicated process. Many times one has

some idea of the type of function that might fit the given data and will need only

to determine the coefficients of the function. In other situations, where nothing is

known about the data, it is possible to make different types of plots that provide

information about possible forms of functions that might fit the data well. This

section describes some of the basic techniques for curve fitting and the tools that

Matlab has for this purpose.

2.2.1 Curve Fitting with Polynomials; The polyfit Function

Polynomials can be used to fit data points in two ways. In one the polynomial

passes through all the data points, and in the other the polynomial does not neces-

sarily pass through any of the points, but overall gives a good approximation of

the data. The two options are described below.

Polynomials that pass through all the points:

When n points ( xi , yi ) are given, it is possible to write a polynomial of degree

n-1 that passes through all the points. For example, if two points are given it is

23
possible to write a linear equation in the form of y = mx + b that passes through

the points. With three points the equation has the form of y = ax2 + bx + c.

With n points the polynomial has the form an n 1 n 2 + ... + a1 x + a0 .


1x + an 2x

The coefficients of the polynomial are determined by substituting each point in

the polynomial and then solving the n equations for the coefficients. As will be

shown later in this section, polynomials of high degree might give a large error if

they are used to estimate values between data points.

Polynomials that do not necessarily pass through any of the points:

When n points are given, it is possible to write a polynomial of degree less than n-

1 that does not necessarily pass through any of the points, but overall approximates

the data. The most common method of finding the best fit to data points is

the method of least squares. In this method the coefficients of the polynomial

are determined by minimizing the sum of the squares of the residuals at all the

data points. The residual at each point is defined as the difference between the

value of the polynomial and the value of the data. For example, consider the

case of finding the equation of a straight line that best fits four data points as

shown in Figure 2. The points are ( x1 , y1 ), ( x2 , y2 ), ( x3 , y3 ), and ( x4 , y4 ) , and the

polynomial of the first degree can be written as f ( x ) = a1 x + a0 . The residual,

Ri , at each point is the difference between the value of the function at xi and yi ,

Ri = f ( xi ) yi . An equation for the sum of the squares of the residuals Ri of all

24
Figure 2: Least squares fitting of first-degree polynomial to four points.

the points is given by

R = [ f ( x1 ) y1 ]2 + [ f ( x2 ) y2 ]2 + [ f ( x3 ) y3 ]2 + [ f ( x4 ) y4 ]2

The same procedure can be followed with more points and higher-order polynomials.

More details on the least squares method can be found in books on numerical

analysis.

Curve fitting with polynomials is done in Matlab with the polyfit function,

which uses the least squares method. The basic form of the polyfit function is:

p = polyfit(x, y, n)

Where p is the vector of the coefficients of the polynomial that fits the data, x is

25
a vector with the horizontal coordinates of the data points (independent variable),

y is a vector with the vertical coordinates of the data points (dependent variable)

and n is the degree of the polynomial.

For the same set of m points, the polyfit function can be used to fit polynomials

of any order up to m-1. If n=1 the polynomial is a straight line, if n=2 the polynomial

is a parabola, and so on. The polynomial passes through all the points if n

= m-1 (the order of the polynomial is one less than the number of points). It

should be pointed out here that a polynomial that passes through all the points, or

polynomials with higher order, do not necessarily give a better fit overall. High-

order polynomials can deviate significantly between the data points.

Figure 3 shows how polynomials of different degrees fit the same set of data

points. A set of seven points is given by (0.9, 0.9), (1.5, 1.5), (3, 2.5), (4, 5.1),(6,

4.5), (8, 4.9), and (9.5, 6.3). The points are fitted using the polyfit function with

polynomials of degrees 1 through 6. Each plot in Figure 3 shows the same data

points, marked with circles, and a curve-fitted line that corresponds to a polynomial

of the specified degree. It can be seen that the polynomial with n = 1 is a straight

line, and with n = 2 is a slightly curved line. As the degree of the polynomial

increases, the line develops more bends such that it passes closer to more points.

When n = 6, which is one less than the number of points, the line passes through

26
(a) n=1. (b) n=2.

(c) n=3. (d) n=4.

(e) n=5. (f) n=6.

Figure 3: Fitting data with polynomials of different order.

27
all the points. However, between some of the points, the line deviates significantly

from the trend of the data.

The script file used to generate the plot in Figure 3(c) (the polynomial with n = 3)

is shown below.

x=[0.9 1.5 3 4 6 8 9.5];

y=[0.9 1.5 2.5 5.1 4.5 4.9 6.3];

p=polyfit(x,y,3)

xp=0.9:0.1:9.5;

yp=polyval(p,xp)

plot(x,y,’o’,xp,yp)

xlabel(’x’);

ylabel(’y’)

Note that in order to plot the polynomial (the line) a new vector xp with small

spacing is created. This vector is then used with the function polyval to create a

vector yp with the value of the polynomial for each element of xp.

When the script file is executed, the following vector p is displayed in the Command

Window.

p= 0.0220 -0.4005 2.6138 -1.4158

This means that the polynomial of the third degree in Figure 3(c) has the form

0.022 x3 0.4005 x2 + 2.6138 x 1.4148.

28
2.2.2 Curve Fitting with Functions Other than Polynomials

Many situations in science and engineering require fitting functions that are not

polynomials to given data. Theoretically, any function can be used to model data

within some range. For a particular data set, however, some functions provide

a better fit than others. In addition, determining the best-fitting coefficients can

be more difficult for some functions than for others. This section covers curve

fitting with power, exponential, logarithmic, and reciprocal functions, which are

commonly used. The forms of these functions are:

y = bx m (power function)

y = bemx or y = b10mx (exponential function)

y = mln( x ) + b or y = mlog( x ) + b (logarithmic function)

1
y= mx +b (reciprocal function)

All of these functions can easily be fitted to given data with the polyfit function.

This is done by rewriting the functions in a form that can be fitted with a linear

polynomial (n = 1), which is

y = mx + b

The logarithmic function is already in this form, and the power, exponential

and reciprocal equations can be rewritten as:

29
ln(y) = mln( x ) + ln(b) (power function)

ln(y) = mx + ln(b) or log(y) = mx + log(b) (exponential function)

1
y = mx + b (reciprocal function)

These equations describe a linear relationship between ln(y) and ln( x ) for

the power function, between ln(y) and x for the exponential function, between

1
y and ln( x ) or log( x ) for the logarithmic function, and between y and x for the

reciprocal function.

2.3 Interpolation

Interpolation is the estimation of values between data points. Matlab has interpolation

functions that are based on polynomials, which are described in this section, and

on Fourier transformation.

One-dimensional interpolation in Matlab is done with the interp1 (the last

character is the number one) function, which has the form:

yi = interp1( x, y, xi ,0 method0 )

Where yi is the interpolated value, x is a vector with the horizontal coordinates

of the input data points (independent variable), y is a vector with the vertical

coordinates of the input data points (dependent variable), xi is the horizontal

30
coordinate of the interpolation point (independent variable) and ’method’ is the

method of interpolation.

Matlab can do the interpolation using one of several methods that can be

specified. These methods include:

‘nearest’ returns the value of the data point that is nearest to the interpolated

point.

‘linear’ uses linear spline interpolation.

‘spline’ uses cubic spline interpolation.

‘pchip’ uses piecewise cubic Hermite interpolation, also called ‘cubic’

31

You might also like