Lecture Notes - Interpolation
Lecture Notes - Interpolation
Interpolation
Linear Interpolation
Recall: The general formula for an nth-order polynomial is:
𝒇(𝒙) = 𝒂𝒐 + 𝒂𝟏 𝒙 + 𝒂𝟐 𝒙𝟐 + ⋯ + 𝒂𝒏 𝒙𝒏 (𝟏)
Linear interpolation polynomial od degree 1:
𝒇(𝒙) = 𝒂𝒐 + 𝒂𝟏 𝒙 (𝟐)
𝒇(𝒙𝟏 ) − 𝒇(𝒙𝒐 )
𝒇(𝒙) = 𝒇(𝒙𝒐 ) + (𝒙 − 𝒙𝒐 ) (6)
𝒙𝟏 − 𝒙𝒐
College of Engineering, Department of Mechanical Engineering
Lecture Notes 0402307 Numerical Analysis Khalid Ramadan
Quadratic Interpolation
𝒇(𝒙) = 𝒃𝒐 + 𝒃𝟏 (𝒙 − 𝒙𝒐 ) + 𝒃𝟐 (𝒙 − 𝒙𝒐 )(𝒙 − 𝒙𝟏 )
→ 𝒇(𝒙𝒐 ) = 𝒃𝒐
𝒇(𝒙𝟏 ) = 𝒇(𝒙𝒐 ) + 𝒃𝟏 (𝒙𝟏 − 𝒙𝒐 )
𝒇(𝒙𝟏 ) − 𝒇(𝒙𝒐 )
→ 𝒃𝟏 =
𝒙𝟏 − 𝒙𝒐
𝒇(𝒙𝟏 ) − 𝒇(𝒙𝒐 )
𝒇(𝒙𝟐 ) = 𝒇(𝒙𝒐 ) + (𝒙𝟐 − 𝒙𝒐 ) + 𝒃𝟐 (𝒙𝟐 − 𝒙𝒐 )(𝒙𝟐 − 𝒙𝟏 )
𝒙𝟏 − 𝒙𝒐
Solve for 𝒃𝟐 :
𝒇(𝒙𝟐 ) − 𝒇(𝒙𝟏 ) 𝒇(𝒙𝟏 ) − 𝒇(𝒙𝒐 )
−
𝒙 𝟐 − 𝒙𝟏 𝒙 𝟏 − 𝒙𝒐
𝒃𝟐 =
𝒙𝟐 − 𝒙𝒐
In general:
𝒇(𝒙𝒊 ) − 𝒇(𝒙𝒋 )
𝟏𝒔𝒕 𝒅𝒊𝒗𝒊𝒅𝒆𝒅 𝒅𝒊𝒇𝒇𝒆𝒓𝒆𝒏𝒄𝒆: 𝒇[𝒙𝒊 , 𝒙𝒋 ] =
𝒙𝒊 − 𝒙𝒋
Example 1:
Estimate the natural logarithm of 2 using linear interpolation.
First, perform the computation by interpolating between ln 1 = 0 and ln 6 =
1.791759.
Then, repeat the procedure, but use a smaller interval from ln 1= 0 to ln 4 =
1.386294. Note that the true value of ln 2 is 0.69314718
Solution:
Linear interpolation:
Case 1: 𝒙𝒐 = 𝟏, 𝒙𝟏 = 𝟔
𝒇𝟏 (𝒙) = 𝒇(𝒙𝒐 ) + 𝒇[𝒙𝟏 , 𝒙𝒐 ](𝒙 − 𝒙𝒐 )
𝒇(𝒙𝒐 ) = 𝒍𝒏 𝒙𝒐 = 𝒍𝒏 𝟏 = 𝟎
𝒇(𝒙𝟏 ) − 𝒇(𝒙𝒐 ) 𝒇(𝟔) − 𝒇(𝟏) 𝒍𝒏 𝟔 − 𝒍𝒏 𝟏
𝒇[𝒙𝟏 , 𝒙𝒐 ] = = = = 𝟎. 𝟑𝟓𝟖𝟑𝟓𝟏𝟗
𝒙𝟏 − 𝒙𝒐 𝟔−𝟏 𝟓
→ 𝒇𝟏 (𝒙) = 𝒇(𝟎) + 𝟎. 𝟑𝟓𝟖𝟑𝟓𝟏𝟗(𝒙 − 𝟏)
→ 𝒇𝟏 (𝟐) = 𝟎 + 𝟎. 𝟑𝟓𝟖𝟑𝟓(𝟐 − 𝟏) = 𝟎. 𝟑𝟓𝟖𝟑𝟓𝟏𝟗
(𝟎. 𝟔𝟗𝟑𝟏𝟒𝟕𝟏𝟖 − 𝟎. 𝟑𝟓𝟖𝟑𝟓𝟏𝟗)
→ ∈𝒕 = × 𝟏𝟎𝟎% = 𝟒𝟖. 𝟑%
𝟎. 𝟔𝟗𝟑𝟏𝟒𝟕𝟏𝟖
College of Engineering, Department of Mechanical Engineering
Lecture Notes 0402307 Numerical Analysis Khalid Ramadan
Case 2: 𝒙𝒐 = 𝟏, 𝒙𝟏 = 𝟒
𝒇𝟏 (𝒙) = 𝒇(𝒙𝒐 ) + 𝒇[𝒙𝟏 , 𝒙𝒐 ](𝒙 − 𝒙𝒐 )
𝒇(𝒙𝒐 ) = 𝒍𝒏 𝒙𝒐 = 𝒍𝒏 𝟏 = 𝟎
𝒇(𝒙𝟏 ) − 𝒇(𝒙𝒐 ) 𝒇(𝟒) − 𝒇(𝟏) 𝒍𝒏 𝟒 − 𝒍𝒏 𝟏
𝒇[𝒙𝟏 , 𝒙𝒐 ] = = = = 𝟎. 𝟒𝟔𝟐𝟎𝟗𝟖𝟏
𝒙𝟏 − 𝒙𝒐 𝟒−𝟏 𝟑
→ 𝒇𝟏 (𝒙) = 𝒇(𝟎) + 𝟎. 𝟒𝟔𝟐𝟎𝟗𝟖𝟏(𝒙 − 𝟏)
→ 𝒇𝟏 (𝟐) = 𝟎 + 𝟎. 𝟒𝟔𝟐𝟎𝟗𝟖𝟏(𝟐 − 𝟏) = 𝟎. 𝟒𝟔𝟐𝟎𝟗𝟖𝟏
(𝟎. 𝟔𝟗𝟑𝟏𝟒𝟕𝟏𝟖 − 𝟎. 𝟒𝟔𝟐𝟎𝟗𝟖𝟏)
→ ∈𝒕 = × 𝟏𝟎𝟎% = 𝟑𝟑. 𝟑%
𝟎. 𝟔𝟗𝟑𝟏𝟒𝟕𝟏𝟖
Example 2:
Fit a second-order polynomial to the three points used in Example 1:
𝒙𝒐 = 𝟏, 𝒇(𝒙𝒐 ) = 𝒍𝒏 𝟏 = 𝟎
𝒙𝟏 = 𝟒, 𝒇(𝒙𝟏 ) = 𝒍𝒏 𝟒 = 𝟏. 𝟑𝟖𝟔𝟐𝟗𝟒
𝒙𝟐 = 𝟔, 𝒇(𝒙𝟐 ) = 𝒍𝒏 𝟔 = 𝟏. 𝟕𝟗𝟏𝟕𝟓𝟗
Use the polynomial to estimate ln 2.
Solution:
𝒇𝟐 (𝒙) = 𝒇(𝒙𝒐 ) + 𝒇[𝒙𝟏 , 𝒙𝒐 ](𝒙 − 𝒙𝒐 ) + 𝒇[𝒙𝟐 , 𝒙𝟏 , 𝒙𝒐 ](𝒙 − 𝒙𝒐 )(𝒙 − 𝒙𝟏 )
College of Engineering, Department of Mechanical Engineering
Lecture Notes 0402307 Numerical Analysis Khalid Ramadan
𝒇(𝒙𝒐 ) = 𝟎
𝒇 [ 𝒙 𝟐 , 𝒙 𝟏 ] − 𝒇[ 𝒙 𝟏 , 𝒙 𝒐 ]
𝒇[ 𝒙 𝟐 , 𝒙 𝟏 , 𝒙 𝒐 ] =
𝒙𝟐 − 𝒙𝒐
Example 3:
In Example 2, data points at 𝒙𝒐 = 𝟏, 𝒙𝟏 = 𝟒, 𝒙𝟐 = 𝟔 were used to estimate ln 2
with a parabola. Now, adding a fourth point [𝒙𝟑 = 𝟓; 𝒇(𝒙𝟑 ) = 𝟏. 𝟔𝟎𝟗𝟒𝟑𝟖],
estimate ln 2 with a third-order Newton’s interpolating polynomial.
Solution:
The third-order polynomial is:
∈𝒕 = 𝟗. 𝟑%
College of Engineering, Department of Mechanical Engineering
Lecture Notes 0402307 Numerical Analysis Khalid Ramadan
Check:
𝒂𝒕 𝒙 = 𝒙𝒐 , → 𝒇𝟏 (𝒙) = 𝒇(𝒙𝒐 )
𝒂𝒕 𝒙 = 𝒙𝟏 , → 𝒇𝟏 (𝒙) = 𝒇(𝒙𝟏 )
Note:
𝒂𝒕 𝒙 = 𝒙𝒐 , → 𝒔𝒆𝒄𝒐𝒏𝒅 𝒕𝒆𝒓𝒎 𝒊𝒔 𝒛𝒆𝒓𝒐
𝒂𝒕 𝒙 = 𝒙𝟏 , → 𝑭𝒊𝒓𝒔𝒕 𝒕𝒆𝒓𝒎 𝒊𝒔 𝒛𝒆𝒓𝒐
(𝒙 − 𝒙𝒐 )(𝒙 − 𝒙𝟏 )
= (𝒇[𝒙𝟐 , 𝒙𝟏 ] − 𝒇[𝒙𝟏 , 𝒙𝒐 ])
𝒙𝟐 − 𝒙𝒐
(𝒙 − 𝒙𝒐 )(𝒙 − 𝒙𝟏 ) 𝒇(𝒙𝟐 ) − 𝒇(𝒙𝟏 ) 𝒇(𝒙𝟏 ) − 𝒇(𝒙𝒐 )
= ( − )
𝒙𝟐 − 𝒙𝒐 𝒙𝟐 − 𝒙𝟏 𝒙𝟏 − 𝒙𝒐
(𝒙 − 𝒙𝒐 )(𝒙 − 𝒙𝟏 ) (𝒙 − 𝒙𝒐 )(𝒙 − 𝒙𝟏 )
= (𝒇(𝒙𝟐 ) − 𝒇(𝒙𝟏 )) + (𝒇(𝒙𝟏 ) − 𝒇(𝒙𝒐 )) (𝟑)
(𝒙𝟐 − 𝒙𝒐 )(𝒙𝟐 − 𝒙𝟏 ) (𝒙𝟐 − 𝒙𝒐 )(𝒙𝟏 − 𝒙𝒐 )
Substitute (3) in (2) to get:
2nd order Lagrange interpolating polynomial:
𝒏 𝒏
𝒙 − 𝒙𝒋
𝒇𝒏 (𝒙) = ∑ ( ∏ ) 𝒇(𝒙𝒊 )
𝒙𝒊 − 𝒙𝒋
𝒊=𝟎 𝒋=𝟎,𝒋≠𝒊
𝒏 = 𝟏, → 𝒊 = 𝟎, 𝟏
𝟏 𝟏
𝒙 − 𝒙𝒋 𝒙 − 𝒙𝒋
𝒇𝟏 (𝒙) = ∏ 𝒇(𝒙𝒐 ) + ∏ 𝒇(𝒙𝟏 )
𝒙𝒐 − 𝒙𝒋 𝒙𝟏 − 𝒙𝒋
𝒋=𝟎,𝒋≠𝒊 𝒋=𝟎,𝒋≠𝒊
𝒊=𝟎 𝒊=𝟏
College of Engineering, Department of Mechanical Engineering
Lecture Notes 0402307 Numerical Analysis Khalid Ramadan
𝟏 𝟏
𝒙 − 𝒙𝟏 𝒙 − 𝒙𝒐
𝒇𝟏 (𝒙) = ∏ 𝒇(𝒙𝒐 ) + ∏ 𝒇(𝒙𝟏 )
𝒙𝒐 − 𝒙𝟏 𝒙𝟏 − 𝒙𝒐
𝒋=𝟎,𝒋≠𝒊 𝒋=𝟎,𝒋≠𝒊
𝒙 − 𝒙𝟏 𝒙 − 𝒙𝒐
𝒇𝟏 (𝒙) = 𝒇(𝒙𝒐 ) + 𝒇(𝒙𝟏 )
𝒙𝒐 − 𝒙𝟏 𝒙𝟏 − 𝒙𝒐
2. Derive the 2nd order Lagrange interpolating polynomial from the general
formula above:
𝒏 = 𝟐, → 𝒊 = 𝟎, 𝟏, 𝟐
𝟐 𝟐
𝒙 − 𝒙𝒋
𝒇𝟐 (𝒙) = ∑ ( ∏ ) 𝒇(𝒙𝒊 )
𝒙𝒊 − 𝒙𝒋
𝒊=𝟎 𝒋=𝟎,𝒋≠𝒊
𝟐 𝟐 𝟐
𝒙 − 𝒙𝒋 𝒙 − 𝒙𝒋 𝒙 − 𝒙𝒋
𝒇𝟐 (𝒙) = ∏ 𝒇(𝒙𝒐 ) + ∏ 𝒇(𝒙𝟏 ) + ∏ 𝒇(𝒙𝟐 )
𝒙𝒐 − 𝒙𝒋 𝒙𝟏 − 𝒙𝒋 𝒙𝟐 − 𝒙𝒋
𝒋=𝟎,𝒋≠𝒊 𝒋=𝟎,𝒋≠𝒊 𝒋=𝟎,𝒋≠𝒊
𝒊 = 𝟎, 𝒋 = 𝟏, 𝟐 𝒊 = 𝟏, 𝒋 = 𝟎, 𝟐 𝒊 = 𝟐, 𝒋 = 𝟎, 𝟏
Spline Interpolation
Rather than using higher – order polynomials to connect between (n+1)
discrete data points, we use lower – order polynomials to subsets of data
points.
Such lower order polynomials are called: “Spline Functions”.
Spline functions give better accuracy when there are abrupt changes in
data, such as shock waves, solid – liquid interface,…..
A visual representation of a situation where the splines are superior to higher-order interpolating
polynomials. The function to be fit undergoes an abrupt increase at x 5 0.
Parts (a) through(c) indicate that the abrupt change induces oscillations in interpolating
polynomials. In contrast, because it is limited to third-order curves with smooth transitions, a linear
spline (d) provides a much more acceptable approximation.
College of Engineering, Department of Mechanical Engineering
Lecture Notes 0402307 Numerical Analysis Khalid Ramadan
1. Linear Splines
Example:
Fit the data below in 1st order splines, then evaluate the function at x = 5.
x: 3 4.5 7.0 9.0
f(x): 2.5 1.0 2.5 0.5
Solution:
m: -1 0.6 -1.0 -
𝒇𝟏 (𝒙) = 𝟐. 𝟓 − (𝒙 − 𝟑), 𝟑 ≤ 𝒙 ≤ 𝟒. 𝟓
𝒇𝟐 (𝒙) = 𝟏. 𝟎 + 𝟎. 𝟔(𝒙 − 𝟒. 𝟓), 𝟒. 𝟓 ≤ 𝒙 ≤ 𝟕. 𝟎
𝒇𝟑 (𝒙) = 𝟐. 𝟓 − (𝒙 − 𝟕. 𝟎), 𝟕. 𝟎 ≤ 𝒙 ≤ 𝟗. 𝟎
→ 𝒇(𝟓) = 𝒇𝟐 (𝟓) = 𝟏. 𝟎 + 𝟎. 𝟔(𝟓 − 𝟒. 𝟓) = 𝟏. 𝟑
Notes:
Linear splines are not smooth;
check at the “knots” (points where splines meet):
𝒇′𝟏 = −𝟏, 𝒇′𝟐 = 𝟎. 𝟔, 𝒇′𝟑 = −𝟏
→ 𝒍𝒊𝒏𝒆𝒂𝒓 𝒔𝒑𝒍𝒊𝒏𝒆𝒔: 𝟏𝒔𝒕 𝒅𝒆𝒓𝒊𝒗𝒂𝒕𝒊𝒗𝒆 𝒊𝒔 𝒅𝒊𝒔𝒄𝒐𝒏𝒕𝒊𝒏𝒖𝒐𝒖𝒔
2. Quadratic Splines
1st derivative is smooth at the “knots”
Quadratic Splines
College of Engineering, Department of Mechanical Engineering
Lecture Notes 0402307 Numerical Analysis Khalid Ramadan
𝒇𝒊 (𝒙) = 𝒂𝒊 𝒙𝟐 + 𝒃𝒊 𝒙 + 𝒄𝒊 , 𝒊 = 𝟏, … , 𝒏 − 𝟏
Conditions:
𝒇𝒊 (𝒙𝒊 ) = 𝒂𝒊 𝒙𝟐𝒊 + 𝒃𝒊 𝒙𝒊 + 𝒄𝒊 , 𝒊 = 𝟏, 𝟐, … , 𝒏 − 𝟏
= 𝟑(𝒏 − 𝟏) − 𝟏
Example: 𝒇′′
𝟏 (𝒙𝟏 ) = 𝟎, → 𝒂𝟏 = 𝟎
Example
Solution:
College of Engineering, Department of Mechanical Engineering
Lecture Notes 0402307 Numerical Analysis Khalid Ramadan
𝒇𝒊 (𝒙𝒊 ) = 𝒂𝒊 𝒙𝟐𝒊 + 𝒃𝒊 𝒙𝒊 + 𝒄𝒊 , 𝒊 = 𝟏, 𝟐, 𝟑, 𝟒
3. Cubic Splines
Cubic Splines
General Form:
𝒇𝒊 (𝒙) = 𝒂𝒊 𝒙𝟑 + 𝒃𝒊 𝒙𝟐 + 𝒄𝒊 𝒙 + 𝒅𝒊 , 𝒊 = 𝟏, … , 𝒏 − 𝟏
𝒇′′ ′′
𝒊 (𝒙𝒊+𝟏 ) = 𝒇𝒊+𝟏 (𝒙𝒊+𝟏 ), 𝒊 = 𝟏, 𝟐, … . , 𝒏 − 𝟐
Short by 2 conditions
𝒇′′
𝟏 (𝒙𝟏 ) = 𝟎, → 𝟔𝒂𝟏 𝒙𝟏 + 𝒃𝟏 = 𝟎
𝒇′′
𝒏−𝟏 (𝒙𝒏 ) = 𝟎, → 𝟔𝒂𝒏−𝟏 𝒙𝒏 + 𝒃𝒏−𝟏 = 𝟎
College of Engineering, Department of Mechanical Engineering
Lecture Notes 0402307 Numerical Analysis Khalid Ramadan
Problem 2:
Given 𝒇(𝒙) = 𝒍𝒏 𝒙,
Estimate ln 2 using:
A. Linear Lagrange interpolating functions, with 𝒙𝒐 = 𝟏, 𝒙𝟏 = 𝟒
B. 2nd order Lagrange interpolating functions, with 𝒙𝒐 = 𝟏, 𝒙𝟏 = 𝟒, 𝒙𝟐 = 𝟓
C. 3rd order Lagrange interpolating functions, with
𝒙𝒐 = 𝟏, 𝒙𝟏 = 𝟒, 𝒙𝟐 = 𝟓, 𝒙𝟑 = 𝟔