You are on page 1of 7

1

Curve Fitting II
The least squares curve fit, creates a curve such that the sum of the squared errors at the data points is minimized. It can be done with any set of basis functions. Common use to do it with a truncated power series a polynomial. The function in Matlab that does that is polyfit(). Example: Lets use the following data: x = [0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1]; y = [-.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2]; to use polyfit() we need to give it : - the data (vectors), and - the degree of the polynomial wanted for the degree = 1 - we get a regression line the best straight line approximation. For degree = 2 - we get a quadratic polynomial n = 2; p = polyfit(x,y,n); Lets check what coefficients we got for p : p = -9.8108 20.1293 -0.0317

2 Lets plot the original data set x,y and a new vector yi: the polynomial values for the same xs , and for the comparison lets also add a 10 degree polynom on the same plot: xi = linspace(0,1,100); yi = polyval(p,xi); pp = polyfit(x,y,10); y10 = polyval(pp,xi); % evaluate 10th order polynomial plot(x,y,'o',xi,yi,'--',xi,y10) % plot data xlabel('x'), ylabel('y=f(x)') title('2nd and 10th Order Curve Fitting') In the plot the original data is marked with o, the 2nd degree curve is dashed and the 10th poly fit is solid line. It takes - 2 points to define a line - 1st order poly - 3 points to define a 2nd order poly - n+1 points to define an nth order poly in our case (11 data points) we could choose 10th degree poly. High order polys have poor numerical properties not good to choose a higher than needed poly. big difference in order of magnitude between the coefficients

Cubic splines
To eliminate bad behaviour of high degrees polynomials we may use cubic splines. In cubic splines cubic polynomials are found to approx the curve between each pair of data points. - in splines language these points are called breakpoints. - because an infinite number of diff polynomials may be used to approx a curve between 2 points, additional restrictions are imposed in order to make the these cubic polynomials unique. - by constraining the 1st and the 2nd derivatives of each cubic polynomial to match at the breakpoints, all internal polynomials are well defined. - slope and curvature of the approx polynomials are continuous at the breakpoints. These additional conditions on the approximating polynomials require solving a large set of linear equations: Given n breakpoints there are n-1 cubic polynomials to be found, each having 4 unknown coefficients. The set of equations involves 4(n-1) unknowns By writing each cubic polynomial in a special form by applying the constrains The approx polynomials can be found by solving n equations for n unknows. The function spline takes data: - x - y - xi and produces the yi corresponding to the xi.

4 x = 0:12; y = tan(pi*x/25); xi = linspace(0,12); yi = spline(x,y,xi); plot(x,y,'o',xi,yi) title('Spline Fit') when the data is not so smooth we use another function: pchip(), for piecewise cubic hermite interpolating polynomials. x=[0 2 4 5 7.5 10]; y=exp(-x/6).*cos(x); xi=linspace(0,10); ysi=spline(x,y,xi); yhi=pchip(x,y,xi); plot(x,y,'o',xi,ysi,':',xi,yhi) legend('data','spline','hermite') title('Spline and Hermite Interpolation') Spline interpolation on a plane Spline interpolation as done by the function spline assumes that the independent variable is monotonic : the spline function y=s(x) describes a continuous function. When this function is not continuous , there is no one-to-one relation between x, y. Example: t = linspace(0,3*pi,15); x = sqrt(t).*cos(t); y = sqrt(t).*sin(t); plot(x,y) xlabel('X') ylabel('Y') title('Spiral Y=f(X)')

5 It is not possible to compute a spline for the spiral as a function of x, because there may be multiple values of y for each x. Well use spline for x(t) and then for y(t). t = linspace(0,3*pi,15); x = sqrt(t).*cos(t); y = sqrt(t).*sin(t); ti = linspace(0,3*pi); % total range, 100 points xy = spline(t,[x;y],ti); plot(x,y,'d',xy(1,:),xy(2,:)) xlabel('X') ylabel('Y') title('Interpolated Spiral Y=f(X)') Lets try to do the same in 3D t x y z = = = = linspace(0,3*pi,15); sqrt(t).*cos(t); sqrt(t).*sin(t); sqrt(t).*tan(2*t);

ti = linspace(0,3*pi); % total range, 100 points xy = spline(t,[x;y;z],ti); plot3(x,y,z,'o',xy(1,:),xy(2,:),xy(3,:)) xlabel('X') ylabel('Y') zlabel('Z') title('Interpolated 3D noisy Spiral') Exercises - create 4 different complex 2D planar figures non functions, and approximate them with cubic splines. - create 4 different complex 3D figures non functions, and approximate them with cubic splines.

6 MESHGRID X and Y arrays for 3-D plots. [X,Y] = MESHGRID(x,y) transforms the domain specified by vectors x and y into arrays X and Y that can be used for the evaluation of functions of two variables and 3-D surface plots. The rows of the output array X are copies of the vector x and the columns of the output array Y are copies of the vector y. [X,Y,Z] = MESHGRID(x,y,z) produces 3-D arrays that can be used to evaluate functions of three variables and 3-D volumetric plots. For example, to evaluate the function x*exp(-x^2-y^2) over the range -2 < x < 2, -2 < y < 2, Mesh see help [X,Y] = meshgrid(-2:.2:2, -2:.2:2); Z = X .* exp(-X.^2 - Y.^2); mesh(Z) GRIDDATA Data gridding and surface fitting. ZI = GRIDDATA(X,Y,Z,XI,YI) fits a surface of the form Z = F(X,Y) to the data in the (usually) nonuniformly-spaced vectors (X,Y,Z). GRIDDATA interpolates this surface at the points specified by (XI,YI) to produce ZI. The surface always goes through the data points. XI and YI are usually a uniform grid (as produced by MESHGRID) and is where GRIDDATA gets its name. XI can be a row vector, in which case it specifies a matrix with constant columns.

7 Similarly, YI can be a column vector and it specifies a matrix with constant rows. [XI,YI,ZI] = GRIDDATA(X,Y,Z,XI,YI) also returns the XI and YI formed this way (the results of [XI,YI] = MESHGRID(XI,YI)). [...] = GRIDDATA(X,Y,Z,XI,YI,METHOD) where METHOD is one of : 'linear' - Triangle-based linear interpolation (default) 'cubic' - Triangle-based cubic interpolation 'nearest' - Nearest neighbor interpolation 'v4' - MATLAB 4 griddata method defines the type of surface fit to the data. The 'cubic' and 'v4' methods produce smooth surfaces while 'linear' and 'nearest' have discontinuities in the first and zero-th derivative respectively. All the methods except 'v4' are based on a Delaunay triangulation of the data. If METHOD is [], then the default 'linear' method will be used. x=rand(100,1)*4-2; y=rand(100,1)*4-2; z=x.*exp(-x.^2-y.^2); hi = -2:.1:2; [XI,YI]=meshgrid(hi); ZI=griddata(x,y,z,XI,YI,cubic); mesh(XI,YI,ZI),hold plot3(x,y,z,'o'),hold off

try different approaches: linear, nearest,cubic, v4 Exercises - create 4 different complex 3D surface figures , and approximate interpolate them.

You might also like