Chapter 13: Polynomial Curve Fitting in MATLAB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
MATLAB has a variety of functions that are specific to polynomials MATLAB will integrate differentiate multiply divide and find roots of polynomials among other things in this video I'll give a brief overview of some of these commands and then talk about what's important to this chapter matlab's built-in function for curve fitting using polynomials polynomials are equations of this form the value of the polynomial is the sum of terms that individually are powers of the independent variable X multiplied by some numbers the powers are all non-negative integers so Y of X is equal to some coefficient a number times X to the n plus another coefficient times X to the n minus 1 and so on down to X to the 0 the degree of the polynomial is the highest power of X so this is an an order polynomial here are a few examples Y of X here is a second-order polynomial which is also called a quadratic function this polynomial is fourth order notice that it doesn't have terms corresponding to x squared or X to the zeroth power this means that the coefficients multiplying those particular powers of X are 0 this is a first-order polynomial which is just the equation for a straight line this one has a slope of 2 and a y-intercept of 1 to specify a polynomial in MATLAB just place the coefficients in a row vector with the coefficients listed in decreasing powers of X the last term will always be the X to the zeroth power term for example the coefficients of this polynomial in decreasing order are 7 negative 2 and 1 so my vector defining the polynomial is just 7 negative 2 & 1 the rightmost term will always be the coefficient of x to the 0th power this polynomial has coefficients that are 0 for the x squared and constant term since there's no x squared or constant term in the equation so the coefficients are 5 to 0 negative 1 and 0 the zeros are required as lease holders so that MATLAB can keep track of what power of X corresponds to the nonzero terms the equation for a straight line which is a first-order polynomial has only two terms the slope is first and the y-intercept is second now I'll give just a quick list of some MATLAB commands that use polynomials for the most part I won't tell you the syntax for these since you can use MATLAB documentation to figure out how to use them once you know the function names Paul eval evaluates a polynomial at some point x poly int integrates polynomials poly dur will take the derivative of a polynomial roots returns the roots of a polynomial Co NV is the product of two polynomials and D Co and V is the quotient of two polynomials finally poly fit performs least squares curve fitting of polynomials to data now let's talk about polynomial functions that are useful for curve fitting the poly Val function determines the value of a polynomial at a specific value of the independent variable the syntax is y is equal to poly Val of P comma X where P is a vector containing the polynomial coefficients and X is an array containing the values of the independent variable the function returns an array Y which corresponds to the values of the polynomial at the values given in the array X the poly fit function does a least squares curve fit of an arbitrary order polynomial to a set of XY data the syntax is poly fit of X comma y comma N the x and y vectors contain the x and y values of the data points and an is the order of the polynomial to be fit to the data a vector P is returned which contains the coefficients of the polynomial next I'll do an example of how to fit a straight line to some data and display the original data along with the curve fit these are my vectors of data points I can curve a straight line using the poly fit command on the X&Y data and specifying that I want a first-order polynomial this results in exactly the same process I did in a previous video by solving a system of over determined equations since I'll want to display the line approximating the data I'll evaluate some points on the line with the poly Val command so in this command I'm calculating points on my straight line using the polynomial coefficients from the previous command and calculating values at the same values of X as my original data finally I'll plot the data and the curve fit now I'll go to MATLAB and walk through this process after that I'll demonstrate what happens as we fit higher order polynomials to some data by changing this argument in the poly fit command I'll start out by implementing the code exactly as I outlined on the slide the data is defined and I use the poly fit command to create a least squares best fit straight line by specifying a first order polynomial then I create a line based on the curve fit and plot the line and the data points together looks like a reasonably good fit but let's try increasing the order of the polynomial used to fit the data when I use higher order polynomials I'm going to add a few values to the data set I'm using to fit the line here are my new x and y vectors they contain the same points as before but include a few extra ones too the line still looks like a pretty good approximation now I'll do the same thing but with a quadratic the last argument to the poly fit command is now a 2 and my polynomial coefficients are now returned to a vector named p2 I'm also going to set up a different vector of values where I'm calculating my curve fit to include a lot more values of X it didn't matter when I was using straight lines but since I'm now using a curved line I want to be able to see its shape between the data points better finally I calculate the values of Y at these X values and plot the data with the curve fit that looks like it might be a better fit to the data so it seems like higher order polynomials are better in that case let's go to the extreme this code does a 10th order curve fit it's the highest we can go with 11 data points this curve fit is great at the data points it passes through all the points and the curve fit matches the data exactly however I'm pretty sure that the process I'm measuring doesn't have these large Wiggles in whatever behavior displays so I would really not want to use this curve to predict what would actually be the value for y at an x value of for example 0.05 the problem actually gets worse if I try to predict what the data would look like outside the range of X values that I measured let's see what the kerf it looks like over a range of minus 0.02 to 1.0 - I'm only extending the range of the curve fit by 0.02 beyond the range of the data covers the kerf it just completely goes crazy outside the range specified by the data and I can't believe it at all so here are a couple practical points about curve fitting if you use a polynomial curve fit it's usually best to use the lowest order polynomial that you think adequately represents the data even better use some physical insight into the system itself to decide what kind of curve to fit to the data quite often the governing equations for the system indicate what type of curve you should use and then the curve fit can help you decide with the coefficients in the equation R now we can fit lines to data using MATLAB matrix operators applied to over determined equations and we can streamline that process by using mallet polyfit function but what if we need to fit a curve to data that's not a polynomial there are some tricks we can do with our existing tools to fit non polynomial functions to data those are the topic of the next video
Info
Channel: Hanshaw Virtual University
Views: 44,570
Rating: 4.9320884 out of 5
Keywords: Hanshaw, HVU, MATLAB, Polynomial, Polyfit, Curve Fitting, Linear Regression
Id: LP0YrsMGMAM
Channel Id: undefined
Length: 8min 48sec (528 seconds)
Published: Wed Nov 25 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.