polynomials, curve fitting and interpolation. in this chapter will study polynomials – functions...

46
Polynomials, Curve Fitting and Interpolation

Upload: richard-ball

Post on 18-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Polynomials, Curve Fitting and Interpolation

Page 2: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 3: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 4: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Vector must include all polynomial coefficients, even those that are zero

POLYNOMIALS

Page 5: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

POLYNOMIALS

Page 6: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 7: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

If you know the roots of a polynomial, you can get the polynomial's coefficients withp = 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

Roots of a Polynomial

Page 8: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Addition, Multiplication, and Division of Polynomials

Addition:

To add (subtract) two polynomials, add (subtract) their vectors of coefficients

• If one vector is shorter, must stick enough zeroes in front of it so same size as longer

Page 9: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Multiplication:

Multiply two polynomials with the built-in function conv, like so:

c = conv( a, b )

where a and b are two vectors of polynomial coefficients and c is a vector of the coefficients of the product

• a and b can be different degrees

Addition, Multiplication, and Division of Polynomials

Page 10: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Division:

Divide two polynomials with the built-in function deconv, which has the form

Addition, Multiplication, and Division of Polynomials

Page 11: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Can calculate derivative of a polynomial, product of polynomials, or quotient of polynomials with the MATLAB function polyder

Page 12: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

[

Page 13: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

This section will study basic curve-fitting techniques and related MATLAB tools.

Page 14: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 15: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 16: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 17: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 18: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

EXAMPLE

Page 19: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

MATLAB function polyfit computes least-squares best fit of data points to a polynomial

Page 20: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 21: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

polyfit – An example>> x = linspace(0,1,5)x = 0 0.2500 0.5000 0.7500 1.0000>> y = 1./(1+x)y = 1.0000 0.8000 0.6667 0.5714 0.5000>> p1 = polyfit(x,y,4)p1 = 0.1524 -0.5333 0.8667 -0.9857 1.0000>> plot(x,y,'o')>> f1 = polyval(p1,x)f1 = 1.0000 0.8000 0.6667 0.5714 0.5000>> hold on>> plot(x,f1,'r--')

Page 22: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

polyfit – Another example>> x = linspace(0,1,5)

x = 0 0.2500 0.5000 0.7500 1.0000>> y = 1./(1+x)y = 1.0000 0.8000 0.6667 0.5714 0.5000>> p2 = polyfit(x,y,2)p2 = 0.3374 -0.8288 0.9955>> plot(x,y,'o')>> f2 = polyval(p2,x)f2 = 0.9955 0.8094 0.6654 0.5637 0.5041>> hold on>> plot(x,f2,'r--')

Not bad at all!

Page 23: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

polyfit – Another exampleThe following script compares actual data points with

different polynomial equations of degrees 1 to 4.

data_x = linspace (1,100,10);data_y = [1 4 6 8 9 6 6 3 2 1]; plot(data_x, data_y, '-o');hold onp = polyfit(data_x, data_y, 1); plot(data_x, polyval(p,data_x), '--k');p = polyfit(data_x, data_y, 2); plot(data_x, polyval(p,data_x), '--r');p = polyfit(data_x, data_y, 3); plot(data_x, polyval(p,data_x), '--m');p = polyfit(data_x, data_y, 4); plot(data_x, polyval(p,data_x), '--c');legend ('Original data', '1st degree', '2nd degree', '3rd degree', '4th degree')hold off

Page 24: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

polyfit – Another example

Page 25: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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(read your book carefully)

Curve Fitting with Functions Other than Polynomials

Page 26: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Curve Fitting with Functions Other than Polynomials

Page 27: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 28: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

Page 29: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Remember the 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

Page 30: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Here is an example script trying the different axes scales to make the proper decision:

data_x = linspace (1,100,10);data_y = [2 8 12 18 24 38 56 100 200 400]; subplot (2,2,1);plot(data_x, data_y);title ('LINEAR');subplot (2,2,2);semilogx(data_x, data_y, 'k');title ('LOG IN X / LINEAR IN Y');subplot (2,2,3);semilogy(data_x, data_y, 'm');title ('LINEAR IN X / LOG IN Y');subplot (2,2,4);loglog(data_x, data_y, 'g');title ('LOGARITHMIC');

Curve Fitting with Functions Other than Polynomials

Page 31: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Curve Fitting with Functions Other than Polynomials

This one looks like the straighter curve, so an exponential function might be the best choice for this set of data.

Page 32: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Other considerations in choosing a function:

• Exponential functions Can't pass through origin

Can only fit data that is all positive or all negative

• Logarithmic functions can't model points with x ≤ 0

• Power function is 0 when x = 0

• Reciprocal function can't model y = 0

Page 33: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

INTERPOLATION

Interpolation is estimating values between data points. MATLAB can do interpolation with polynomials or the Fourier transform

Won't discuss Fourier-transform interpolation in this book

Page 34: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

INTERPOLATION

Page 35: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

INTERPOLATION

Page 36: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

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

INTERPOLATION

Page 37: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

MATLAB function interp1() does one-dimensional interpolation

"one"

Page 38: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

INTERPOLATION

Page 39: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

THE BASIC FITTING INTERFACEMATLAB has a tool that lets you perform interpolation interactively. With it, you can

• Curve fit with polynomials up to degree 10

• Compare fits with different polynomials by plotting on same graph

• Plot and compare residuals

• Calculate interpolated values

Page 40: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

To activate tool (in the plot window)

1. Plot data points

2. Select Tools|Basic Fitting

3. Click right-arrow button twice so that window looks like that in Fig. 8-3

THE BASIC FITTING INTERFACE

Page 41: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Figure 8-3: The Basic Fitting Window

Page 42: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Select data

If more than one data set plotted, lets you select which one to work on

Can only work on one data set at a time, but can perform multiple fits simultaneously on that data

• Center and scale x data

Data centered at zero mean and transformed so that standard deviation is one

• Check to display fits on figure

Select types of fits you want MATLAB to perform and display

Page 43: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

• Show equations

If checked, MATLAB displays equations of fits selected in box above field

• Plot residuals

Indicate whether or not to plot fit residuals and style of residual plot

• Show norm of residuals

Indicate whether or not to display norm of residuals. Norm is A measure of the quality of the fit

Smaller norm means a better fit

Page 44: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

• Fit

Select which fit to examine details of

• Coefficients and norm of residuals

Show numerical values of coefficients in fit equation and value of norm

• Find y = f(x)

Provide ability to examine fit values of manually-entered independent-variable values

Fig. 8-4 is example of a Figure Window modified by changes in Basic Fitting Interface

Page 45: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

Figure 8-4: A Figure Window modified by the Basic Fitting Interface

Page 46: Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering

END OF LESSON