Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering."— Presentation transcript:

1 Polynomials, Curve Fitting and Interpolation

2 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

3 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 Vector must include all polynomial coefficients, even those that are zero POLYNOMIALS

5 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

6 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 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 Roots of a Polynomial

8 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

9 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

10 Division: Divide two polynomials with the built-in function deconv, which has the form Addition, Multiplication, and Division of Polynomials

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

12 [

13 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.

14 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

15 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

16 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

17 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

18 EXAMPLE

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

20 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

21 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--')

22 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!

23 polyfit – Another example The 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 on p = 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

24 polyfit – Another example

25 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

26

27 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

28 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

29 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

30 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

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

32 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

33 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

34 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

35

36 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

37 MATLAB function interp1() does one-dimensional interpolation "one"

38 INTERPOLATION

39 THE BASIC FITTING INTERFACE MATLAB 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

40 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

41 Figure 8-3: The Basic Fitting Window

42 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

43 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

44 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

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

46 END OF LESSON


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

Similar presentations


Ads by Google