Presentation is loading. Please wait.

Presentation is loading. Please wait.

Curve Fitting: Splines

Similar presentations


Presentation on theme: "Curve Fitting: Splines"— Presentation transcript:

1 Curve Fitting: Splines
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 Zero curvatures at end points
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

11 Example: Ship Lines waterline

12 Bow Stern

13 Spline interpolation for submerged hull (below waterline)
Original database Spline interpolation for submerged hull (below waterline)

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

15 Linear Splines si (x) = ai + bi (x  xi) x x x x4

16 Identical to Lagrange interpolating polynomials
Linear Splines Identical to Lagrange interpolating polynomials

17 Linear splines Connect each two points with straight line
Functions connecting each pair of points are slope

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

19 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

20 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

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

22 Piecewise Quadratic Splines
Example with n = 5 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 3(n1) equations for 3(n1) unknowns 2(n1) + (n2) + (1) = 3(n1)

23 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)

24 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)

25 Example: Quadratic Spline

26 Quadratic Spline Interpolation
function [A, b] = quadratic(x, f) % exact solution f(x)=x^3-5x^2+3x+4 % x=[ ]; f=[ ]; 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=[ 0 h1 h1^ h2 h2^ h3 h3^ h4 h4^2 *h *h *h ]; b=[f1; f2-f1; f2; f3-f2; f3; f4-f3; f4; f5-f4; 0; 0; 0; 0];

27 Quadratic Spline Interpolation
>> x=[ ]; f=[ ]; >> [A,b]=quadratic(x,f) p = A\b 9.0000 4.0000 7.3333 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);

28 Quadratic Spline s4(x) Exact function s2(x) s1(x) s3(x)

29 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

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

31 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’’

32 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

33 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:

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

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

36 Cubic Splines Tridiagonal matrix

37 Hand Calculations

38 Hand Calculations can be further simplified since c1 = c4 = 0 (natural spline)

39 Cubic Spline Interpolation

40 Cubic Splines Piecewise cubic splines (cubic polynomials)

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

42 Cubic Spline Interpolation

43 >> xx=[-1 0 2 5 6]; f = [-5 4 -2 19 58];
>> Cubic_spline(xx,f); Resulting piecewise function: s1 = ( )+( )*(x-( )) s2 = ( )*(x-( )).^2+( )*(x-( )).^3 ( )+( )*(x-( )) ( )*(x-( )).^2+( )*(x-( )).^3 ( )+( )*(x-( )) ( )*(x-( )).^2+( )*(x-( )).^3 ( )+( )*(x-( )) ( )*(x-( )).^2+( )*(x-( )).^3 (i = 1) (i = 2) (i = 3) (i = 4)

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

45 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

46 Continuous third derivatives at x2 and xn-1
>> 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)

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

48 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

49 MATLAB Function: interp1
Piecewise polynomial interpolation on velocity time series for an automobile

50 Spline Interpolations
>> x=[ ] x = >> y=[ ] y = >> 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

51 Spline Interpolations
Linear Not-a-knot Cubic

52 MATLAB’s Functions Two-dimensional interpolations
zi = interp2 (x, y, z, xi, yi)

53 MATLAB Function: interp2
>> [x,y,z]=peaks(100); [xi,yi]=meshgrid(-3:0.1:3,-3:0.1:3); >> zi = interp2(x,y,z,xi,yi); surf(xi,yi,zi) >> print -djpeg075 peaks1.jpg >> [x,y,z]=peaks(10); [xi,yi]=meshgrid(-3:0.1:3,-3:0.1:3); >> print -djpeg075 peaks2.jpg 10  10 data base 100 100 data base

54 CVEN 302-501 Homework No. 11 Chapter 16 Chapter 19
Problem 16.1(a) (30) – Hand Calculation Problem 16.1(b) (20)– MATLAB program not-a-knot spline: yy = spline(x, y, xx) Problem 16.1(c)(20) – MATLAB program Chapter 19 Problem 19.2 (20)– Hand Calculation Problem 19.4 (20)– Hand Calculation Due on Wed. 11/05/08 at the beginning of the period


Download ppt "Curve Fitting: Splines"

Similar presentations


Ads by Google