Applied Numerical Analysis Interpolation Lecturer: Emad Fatemizadeh
Interpolation Input: A set (x i -yy i ) data. Output: Function value at arbitrary point x. 0 1 4 1-3 3 9 What for x = 1.?
Interpolation Piecewise-Linear ea Interpolation: o Draw a line from (x k,y k ) to (x k+1,y k+1 ) (x k+1,y k+1 ) (x k,y k ) k k 1 x xk f x = y + y + 1 y x x ( ) ( ) k k k k k+ 1 k x x x + Result is continuous but NOT Differentiable!
Piecewise-Linear Interpolation
Interpolation Polynomial Interpolation: Why Polynomial: Taylor Series of a f(x) near x 0: ( ) k = 0 n! ( k ) ( x0 ) ( ) N f f x x x Computation is to simple Practical Application: Complex function in engineering problems! 0 k
Interpolation Polynomial Interpolation Simple approach: We have ( x, y ) We have { } n i i i= 0 n n k i i k i k = 0 k = 0 k k ( ) = = ( ) =, = 0,1,, f x a x y f x a x i n 1 n n y 0 = a0 + ax 1 0 + + anx 0 1 x a 0 x 0 0 y 0 1 n n y 1 = a0 + ax 1 1 + + anx1 1 x a 1 x 1 1 y 1 = 1 n n y 1 n = a0 + ax 1 n + + a x a nx n n x n n y n a= 1 X y
Interpolation Example: Data: {( 0,6 ),( 1,0 ),(,) } 1 0 0 a 6 1 4 a 0 1 1 1 a1 = 0 f ( x) = 4x 10x+ 6
Polynomial Interpolation
Interpolation Problems of the this formulation: Matrix Inversion is time consuming. Matrix Inversion is erroneous.
Interpolation Lagrange Method: n f ( x ) = Lk ( x ) y k k = 0 f ( x i) = y i 1 x = x i Li ( x ) = 0 x x i x x 0 x x k 1 x x k + 1 x x n Lk ( x ) = x x x x x x x x ( ) ( )( ) ( ) ( k 0 ) ( k k 1 )( k k + 1 ) ( k n ) ( ) = 1, ( ) = 0, L x L x x x k k k i i k
Interpolation Example: {( ) ( ) ( )} ( x 1)( x ) x 3x + ( x) = = ( 0 1)( 0 ) ( x 0)( x ) x x ( x) = = ( 1 0)( 1 ) 1 ( x 0)( x 1) x x ( x) = = ( 0)( 1) ( ) ( ) ( ) ( ) Data: 0,6, 1,0,, L L L 0 1 f x = 6L x + 0L x + L x = 4x 10x+ 6 0 1
Interpolation Error: ( ) ( x x )( x x ) ( x xn ) ( N + 1! ) ( ) 0 1 ( N + 1) EN x = f c x0 c xn,
Interpolation Newton Method: x 0 1-1 - y -5-3 -15 39-9 ( ) = = ( ) ( ) ( ) ( ) P1( x1) = y1 ( ) ( ) ( ) ( )( ) P( x) = y ( ) ( ) ( ) ( )( )( ) P3( x3) = y3 ( ) P x y 0 0 5 P x = P x + c x x = 5+ c x 0 P x = 5+ x 1 0 0 1 P x = P x + c x x x x P x = 5+ x 4 x( x 1) 1 0 1 P x = P x + c x x x x x x P x = 5+ x 4 x ( x 1) + 3 0 1 3 ( ) ( ) ( )( )( )( ) P x = y 4 3 0 1 3 4 4 4 4 P x P x c x x x x x x x x P x ( ) 8x( x 1)( x+ 1) ( ) 5 x x x + ( )( + ) + ( )( + )( ) = + = + 4 ( 1) 8x x 1 x 1 3x x 1 x 1 x
Matlab Commands polyfit: Find polynomial p = polyfit(x,y,n);
Interpolation Radial Basis Function: Symmetric Function (1D) ( ) Consider, then f or radial basis. D example: f x ( ) exp f x f ( x ) ( x y ) = exp ( r )
Interpolation If φ(x) be a radial basis function: ( x y ) Data:, n { } i i i = 1 n = k k = 1 ( ) ϕ ( ) f x w x x n ( ) ϕ ( ) i i k i k k = 1 k f x = y = w x x, i = 1,,, n ( ) ϕ = ϕ x x y = wϕ ik, i k i k ik, k = 1 n
Interpolation y1 ϕ1,1 ϕ1, ϕ1, n w1 y ϕ,1 ϕ, ϕ, n w =, ϕ ik, = ϕ ki,, ϕ ii, = ϕ 0 y ϕ n n,1 ϕn, ϕn, n wn 1 w1 ϕ1,1 ϕ1, ϕ1, n y1 w ϕ,1 ϕ, ϕ, n y = w ϕ n n,1 ϕn, ϕn, n yn ( )
Interpolation Sample of RBF function: r = x x exp r r r r r 3 i ( r ) log j ( r ) 1 + c + c
{ x } { y } i Numerical Example = [ 101], = [ 15] i ( ) ( ) ( ) ( ) 1exp 1 exp 3exp 3 y = f x = w x x + w x x + w x x ( ( ) ) ( ) ( ) ( ) y = w exp x + 1 + w exp x + w exp x 1 1 3 ( ) ( ) ( ) y = f x = w e + w e + w e = 1 1 e e 1 y f x w e w e w e e = y f x w e w e w 5 0 1 4 1 4 1 1 1 3 w 1 1 0 1 1 1 = = 1 + + 3e = 1 w 4 1 0 4 1 1 3 = 3 = 1 + + 3e = 5 e e w 3 w 1-1.3650 w = 0.7559 w 3 4.7469 ( ) ( ) ( ) ( ) ( ( ) ) y = f x = -1.3650exp x + 1 + 0.7559exp x + 4.7469exp x 1
Numerical Example
Matlab Coding Hints What we need: φ = ϕ ( x ) i x j What we need: First we calculate: r = x x First Method: a for loop. x = [-1 0 1]'; y = [-1 5]'; r = zeros(length(x)); for i=1:length(x), for j=1:length(x), end; end r(i,j)=abs(x(i)-x(j)); x(j)); i j
Matlab Coding Hints Another Methods: Note that: 1 1 1 1 0 * [ 1 1 1 ] 0 0 0 = 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 = 1 0 1 1 1 1 1 1 1 0 0 1 Tabe absolute value: 1 0 1 0 T
Matlab Coding Hints Matlab Code: Other parts: x = [-1 0 1]'; y = [-1 5]'; Tmp = x*ones(1,3); r = abs(tmp-tmp'); Phi = exp(-r.^); w = inv(phi)*y;
Spline Consider two discussed approach: A single polynomial to N points. (N-1) line between two successive point. Now we could do a combination!
Linear Splines The simplest connection between two points is a straight line: f ( x ) = f ( x0 ) + m0 ( x x0 ), x0 x x1 f ( x) = f ( x ) + m ( x x ), x x x 1 1 1 1 ( ) ( ) ( ) f ( x ) f ( x ) f x = f x + m x x, x x x m i = i+ 1 x i+ 1 n 1 n 1 n 1 n 1 n x i i
Linear Spline Continuous at knots Not differentiable at knots. Knots
Quadratic Splines Goal: Derive a nd order polynomial for each interval between data points ( ) f x = a x + bx+ c i i i i x i-1 x i
Quadratic Splines For (n+1) points (0,1,,,n), we have n interval, 3n unknown parameters (a i, b i, and c i ). Thus we need 3n conditions
Quadratic Splines The function value of adjacent polynomials must be equal at interior knots. (i=1, i=n-1); ai 1xi 1+ bi 1xi 1+ ci 1 = f ( xi 1), fi 1 at upper bound ax + bx + c = f ( x ), f at lower bound i i 1 i i 1 i i 1 i i=,3,,n (n-1) conditions (we need 3n)
Quadratic Splines The function value of first and last polynomials must pass through the end points. (i=0, i=n); ax + bx + c = f x ( ) ( ) 1 0 1 0 1 0 ax + bx + c = f x n n n n n n conditions (n-1)+=n conditions (we need 3n)
Quadratic Splines The first derivatives at interior knots must be equal (i=0, i=n); a x + b = a x + b i 1 i 1 i 1 i i 1 i i =,3,, n n-1 conditions n+n-1=3n-11 conditions (we need 3n)
Quadratic Splines An arbitrary condition: nd derivative at first point is zero.! 1 conditions a = 1 0 3n-1+1=3n conditions (we need 3n)
Example x=[3, 45 4.5, 7, 9] and y=[.5, y=[5 1,.5, 5 0.5] 05] n=3; x a1 b1 c1 = 4.5: 0.5 + 4.5 + = 1 x = 45: 4.5: 05 0.5 a + 4.5 45 b + c = 1 x= 7: 49a + 7b + c =.5 Function value x = 7: 49 a 3 + 7 b 3 + c 3 =.5 x= 3: 9a1+ 3b1+ c1 =.5, First point x = 9: 81a + 9b + c = 1 1 1 0.5, Last point x= 4.5: 9a1+ b1 = 9a + b Function Deriv. x = 7 : 17a + b = 14a + b a 1 = 0 3 3
Example x + 55 5.5 3 x 4.5 45 f ( x) = 0.64x 6.76x+ 18.46 4.5 x 7 16 1.6 x + 4.6 46 x + 91.3 913 7 x 9
Cubic Splines Goal: Derive a 3 rd order polynomial for each interval between data points ( ) 3 f x = a x + bx + cx+ d i i i i i x i-1 x i
Cubic Splines Conditions: o The function value must be equal at the interior knots (i=,n-1) 1)-> n- The first and last function must pass through the end points (i=0,n)-> The first derivatives at the interior knots must be equal (i=,n-1) -> n-1 The second derivatives at the interior knots must be equal (i=,n-1) -> n-1 The second derivatives at the end knots are zeros (i=0,n) -> 4n condition and 4n parameters!
Cubic Splines Special case: x i+1 -x i =h, i=n-1 1,,0 x x x i 1 i ( x x ) i ( x xi 1)( x xi) 3 fi( x) = + f h h i 1 ( x x ) ( x x ) ( x x ) + h i 1 i 1 i 3 ( x x )( x x ) ( x x ) ( x x ) + S + h h i 1 i i 1 i i 1 h f i S i
Cubic Splines Condition: fi( xi) = yi ( ) i ( i ) = i + ( i ) ( ) i( i) = i+ ( i) fi( xi) = fi+ 1 ( xi) ( ) 1 1( 0) = 0 fn( xn) yn ( ) 1 1( 0) = 0 f ( x ) = 0 f 1 x x 0 x x 1 f x f 1 x f x x1 x x f x f 1 x fi x xi x xi f x y = fn x xn x xn f x n n
Cubic Splines 3 S i + 4 S i + 1 + S i + = ( y i + y i ) i = 0,1,, n h Si = 0 i = 0, n
Example x=[3, 3.5, 4] y=[1.098, 1.53, 1.386] S 0 = S = 0 3 4S1 = ( 1.386 1.098) S1 = 0.43 0.5
Matlab Command Spline Ynew = spline(xold,yold,xnew) Xnew) x=[0 1 3 4 5 6 7 8]; y=[6 0-1 3 4-5 4]; xx=0:.01:8; yy=spline(x,y,xx);
Example
Two Dimensional Interpolation We have z=f(x,y) at limited (x i,y i ): Goal: Find f(x,y) at arbitrary (x,y) among (x i,y i ).
Lagrange Method Lagrange Method M N f ( x, y ) = Lij ( x, y ) z ij i= 1 j= 1 f ( x, y ) = z 1 x = x i and y = y i Lij ( x, y ) = 0 O.W. L x, y = L x L y L i i ij ( ) ( ) ( ) ( x x 0 ) ( x x k 1 )( x x k + 1 ) ( x x n ) ( x ) = ( x k x 0) ( x k x k 1)( x k x k + 1) ( x k x n) ( ) = 1, ( ) = 0, ij i j k L x L x x x k k k i i k
Bilinear A quadratic D spline Data is regular. (, ) = (, ), f x y z i i i j f x y = z i+ 1 i i+ 1, j (, ) f x y + = z + i j+ 1 i, j+ 1 (, ) f x y = z i+ 1 j+ 1 i+ 1, j+ 1
Bilinear Interpolation Formulation: (, ) = + + +, i i+ 1, i i+ 1 = (, ) = + + + f x y axy bx cy d x x x y y y z f x y ax y bx cy d ij i i i i i i
RBF function Formulation is very similar f = f ( x, x,, x ), i = 1,,, N i 1 L L = 1 L i = = p p p= 1 T ( x, x,, x ) f f ( ), ( x y ) x x x y N k x xk k = 1 N ( ) = ( ) f x wϕ ( ) ( ) i i kϕ k f x = f = w x x, i = 1,,, N k = 1 ( x x ) ϕ, = ϕ y = wϕ, ik k i k ik k = 1 N