recap summary of chapter 6 interpolation linear interpolation

40
Lecture 29

Upload: patience-flowers

Post on 13-Jan-2016

266 views

Category:

Documents


0 download

TRANSCRIPT

Lecture 18

Lecture 29Recap Summary of Chapter 6 InterpolationLinear Interpolation

Cubic Spline Interpolation Connecting data points with straight lines probably isnt the best way to estimate intermediate values, although it is surely the simplestA smoother curve can be created by using the cubic spline interpolation technique, included in the interp1 function. This approach uses a third-order polynomial to model the behavior of the dataTo call the cubic spline, we need to add a fourth field to interp1 :interp1(x,y,3.5,'spline')This command returns an improved estimate of y at x = 3.5:ans = 3.9417The cubic spline technique can be used to create an array of new estimates for y for every member of an array of x -values:new_x = 0:0.2:5;new_y_spline = interp1(x,y,new_x,'spline');A plot of these data on the same graph as the measured data using the commandplot(x,y,new_x,new_y_spline,'-o') results in two different lines

Multidimensional Interpolation Suppose there is a set of data z that depends on two variables, x and y . For example

Continued.In order to determine the value of z at y = 3 and x = 1.5, two interpolations have to performedOne approach would be to find the values of z at y = 3 and all the given x -values by using interp1 and then do a second interpolation in new chartFirst lets define x , y , and z in MATLAB :y = 2:2:6;x = 1:4;z = [ 7 15 22 3054 109 164 218403 807 1210 1614];Now use interp1 to find the values of z at y = 3 for all the x -values:new_z = interp1(y,z,3) returnsnew_z =30.50 62.00 93.00 124.00Finally, since we have z -values at y = 3, we can use interp1 again to find z at y = 3 and x = 1.5:new_z2 = interp1(x,new_z,1.5)new_z2 =46.25Continued.Although the previous approach works, performing the calculations in two steps is awkwardMATLAB includes a two-dimensional linear interpolation function, interp2 , that can solve the problem in a single step:interp2(x,y,z,1.5,3)ans =46.2500The first field in the interp2 function must be a vector defining the value associated with each column (in this case, x ), and the second field must be a vector defining the values associated with each row (in this case, y )The array z must have the same number of columns as the number of elements in x and must have the same number of rows as the number of elements in y The fourth and fifth fields correspond to the values of x and of y for which you would like to determine new z -valuesContinued.MATLAB also includes a function, interp3 , for three-dimensional interpolationConsult the help feature for the details on how to use this function and interpn , which allows you to perform n -dimensional interpolationAll these functions default to the linear interpolation technique but will accept any of the other techniquesCurve FittingAlthough interpolation techniques can be used to find values of y between measured x -values, it would be more convenient if we could model experimental data as y=f(x)Then we could just calculate any value of y we wantedIf we know something about the underlying relationship between x and y , we may be able to determine an equation on the basis of those principlesMATLAB has built-in curve-fitting functions that allow us to model data empiricallyThese models are good only in the region where weve collected dataIf we dont understand why a parameter such as y changes as it does with x , we cant predict whether our data-fitting equation will still work outside the range where weve collected dataLinear Regression The simplest way to model a set of data is as a straight lineSuppose a data setx = 0:5;y = [15, 10, 9, 6, 2, 0];If we plot the data, we can try to draw a straight line through the data points to get a rough model of the datas behaviorThis process is sometimes called eyeballing itmeaning that no calculations were done, but it looks like a good fit

Continued.Looking at the plot, there are several of the points appear to fall exactly on the line, but others are off by varying amountsIn order to compare the quality of the fit of this line to other possible estimates, we find the difference between the actual y -value and the value calculated from the estimate. This difference is called the residual

Continued.

Continued.The linear regression technique uses an approach called least squares fit to compare how well different equations model the behavior of the dataIn this technique, the differences between the actual and calculated values are squared and added together. This has the advantage that positive and negative deviations dont cancel each other outMATLAB could be used to calculate this parameter for data. We havesum_of_the_squares = sum((y-y_calc).^2)which gives ussum_of_the_squares =5Continued.Linear regression is accomplished in MATLAB with the polyfit functionThree fields are required by polyfit : a vector of x valuesa vector of y valuesan integer indicating what order polynomial should be used to fit the dataSince a straight line is a first-order polynomial, enter the number 1 into the polyfit function:polyfit(x,y,1)ans = -2.9143 14.2857The results are the coefficients corresponding to the best-fit first-order polynomial equation:y = -2.9143x + 14.2857Calculate the sum of the squares to find out that if this is a better fit than eyeballed model or not:best_y = -2.9143*x+14.2857;new_sum = sum((y-best_y).^2)new_sum = 3.3714Since the result of the sum-of-the-squares calculation is indeed less than the value found for the eyeballed line, we can conclude that MATLAB found a better fit to the dataWe can plot the data and the best-fit line determined by linear regression to try to get a visual sense of whether the line fits the data well:plot(x,y,'o',x,best_y)

Polynomial Regression Continued.We can find the sum of the squares to determine whether these models fit the data better:y2 = 0.0536*x.^2-3.182*x + 14.4643;sum((y2-y).^2)ans = 3.2643y3 = -0.0648*x.^3+0.5398*x.^2-4.0701*x + 14.6587sum((y3-y).^2)ans = 2.9921The more terms we add to our equation, the better is the fit, at least in the sense that the distance between the measured and predicted data points decreasesContinued.In order to plot the curves defined by these new equations, more than the six data points are used in the linear modelMATLAB creates plots by connecting calculated points with straight lines, so if a smooth curve is needed, more points are required We can get more points and plot the curves with the following code:smooth_x = 0:0.2:5;smooth_y2 = 0.0536*smooth_x.^2-3.182*smooth_x + 14.4643;subplot(1,2,1)plot(x,y,'o',smooth_x,smooth_y2)smooth_y3 = -0.0648*smooth_x.^3+0.5398*smooth_x.^2-4.0701*smooth_x + 14.6587;subplot(1,2,2)plot(x,y,'o',smooth_x,smooth_y3)

The Polyval Function The polyfit function returns the coefficients of a polynomial that best fits the data, at least on the basis of a regression criterionIn the previous section, we entered those coefficients into a MATLAB expression for the corresponding polynomial and used it to calculate new values of y The polyval function can perform the same job without having to reenter the coefficientsThe polyval function requires two inputsThe first is a coefficient array, such as that created by polyfit The second is an array of x -values for which we would like to calculate new y valuesFor example:coef = polyfit(x,y,1)y_first_order_fit = polyval(coef,x)These two lines of code could be shortened to one line by nesting functions:y_first_order_fit = polyval(polyfit(x,y,1),x)Continued.So according to new understanding of the polyfit and polyval functions to write a program to calculate and plot the fourth- and fifth-order fits for the datay4 = polyval(polyfit(x,y,4),smooth_x);y5 = polyval(polyfit(x,y,5),smooth_x);subplot(1,2,1)plot(x,y,'o',smooth_x,y4)axis([0,6,-5,15])subplot(1,2,2)plot(x,y,'o',smooth_x,y5)axis([0,6,-5,15])

The Interactive Fitting Tools MATLAB includes new interactive plotting tools that allow to annotate plots without using the comman windowMATLAB also included Basic curve fittingMore complicated curve fittingStatistical toolsBasic Curve FittingTo access the basic fitting tools, first create a figure:x = 0:5;y = [0,20,60,68,77,110]plot(x,y,'o')axis([-1,7,-20,120])These commands produce a graph with some sample data

Continued.To activate the curve-fitting tools, select Tools -> Basic Fitting from the menu bar in the figureThe basic fitting window opens on top of the plotBy checking linear , cubic , and show equation, plot is generated

Continued.Checking the plot residuals box generates a second plot, showing how far each data point is from the calculated line

Continued.In the lower right-hand corner of the basic fitting window is an arrow button.Selecting that button twice opens the rest of the window

Continued.The center panel of the window shows the results of the curve fit and offers the option of saving those results into the workspaceThe right-hand panel allows to select x -values and calculate y -values based on the equation displayed in the center panel

Continued.In addition to the basic fitting window, the data statistics window can be accessed from the figure menu barSelect Tools -> Data Statistics from the figure windowThe data statistics window allows to calculate statistical functions such as the mean and standard deviation interactively, based on the data in the figure, and allows to save the results to the workspace

Curve-Fitting ToolboxIn addition to the basic fitting utility, MATLAB contains toolboxes to help to perform specialized statistical and data-fitting operationsIn particular, the curve-fitting toolbox contains a graphical user interface (GUI) that allows to fit curves with more than just polynomialsThe curve-fitting toolbox must be installed in copy of MATLAB before execution the examples that followExample The data weve x = 0:5;y = [0,20,60,68,77,110];To open the curve-fitting toolbox, typecftoolThis launches the curve-fitting tool windowNow tell the curve-fitting tool what data to useSelect the data button, which will open a data windowThe data window has access to the workspace and will let you select an independent (x) and dependent (y) variable from a drop-down list

Example Continued.Choose x and y, respectively, from the drop-down listsA data-set name can be assigned, or MATLAB will assign one automaticallyOnce chosen variables have been chosen, MATLAB plots the dataAt this point, the data window can be closedGoing back to the curve-fitting tool window, now select the Fitting button that offers you choices of fitting algorithmsSelect New fit , and select a fit type from the Type of fit listChose an interpolated scheme that forces the plot through all the points, and a third-order polynomial

Numerical Integration Continued.

Continued.MATLAB includes two built-in functions, quad and quadl , which will calculate the integral of a function without requiring the user to specify how the rectangles (shown in previous slide) are definedThe two functions differ in the numerical technique usedFunctions with singularities may be solved with one approach or the other, depending on the situationThe quad function uses adaptive Simpson quadrature:quad('x.^2',0,1)ans = 0.3333The quadl function uses adaptive Lobatto quadrature:quadl('x.^2',0,1)ans =0.3333Continued.