<<

Spline

 Given: (n + 1) observations or data pairs [(x0, f0), (x1, f1), (x2, f2) … (xn, fn)]

 This gives a mesh of nodes 𝑥,𝑥,𝑥,⋯𝑥 on the independent variable and the corresponding values as 𝑓,𝑓,𝑓,⋯𝑓  Goal: fit an independent in each interval (between two points) with certain continuity requirements at the nodes.  Linear : continuity in function values, C0 continuity  Quadratic spline: continuity in function values and 1st , C1 continuity  Cubic spline: continuity in function values, 1st and 2nd derivatives, C2 continuity

 Denote for node i or at xi: functional value fi, first ui, second derivative vi : Cubic

xi+2, fi+2

xi, fi qi(x) qi+1(x)

qi-1(x) xi+1, fi+1 𝑞 𝑥 𝑓 xi-1, fi-1 𝑞 𝑥 𝑞 𝑥 𝑞 𝑥 𝑢 𝑞 𝑥 𝑞 𝑥 𝑣 𝑞 𝑥

 A cubic polynomial in each interval: (n+1) points, n cubic , 4n unknowns  Available conditions: (n + 1) function values, (n - 1) function continuity, (n -1) 1st derivative continuity conditions and (n -1) 2nd derivative continuity conditions, total 4n - 2 conditions.  2 free conditions to be chosen by the user! Spline Interpolation: Cubic

 Cubic Spline in the interval 𝑥,𝑥 : 𝑞 𝑥  𝑞 𝑥 is a set of linear splines.  Let us denote the 2nd derivative (v) of the function at the ith node as 𝑓 𝑥 𝑣 𝑥 𝑣  Therefore: 𝑞 𝑥 𝑣 𝑥 𝑣 and 𝑞 𝑥 𝑣 𝑥 𝑣  We may write: 𝑣 𝑣 𝑞 𝑥 𝑥𝑥 𝑥𝑥 ℎ ℎ 𝑣 𝑣 𝑞 𝑥 𝑥𝑥 𝑥𝑥 𝑐𝑥𝑐 6ℎ 6ℎ 𝑣 ℎ 𝑞 𝑥 𝑓 𝑐 𝑥 𝑐 6 𝑣 ℎ 𝑞 𝑥 𝑓 𝑐 𝑥 𝑐 6 Spline Interpolation: Cubic

𝑣 𝑣 𝑞 𝑥 𝑥𝑥 𝑥𝑥 𝑐𝑥𝑐 6ℎ 6ℎ 𝑣 ℎ 𝑣 ℎ 𝑞 𝑥 𝑓 𝑐 𝑥 𝑐 ; 𝑞 𝑥 𝑓 𝑐 𝑥 𝑐 6 6 ℎ 𝑓 𝑓 ℎ ℎ 𝑐 𝑓𝑥,𝑥 𝑣 𝑣 𝑣 𝑣 6 ℎ ℎ 6 6 𝑓 𝑓 ℎ ℎ 𝑐 𝑥 𝑥 𝑣𝑥 𝑣𝑥 ℎ ℎ 6 6 𝑞 𝑥 𝑣 𝑥𝑥 𝑣 𝑥𝑥 ℎ 𝑥𝑥 ℎ 𝑥𝑥 6 ℎ 6 ℎ 𝑓 𝑓 𝑥𝑥 𝑥𝑥 ℎ ℎ

We need to estimate (n + 1) unknown vi. We have (n – 1) conditions from the continuity of the first derivative. Spline Interpolation: Cubic

𝑞 𝑥 𝑞 𝑥 ; 𝑖 1, 2, 3, ⋯ 𝑛 1 …𝑒𝑞. 1 𝑞 𝑥 𝑣 𝑥𝑥 𝑣 𝑥𝑥 𝑓 𝑓 ℎ 𝑥𝑥 ℎ 𝑥𝑥 𝑥𝑥 𝑥𝑥 6 ℎ 6 ℎ ℎ ℎ 𝑣 3 𝑥𝑥 𝑣 3 𝑥𝑥 𝑞 𝑥 ℎ ℎ 𝑓 𝑥,𝑥 6 ℎ 6 ℎ 𝑣 3 𝑥𝑥 𝑣 3 𝑥𝑥 𝑞 𝑥 ℎ ℎ 𝑓 𝑥,𝑥 6 ℎ 6 ℎ In eq. 1, ℎ ℎ ℎ ℎ 𝑣 𝑣 𝑓 𝑥 ,𝑥 𝑣 𝑣 𝑓 𝑥 ,𝑥 6 3 3 6 ℎ ℎ ℎ ℎ 𝑣 𝑣 𝑣 𝑓𝑥 ,𝑥 𝑓 𝑥 ,𝑥 6 3 6 𝒉 𝒉 𝒉 𝒉 𝒊𝟏 𝒗 𝒊 𝒊𝟏 𝒗 𝒊 𝒗 𝒇𝒙 ,𝒙 𝒇𝒙 ,𝒙 𝟔 𝒊𝟏 𝟑 𝒊 𝟔 𝒊𝟏 𝒊𝟏 𝒊 𝒊 𝒊𝟏

 𝐡𝐢𝟏𝐯𝐢𝟏 𝟐 𝐡𝐢 𝐡𝐢𝟏 𝐯𝐢 𝐡𝐢𝐯𝐢𝟏 𝟔𝐟𝐱𝐢𝟏,𝐱𝐢 𝟔𝐟𝐱𝐢,𝐱𝐢𝟏

Spline Interpolation: Cubic

𝒉𝒊𝟏𝒗𝒊𝟏 𝟐 𝒉𝒊 𝒉𝒊𝟏 𝒗𝒊 𝒉𝒊𝒗𝒊𝟏 𝟔𝒇𝒙𝒊𝟏,𝒙𝒊 𝟔𝒇𝒙𝒊,𝒙𝒊𝟏 𝑖 1, 2, 3, ⋯ 𝑛. 1  So, (n – 1) equations, (n + 1) unknowns, two conditions have to be provided by the users. They decide the type of cubic splines  Natural Spline: 𝑣 𝑣 0  Parabolic Runout: 𝑣 𝑣 and 𝑣 𝑣  Not-a-knot: 𝑣 𝑣 𝑣 𝑣 𝑞 𝑥𝑞 𝑥 ⟹ ℎ ℎ 𝑣 𝑣 𝑣 𝑣 𝑞 𝑥𝑞 𝑥 ⟹ ℎ ℎ  Periodic: 𝑓 𝑓; 𝑢 𝑢 and 𝑣 𝑣 First one comes from the data (if not satisfied, the periodic spline is not appropriate); the next two give the other two equations. Spline Interpolation: Cubic

𝒉𝒊𝟏𝒗𝒊𝟏 𝟐 𝒉𝒊 𝒉𝒊𝟏 𝒗𝒊 𝒉𝒊𝒗𝒊𝟏 𝟔𝒇𝒙𝒊𝟏,𝒙𝒊 𝟔𝒇𝒙𝒊,𝒙𝒊𝟏  𝑖 1, 2, 3, ⋯ 𝑛. 1 So, (n – 1) equations, (n + 1) unknowns, two conditions have to be provided by the users. They decide the type of cubic splines  Clamped Spline: 𝑢 𝑞 𝑥 𝛼 and 𝑢 𝑞 𝑥 β 𝑣 3 𝑥𝑥 𝑣 3 𝑥𝑥 𝑞 𝑥 ℎ ℎ 𝑓 𝑥,𝑥 6 ℎ 6 ℎ 𝑣ℎ 𝑣ℎ 𝟔 𝑞 𝑥 𝑓 𝑥,𝑥 𝛼 ⟹ 𝟐𝒗𝟎 𝒗𝟏 𝒇 𝒙𝟏,𝒙𝟎 𝜶 6 3 𝒉𝟎 𝑣 3 𝑥𝑥 𝑣 3 𝑥𝑥 𝑞 𝑥 ℎ ℎ 𝑓 𝑥,𝑥 6 ℎ 6 ℎ 𝑣 ℎ 𝑣 ℎ 𝑞 𝑥 𝑓 𝑥 ,𝑥 β 3 6 𝟔 ⟹ 𝟐𝒗𝒏 𝒗𝒏𝟏 𝜷𝒇 𝒙𝒏,𝒙𝒏𝟏 𝒉𝒏𝟏 Example Problem: Q4 of Tutorial 9

Consider the function exp(x) sampled at points

x, 0 0.5, 1.0, 1.5 and 2 Estimate the function value at x = 1.80 by interpolating the function using - (a) natural cubic spline and (b) not-a-knot cubic spline. Calculate the true percentage error for both the splines. Which is the better spline for this problem and why?

Solution: Posted online along with Tutorial 9 solutions Example Problem: Heat Transfer in Lake

Lakes in temperate zone can become thermally stratified during the summer. As depicted below, warm, buoyant water near the surface overlies colder, denser bottom water. Such stratification effectively divides the lake into two layers: the epilimnion and the hypolimnion separated by a plane called the thermocline.

Temperature (oC) z (m) T(oC) 0.0 5.0 10.0 15.0 20.0 25.0 0 022.8 2.3 22.8 5 4.9 22.8 10 9.1 22.6 15 13.7 13.9

20 18.3 11.7 Depth, z(m) 22.9 11.1 25 27.2 11.1 30 Example Problem: Heat Transfer in Lake

The location of the thermocline can be defined as the

inflection point of the T-z ; i.e. where = 0. It is also the point at which the absolute value of the first derivative or gradient is maximum. Use cubic splines to determine the thermocline depth of this lake. Also use splines to determine the value of the gradient at the thermocline. Spline Interpolation: Using Local Coordinate

𝑥∈ 𝑥,𝑥 → 𝑡 ∈ 0, 1 𝑞 𝑥 in 𝑥,𝑥 → 𝑝 𝑡 in 0, 1  At each node i, we denote the following:

 Location: xi

 Functional value: fi  Intervals: ℎ 𝑥 𝑥 and ℎ 𝑥 𝑥 nd  Derivatives: First derivative ui and the 2 derivative vi  Transformations: 𝑥𝑥 1 𝑑𝑡 1 𝑡 𝑥𝑥 ⟹ 𝑥 𝑥 ℎ 𝑑𝑥 ℎ 𝑑𝑡 1 1 𝑞 𝑥𝑝 𝑡 𝑝 𝑡 𝑞 𝑥 𝑝 𝑡 𝑑𝑥 ℎ ℎ Spline Interpolation: Using Local Coordinate

 C0 – Continuity: 𝑝 1𝑞 𝑥 𝑞 𝑥 𝑝 0𝑓

 C1 – Continuity: 1 1 𝑝 1𝑞 𝑥 𝑞 𝑥 𝑝 0𝑢 ℎ ℎ

 C2 – Continuity: 1 1 𝑝 1𝑞 𝑥 𝑞 𝑥 𝑝 0𝑣 ℎ ℎ Linear and Quadratic Splines: Local Coordinate

 Linear Spline: C0 – Continuous 𝑝 𝑡𝑎𝑡𝑏 ⇒ 𝑝 0𝑏 𝑓,𝑝 1𝑎 𝑏 𝑓 𝑝 𝑡𝑓 𝑓 𝑡𝑓 ⟹ 𝑞 𝑥𝑓𝑥,𝑥 𝑥𝑥 𝑓

 Quadratic Spline: C1 – Continuous 𝑝 𝑡𝑎𝑡 𝑏𝑡𝑐 ⇒ 𝑝 0𝑐 𝑓, 𝑝 1𝑎 𝑏 𝑐 𝑓

Using the definition of ui: 1 𝑏 𝑝 0 𝑢 ⇒ 𝑎 ℎ 𝑓 𝑥,𝑥 𝑢 ℎ ℎ 𝑝 𝑡ℎ 𝑓 𝑥,𝑥 𝑢 𝑡 ℎ𝑢𝑡𝑓

Using C1 – Continuity: 1 1 𝑝 1 𝑝 0 ⇒ 𝑢 2𝑓𝑥,𝑥 𝑢 ℎ ℎ Cubic Spline: Using Local Coordinate

 Cubic Spline: C2 – Continuous 𝑝 𝑡𝑎𝑡 𝑏𝑡 𝑐𝑡𝑑 Using C0 – Continuity: 𝑝 0𝑑 𝑓, 𝑝 1𝑎 𝑏 𝑐 𝑑 𝑓 Now we have two options:

st 2 Option 1: Using the 1 derivative ui as unknown and C – Continuity to estimate them

nd 1 Option 2: Using the 2 derivative vi as unknown and C – Continuity to estimate them Cubic Spline: Using Local Coordinates

st 2 Option 1: Using the 1 derivative ui as unknown and C – Continuity to estimate them 𝑝 𝑡𝑎𝑡 𝑏𝑡 𝑐𝑡𝑑 𝑑 𝑓, 𝑎 𝑏 𝑐 𝑑 𝑓 1 𝑐 1 3𝑎 2𝑏 𝑐 𝑝 0 𝑢; 𝑝 1 𝑢 ℎ ℎ ℎ ℎ 𝑎 ℎ 𝑢 𝑢 2𝑓 𝑥,𝑥 𝑏 ℎ 3𝑓 𝑥,𝑥 𝑢 2𝑢 Using C2 – Continuity: 1 1 6𝑎 2𝑏 2𝑏 𝑝 1 𝑝 0⇒ ℎ ℎ ℎ ℎ ℎ𝑢 2 ℎ ℎ 𝑢 ℎ𝑢 3ℎ𝑓 𝑥,𝑥 3ℎ𝑓 𝑥,𝑥 𝑖 1, 2, 3, ⋯ 𝑛 1 Using the two other conditions, one may obtain similar splines of different types! Cubic Spline: Using Local Coordinates  Natural Spline: 𝑣 𝑣 0 𝑝 0 2𝑏 𝑣 0; 𝑏 ℎ 3𝑓 𝑥,𝑥 𝑢 2𝑢 0 ℎ ℎ 𝑝 1 6𝑎 2𝑏 𝑣 0 ℎ ℎ 6ℎ 𝑢 𝑢 2𝑓 𝑥,𝑥 2ℎ 3𝑓 𝑥,𝑥 𝑢 2𝑢 0 2𝑢 𝑢 3𝑓𝑥,𝑥 2𝑢 𝑢 3𝑓𝑥,𝑥  Clamped Spline: 𝑢 𝛼 and 𝑢 β  Parabolic Runout: 𝑣 𝑣 and 𝑣 𝑣 𝑝 0 𝑝 1 2𝑏 6𝑎 2𝑏 ⟹ ⟹𝑢 𝑢 2𝑓𝑥,𝑥 ℎ ℎ ℎ ℎ 𝑝 0 𝑝 1 2𝑏 6𝑎 2𝑏 ⟹ ⟹𝑢 𝑢 2𝑓𝑥,𝑥 ℎ ℎ ℎ ℎ Cubic Spline: Using Local Coordinates

 Not-a-knot: 𝑣 𝑣 𝑣 𝑣 𝑞 𝑥𝑞 𝑥 ⟹ ℎ ℎ 𝑣 𝑣 𝑣 𝑣 𝑞 𝑥𝑞 𝑥 ⟹ ℎ ℎ  Periodic: 𝑓 𝑓; 𝑢 𝑢 and 𝑣 𝑣 First one comes from the data (if not satisfied, the periodic spline is not appropriate); the next two give the other two equations. Formulation of these two is left as homework! Cubic Spline: Using Local Coordinates

nd 1 Option 2: Using the 2 derivative vi as unknown and C – Continuity to estimate them 𝑝 𝑡𝑎𝑡 𝑏𝑡 𝑐𝑡𝑑 𝑑 𝑓, 𝑎 𝑏 𝑐 𝑑 𝑓 1 2𝑏 1 6𝑎 2𝑏 𝑝 0 𝑣; 𝑝 1 𝑣 ℎ ℎ ℎ ℎ ℎ ℎ 𝑎 𝑣 𝑣 ; 𝑐 ℎ𝑓 𝑥 ,𝑥 𝑣 2𝑣 6 6 Using C1 – Continuity: 1 1 3𝑎 2𝑏 𝑐 𝑐 𝑝 1 𝑝 0 ⇒ ℎ ℎ ℎ ℎ ℎ𝑣 2 ℎ ℎ 𝑣 ℎ𝑣 6𝑓𝑥,𝑥 6𝑓 𝑥,𝑥 𝑖 1, 2, 3, ⋯ 𝑛 1 This is the same equation that was obtained using Lagrange polynomials! Boundary conditions are also the same! ESO 208A: Computational Methods in Engineering Numerical Differentiation Abhas Singh Department of Civil Engineering IIT Kanpur

Acknowledgements: Profs. Saumyen Guha and Shivam Tripathi (CE) Numerical Differentiation

Let us compute dy/dx or df/dx at node i Denote the difference operators: ∆𝑥 𝑥 𝑥 𝛻𝑥 𝑥 𝑥 𝛿𝑥 𝑥 𝑥 Numerical Differentiation: Finite Difference

Approximate the function between 𝑥,𝑥 as:

𝑥𝑥 𝑥𝑥 𝑓 𝑓 𝑓 𝑥 𝑓 𝑓 𝑥𝑥 𝑥𝑥 𝑥 𝑥 𝑥 𝑥 ∆𝑥 ∆𝑥 Forward Difference: 𝑑𝑓 𝑓 𝑓 ∆𝑓 𝑑𝑥 ∆𝑥 ∆𝑥

Approximate the function between 𝑥,𝑥 as:

𝑥𝑥 𝑥𝑥 𝑓 𝑓 𝑓 𝑥 𝑓 𝑓 𝑥𝑥 𝑥𝑥 𝑥 𝑥 𝑥 𝑥 𝛻𝑥 𝛻𝑥 Backward Difference: 𝑑𝑓 𝑓 𝑓 𝛻𝑓 𝑑𝑥 𝛻𝑥 𝛻𝑥 Numerical Differentiation: Finite Difference

Approximate the function between three points: 𝑥,𝑥,𝑥 𝑓 𝑥

𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑓 𝑓 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥

𝑥𝑥 𝑥𝑥 𝑓 𝑥 𝑥 𝑥 𝑥 𝑓 𝑓 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝛻𝑥 ∆𝑥 𝛻𝑥 𝛻𝑥∆𝑥 𝑓 𝑥𝑥 𝑥𝑥 ∆𝑥 𝛻𝑥 ∆𝑥

Now, evaluate df/dx at x = xi: 𝑑𝑓 𝑓 𝑓 𝑓 ∆𝑥 𝛻𝑥 ∆𝑥 𝛻𝑥 𝑑𝑥 𝛻𝑥 ∆𝑥 𝛻𝑥 𝛻𝑥∆𝑥 ∆𝑥 𝛻𝑥 ∆𝑥 Numerical Differentiation: Finite Difference

Central Difference: 𝑑𝑓 𝑑𝑥 𝑓 𝑓 𝑓 ∆𝑥 𝛻𝑥 ∆𝑥 𝛻𝑥 𝛻𝑥 ∆𝑥 𝛻𝑥 𝛻𝑥∆𝑥 ∆𝑥 𝛻𝑥 ∆𝑥 For regular or uniform grid:

Let us assume regular grid with a mesh size of h Numerical Differentiation: Finite Difference

Approximate the function between three points: 𝑥,𝑥,𝑥

𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑓 𝑥 𝑓 𝑓 𝑓 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥

𝑓 𝑓 𝑓 𝑓 𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 2ℎ ℎ 2ℎ

2 2 Now, evaluate central difference approximations of df/dx and d f/dx at x = xi: 𝑑𝑓 𝑓 𝑓 𝑓 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑥𝑥 𝑑𝑥 2ℎ ℎ 2ℎ 𝑑𝑓 𝑓 𝑓 𝑑𝑥 2ℎ 𝑑𝑓 𝑓 2𝑓 𝑓 𝑑𝑥 ℎ Numerical Differentiation: Finite Difference  Similarly, one can approximate the function between

three points and obtain the forward difference expressions of the first and second

derivatives at x = xi as follows:

This is left for homework practice! Numerical Differentiation: Finite Difference  Similarly, one can approximate the function between

three points and obtain the backward difference expressions of the first and second

derivatives at x = xi as follows:

This is left for homework practice! Numerical Differentiation: Finite Difference

 Accuracy: How accurate is the numerical differentiation scheme with respect to the TRUE differentiation?  Truncation Error analysis  Modified Wave Number, Amplitude Error and Phase Error analysis for periodic functions  Recall: True Value (a) = Approximate Value 𝑎 + Error (ε)  Consistency: A numerical expression for differentiation or a numerical differentiation scheme is consistent if it converges to the TRUE differentiation as h → 0.