1 / 54

Chapter 16

Chapter 16. Curve Fitting: Splines. Spline Interpolation. There are cases where polynomial interpolation is bad Noisy data Sharp Corners (slope discontinuity) Humped or Flat data Overshoot Oscillations. Example : f ( x ) = sqrt ( abs ( x )).

Download Presentation

Chapter 16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 16 Curve Fitting:Splines

  2. Spline Interpolation • There are cases where polynomial interpolation is bad • Noisy data • Sharp Corners(slope discontinuity) • Humped or Flat data • Overshoot • Oscillations

  3. Example: f(x) = sqrt(abs(x)) Interpolation at 4, 3, 2, 1, 0, 1, 2, 3, 4

  4. Spline Interpolation Cubic interpolation 7th-order 5th-order Linear spline

  5. Spline Interpolation Idea behind splines • Use lower order polynomials to connect subsets of data points • Make connections between adjacent splines smooth • Lower order polynomials avoid oscillations and overshoots

  6. Spline Interpolation • Use “piecewise” polynomials instead of a single polynomial • Spline -- a thin, flexible metal or wooden lath • Bent the lath around pegs at the required points • Spline curves -- curves of minimum strain energy • Piecewise Linear Interpolation • Piecewise Quadratic Interpolation • Piecewise Cubic Interpolation (cubic splines)

  7. Drafting Spline • Continuous function and derivatives Zero curvatures at end points

  8. Splines • There are n-1 intervals and n data points • si(x) is a piecewise low-order polynomial

  9. Spline fits of a set of 4 points

  10. Example: Ship Lines waterline

  11. Bow Stern

  12. Original database Spline interpolation for submerged hull (below waterline)

  13. Linear Splines • Connect each two points with straight line functions connecting each pair of points b is the slope between points

  14. Linear Splines si (x) = ai + bi (x xi) x1 x2 x3 x4

  15. Linear Splines Identical to Lagrange interpolating polynomials

  16. Linear splines • Connect each two points with straight line • Functions connecting each pair of points are • slope

  17. Linear splines are exactly the same as linear interpolation! Example:

  18. Linear Splines • Problem with linear splines -- not smooth at data points (or knots) • First derivative (slope) is not continuous • Use higher-order splines to get continuous derivatives • Equate derivatives at neighboring splines • Continuous functional values and derivatives

  19. Quadratic Splines Quadratic splines - continuous first derivatives May have discontinuous second and higher derivatives Derive second order polynomial between each pair of points For n points (i=1,…,n): (n-1) intervals & 3(n-1) unknown parameters (a’s, b’s, and c’s) Need 3(n-1) equations

  20. Quadratic Splines f(x5) s2 (x) f(x2) s4 (x) f(x3) s1 (x) f(x4) s3 (x) f(x1) Interval 1 Interval 2 Interval 3 Interval 4 x1 x2 x3 x4 x5 3(n-1)unknowns si(x) = ai + bi(xxi) + ci(xxi)2

  21. Piecewise Quadratic Splines • The functional must pass through all the points xi (continuity condition) • The function values of adjacent polynomials must be equal at all nodes (identical to condition 1.) 2(n-2) + 2 = 2(n-1) • The 1st derivatives are continuous at interior nodes xi, (n-2) • Assume that the second derivatives is zero at the first points Example with n = 5 3(n1) equations for 3(n1) unknowns 2(n1) + (n2) + (1) = 3(n1)

  22. Piecewise Quadratic Splines • Function must pass through all the points x = xi (2n  2 eqns) • This also ensure the same function values of adjacent polynomials at the interior knots (hi = xi+1 – xi) • Apply to every interval(4 intervals, 8 equations)

  23. Piecewise Quadratic Splines • Continuous slope at interior knots x = xi (n2 equations) • Apply to interior knots x2 ,x3 and x4 (3 equations) • Zero curvatures at x = x1 (1 equation)

  24. Example: Quadratic Spline

  25. Quadratic Spline Interpolation function [A, b] = quadratic(x, f) % exact solution f(x)=x^3-5x^2+3x+4 % x=[-1 0 2 5 6]; f=[-5 4 -2 19 58]; h1=x(2)-x(1); h2=x(3)-x(2); h3=x(4)-x(3); h4=x(5)-x(4); f1=f(1); f2=f(2); f3=f(3); f4=f(4); f5=f(5); A=[1 0 0 0 0 0 0 0 0 0 0 0 0 h1 h1^2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 h2 h2^2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 h3 h3^2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 h4 h4^2 0 1 2*h1 0 -1 0 0 0 0 0 0 0 0 0 0 0 1 2*h2 0 -1 0 0 0 0 0 0 0 0 0 0 0 1 2*h3 0 -1 0 0 0 1 0 0 0 0 0 0 0 0 0]; b=[f1; f2-f1; f2; f3-f2; f3; f4-f3; f4; f5-f4; 0; 0; 0; 0];

  26. Quadratic Spline Interpolation >> x=[-1 0 2 5 6]; f=[-5 4 -2 19 58]; >> [A,b]=quadratic(x,f) p = A\b -5.0000 9.0000 0 4.0000 9.0000 -6.0000 -2.0000 -15.0000 7.3333 19.0000 29.0000 10.0000 dx = 0.1; z1=x(1):dx:x(2); s1=p(1)+p(2)*(z1-x(1))+p(3)*(z1-x(1)).^2; z2=x(2):dx:x(3); s2=p(4)+p(5)*(z2-x(2))+p(6)*(z2-x(2)).^2; z3=x(3):dx:x(4); s3=p(7)+p(8)*(z3-x(3))+p(9)*(z3-x(3)).^2; z4=x(4):dx:x(5); s4=p(10)+p(11)*(z4-x(4))+p(12)*(z4-x(4)).^2; H=plot(z1,s1,'r',z2,s2,'b',z3,s3,'m',z4,s4,'g'); xx=-1:0.1:6; fexact=xx.^3-5*xx.^2+3*xx+4; hold on; G=plot(xx,fexact,'c',x,f,'ro'); set(H,'LineWidth',3); set(G,'LineWidth',3,'MarkerSize',8); H1=xlabel('x'); set(H1,'FontSize',15); H2=ylabel('f(x)'); set(H2,'FontSize',15); H3=title('f(x)=x^3-5x^2+3x+4'); set(H3,'FontSize',15);

  27. Quadratic Spline s4(x) Exact function s2(x) s1(x) s3(x)

  28. Cubic Splines Cubic splines avoid the straight line and the over-swing • Can develop method like we did for quadratic – 4(n–1) unknowns – 4(n–1) equations • interior knot equality • end point fixed • interior knot first derivative equality • assume derivative value if needed

  29. Piecewise Cubic Splines s2 (x) sn-1 (x) s1 (x) Continuous slopes and curvatures x1 x2 x3 xn-1 xn 4(n1)unknowns

  30. Piecewise Cubic Splines s2”(x) Sn-1”(x) s1”(x) s3”(x) x0 x1 x2 xn-1 xn si (x) - piecewise cubic polynomials si’(x) - piecewise quadratic polynomials (slope) si”(x) - piecewise linear polynomials (curvatures) Reduce to (n-1) unknowns and (n-1) equations for si’’

  31. Cubic Splines • Piecewise cubic polynomial with continuous derivatives up to order 2 • The function must pass through all the data points gives 2(n-1) equations i = 1,2,…, n-1

  32. Cubic Splines 2. First derivatives at the interior nodes must be equal: (n-2) equations 3. Second derivatives at the interior nodes must be equal: (n-2) equations

  33. Cubic Splines 4. Two additional conditions are needed (arbitrary) The last two equations Total equations: 2(n-1) + (n-2) + (n-2) +2 = 4(n-1)

  34. Cubic Splines • Solve for (ai, bi, ci, di) – see textbook for details • Tridiagonal system with boundary conditions c1 = cn= 0

  35. Cubic Splines Tridiagonal matrix

  36. Hand Calculations

  37. Hand Calculations • can be further simplified since c1 = c4 = 0 (natural spline)

  38. Cubic Spline Interpolation

  39. Cubic Splines • Piecewise cubic splines (cubic polynomials)

  40. Cubic Spline Interpolation • The exact solution is a cubic function • Why cubic spline interpolation does not give the exact solution for a cubic polynomial? • Because the conditions on the end knots are different! • In general, f (x0)  0 and f (xn)  0 !!

  41. Cubic Spline Interpolation

  42. >> xx=[-1 0 2 5 6]; f = [-5 4 -2 19 58]; >> Cubic_spline(xx,f); Resulting piecewise function: s1 = (-5.000000)+(11.081218)*(x-(-1.000000)) s2 = (0.000000)*(x-(-1.000000)).^2+(-2.081218)*(x-(-1.000000)).^3 Resulting piecewise function: s1 = (4.000000)+(4.837563)*(x-(0.000000)) s2 = (-6.243655)*(x-(0.000000)).^2+(1.162437)*(x-(0.000000)).^3 Resulting piecewise function: s1 = (-2.000000)+(-6.187817)*(x-(2.000000)) s2 = (0.730964)*(x-(2.000000)).^2+(1.221658)*(x-(2.000000)).^3 Resulting piecewise function: s1 = (19.000000)+(31.182741)*(x-(5.000000)) s2 = (11.725888)*(x-(5.000000)).^2+(-3.908629)*(x-(5.000000)).^3 (i = 1) (i = 2) (i = 3) (i = 4)

  43. Cubic Spline Interpolation Piecewise cubic Continuous slope Continuous curvature zero curvatures at end knots exact solution

  44. End Conditions of Splines • Natural spline : zero second derivative (curvature) at end points • Clamped spline: prescribed first derivative (clamped) at end points • Not-a-Knot spline: continuous third derivative at x2 and xn-1

  45. >> x = linspace(-1,1,9); y = 1./(1+25*x.^2);>> xx = linspace(-1,1,100); yy = spline(x,y,xx);>> yr=1./(1+25*xx.^2);>> H=plot(x,y,'o',xx,yy,xx,yr,'--'); Continuous third derivatives at x2 and xn-1 • Comparison of Runge’s function (dashed red line) with a 9-point not-a-knot spline fit generated with MATLAB (solid green line)

  46. Clamped End Spline - Use 11 values including slopes at end points >> yc = [1 y –4];% 1 and -4 are the 1st-order derivatives (or slopes)at 1st & last point, respectively.>> yyc = spline(x,yc,xx);>> >> H=plot(x,y,'o',xx,yyc,xx,yr,'--'); • Note that first derivatives of 1 and 4 are specified at the left and right boundaries, respectively.

  47. MATLAB Functions • One-dimensional interpolations • yy = spline (x, y, xx) • yi = interp1 (x, y, xi, ‘method’) • yi = interp1 (x, y, xi, ‘linear’) • yi = interp1 (x, y, xi, ‘spline’) – not-a-knot spline • yi = interp1 (x, y,, xi, ‘pchip’)– cubic Hermite • yi = interp1 (x, y,, xi, ‘cubic’)– cubic Hermite • yi = interp1 (x, y,, xi, ‘nearest’)– nearest neighbor

  48. MATLAB Function: interp1 • Piecewise polynomial interpolation on velocity time series for an automobile

  49. Spline Interpolations >> x=[1 2 4 7 9 10] x = 1 2 4 7 9 10 >> y=[3 -2 5 4 9 2] y = 3 -2 5 4 9 2 >> xi=1:0.1:10; >> y1=interp1(x,y,xi,'linear'); >> y2=interp1(x,y,xi,'spline'); >> y3=interp1(x,y,xi,'cubic'); >> plot(x,y,'ro',xi,y1,xi,y2,xi,y3,'LineWidth',2,'MarkerSize',12) >> print -djpeg spline00.jpg

More Related