Spline Power Point
Spline Power Point
Cubic Splines
Cubic Splines...
Joseph M. Mahaffy,
[email protected]
Spring 2018
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (1/48)
Polynomial Interpolation
Cubic Splines
Cubic Splines...
Outline
1 Polynomial Interpolation
Checking the Roadmap
Undesirable Side-effects
New Ideas...
2 Cubic Splines
Introduction
Building the Spline Segments
Associated Linear Systems
3 Cubic Splines...
Error Bound
Solving the Linear Systems
Polynomial Interpolation
Construct a polynomial passing through the points (x0 , f (x0 )),
(x1 , f (x1 )), (x2 , f (x2 )), . . . , (xN , f (xn )).
Define Ln,k (x), the Lagrange coefficients:
n
Y x − xi x − x0 x − xk−1 x − xk+1 x − xn
Ln,k (x) = = ··· · ··· ,
xk − xi xk − x0 xk − xk−1 xk − xk+1 xk − xn
i=0, i6=k
where
Hn,j (x) = 1 − 2(x − xj )L′n,j (xj ) L2n,j (x)
Ĥn,j (x) = (x − xj )L2n,j (x),
and Ln,j (x) are our old friends, the Lagrange coefficients:
n
Y x − xi
Ln,j (x) = .
i=0, i6=j
xj − xi
1.5
f (x)
P2 (x)
1 H5 (x)
P5 (x)
0.5
0
y
-0.5
-1
-1.5
-2
0 π/2 π 3π/2 2π
x
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (8/48)
Polynomial Interpolation Checking the Roadmap
Cubic Splines Undesirable Side-effects
Cubic Splines... New Ideas...
P(x)
0
f(x_i)
-1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
P(x)
f(x_i)
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
d. Sj′ (xj+1 ) = ′
Sj+1 (xj+1 ), ∀j = 0, 1, . . . , (n − 2). Slope-match
e. Sj′′ (xj+1 ) = ′′
Sj+1 (xj+1 ), ∀j = 0, 1, . . . , (n − 2). Curvature-match
A Spline Segment
S{j}(x) S{j+1}(x)
S[n]
S[0]
S[2]
S[1] S[n−1]
x[0]
x[1]
x[2]
x[n−1] x[n]
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (21/48)
Polynomial Interpolation Introduction
Cubic Splines Building the Spline Segments
Cubic Splines... Associated Linear Systems
4.5
3.5
2.5
1.5
0.5
0
0 2 4 6 8 10 12 14
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (22/48)
Polynomial Interpolation Introduction
Cubic Splines Building the Spline Segments
Cubic Splines... Associated Linear Systems
4.5
3.5
2.5
1.5
0.5
0
0 2 4 6 8 10 12 14
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (22/48)
Polynomial Interpolation Introduction
Cubic Splines Building the Spline Segments
Cubic Splines... Associated Linear Systems
4.5
3.5
2.5
1.5
0.5
0
0 2 4 6 8 10 12 14
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (22/48)
Polynomial Interpolation Introduction
Cubic Splines Building the Spline Segments
Cubic Splines... Associated Linear Systems
4.5
3.5
2.5
1.5
0.5
0
0 2 4 6 8 10 12 14
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (22/48)
Polynomial Interpolation Introduction
Cubic Splines Building the Spline Segments
Cubic Splines... Associated Linear Systems
4.5
3.5
2.5
1.5
0.5
0
0 2 4 6 8 10 12 14
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (22/48)
Polynomial Interpolation Introduction
Cubic Splines Building the Spline Segments
Cubic Splines... Associated Linear Systems
We start with
Sj (x) = aj + bj (x − xj ) + cj (x − xj )2 + dj (x − xj )3
∀j ∈ {0, 1, . . . , n − 1}
Notice: The only unknowns are {cj }nj=0 , since the values of
n−1
{aj }nj=0 and {hj }j=0 are given.
n−1
Once we compute {cj }j=0 , we get
n−1
We are almost ready to solve for the coefficients {cj }j=0 , but
we only have (n − 1) equations for (n + 1) unknowns...
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (25/48)
Polynomial Interpolation Introduction
Cubic Splines Building the Spline Segments
Cubic Splines... Associated Linear Systems
1 0 0 ··· ··· 0
.. ..
h0 2(h0 + h1 ) h1 . .
.. ..
0 h1 2(h1 + h2 ) h2 . .
A= .. .. .. .. .. ..
. . . . . . ,
.. .. .. ..
. . . . 0
.. ..
. . hn−2 2(hn−2 + hn−1 ) hn−1
0 ··· ··· 0 0 1
2h0 h0 0 ··· ··· 0
.. ..
h0
2(h0 + h1 ) h1 . .
.. ..
0
h1 2(h1 + h2 ) h2 . .
A = ... .. .. .. .. ..
. . . . .
.. .. .. ..
. . . . 0
.. ..
. . hn−2 2(hn−2 + hn−1 ) hn−1
0 ··· ··· 0 hn−1 2hn−1
16 n = length(xi);
17
18 % Vector h with subintervals:
19 h = zeros(n-1,1);
20 for j = 1:n-1
21 h(j) = xi(j+1) - xi(j);
22 end
23
24 % Coefficient matrix A:
25 A = zeros(n);
26
27 % Natural Spline boundary conditions:
28 A(1,1)= 1;
29 A(n,n) = 1;
31 for i = 2:n-1
32 A(i,i-1) = h(i-1);
33 A(i,i) = 2*(h(i-1)+h(i));
34 A(i,i+1) = h(i);
35 end
36
37 % Vector b:
38 b = zeros(n,1);
39
40 for i = 2:n-1
41 b(i) = (3/h(i))*(a(i+1)-a(i)) - ...
(3/h(i-1))*(a(i)-a(i-1));
42 end
43
44 % Coefficient vector cj:
45 cj = A\b;
62 for i = 1:n-1
63 P(i,1) = dj(i);
64 P(i,2) = cj(i);
65 P(i,3) = bj(i);
66 P(i,4) = a(i);
67 X(i,1) = xi(i);
68 X(i,2) = xi(i+1);
69 end
70 P = [P,X];
72 % Plotting results:
73 resolution = 100;
74 for i = 1:n-1
75 f = @(x) a(i) + bj(i).*(x-xi(i)) + ...
cj(i).*(x-xi(i)).ˆ2 + dj(i).*(x-xi(i)).ˆ3;
76 xf = linspace(xi(i),xi(i+1),resolution);
77 plot(xf,f(xf),'b-');
78 end
79 %print -depsc spline nat.eps
80 end
10
0
0 2 4 6 8 10 12 14
39 % Vector b:
40 b = zeros(n,1);
41 b(1) = (3/h(1))*(a(2)-a(1)) - 3*fp(1);
42 b(n) = 3*fp(2) - (3/h(n-1))*(a(n)-a(n-1));
−5
0 2 4 6 8 10
Piecewise Poly. Approx.; Cubic Splines —
Joseph M. Mahaffy, [email protected] (42/48)
Polynomial Interpolation
Error Bound
Cubic Splines
Solving the Linear Systems
Cubic Splines...
5M h4
max |f (x) − S(x)| ≤
a≤x≤b 384
We notice that the linear systems for both natural and clamped
boundary conditions give rise to tri-diagonal linear systems.
Further, these systems are strictly diagonally dominant —
the entries on the diagonal outweigh the sum of the off-diagonal
elements (in absolute terms) —, so pivoting (re-arrangement to
avoid division by a small number) is not needed when solving
for x̃ using Gaussian Elimination...
This means that these systems can be solved very quickly (we
will revisit this topic later on, but for now the algorithm is on
the next couple of slides), see also “Computational Linear
Algebra / Numerical Matrix Analysis.” Math 543
1 function y = tridiag( a, b, c, f )
2
3 % Solve the n x n tridiagonal system Ay = f ...
for y
4 % with diagonal a, subdiagonal b, and ...
superdiagonal c
5 % f must be a vector (row or column) of length n
6 % a, b, c must be vectors of length n
7 % (note that b(1) and c(n) are not used)
20 n = length(f);
21 v = zeros(n,1);
22 y = v;
23 w = a(1);
24 y(1) = f(1)/w;
25 for i=2:n
26 v(i-1) = c(i-1)/w;
27 w = a(i) - b(i)*v(i-1);
28 y(i) = ( f(i) - b(i)*y(i-1) )/w;
29 end
30 for j=n-1:-1:1
31 y(j) = y(j) - v(j)*y(j+1);
32 end