Interpolating With Splines: Linear Spline Cubic Spline Interpolation Polynomials
Interpolating With Splines: Linear Spline Cubic Spline Interpolation Polynomials
Interpolating With Splines: Linear Spline Cubic Spline Interpolation Polynomials
0
x0 x1 xi-1 xi xi+1 xn-1 xn
2
0
!1 0 1
Numerical Analysis 2, lecture 4, slide! 2
s1 si
si+1 sn error in ith piece
f (x) ! si (x) " 18 (xi ! xi!1 ) 2 max f $$(# )
!#"# $ xi!1 "# " xi
hi
x0 x1 xi-1 xi xi+1 xn-1 xn knots f !(x) " si! (x) # hi max f !!($ )
xi"1 #$ # xi
The two extra conditions for cubic splines Recall the cardinal functions
can be chosen in many ways for Hermite interpolation (lecture 6)
not-a-knot b.c.
s1!!!= s2!!!, sn"1
!!! = sn!!! A1 B1
periodic b.c.
s1! (x0 ) = sn! (xn ), s1!!(x0 ) = sn!!(xn )
x = [-0.2 0:6];
function pp = nspline(x,y) f = [1 0 0 0 0 0 0 0];
>> s = nspline(x,f); sk = spline(x,f);
>> ss = ppval(s,xx); x = x(:); y = y(:);
n = length(x)-1; sn = nspline(x,f);
>> plot(xx,ss,'r',x,f,'ko') xx = linspace(x(1),x(end));
>> plot(xx,ss-ff,’r’,x,f-f,’ko’) if n == 1
pp=mkpp(x',[diff(y)./diff(x) y(1)]); plot(xx,ppval(sk,xx),'r',xx,ppval(sn,xx),'b',x,f,'o')
else legend('not-a-knot','natural')
1 h = diff(x);
A = [[h(2:n);1;NaN],...
[2;2*(h(1:n-1)+h(2:n));2],... 1
[NaN;1;h(1:n-1)]];
T = spdiags(A,-1:1,n+1,n+1); not!a!knot
dy = diff(y); natural
k = dy./h;
0
!1 0 1 r = 3*[k(1);h(2:n).*k(1:n-1)...
+h(1:n-1).*k(2:n);k(n)]; 0
g = T\r;
0.02
d = (g(1:n) + g(2:n+1) - 2*k)./h.^2;
c = (3*k - 2*g(1:n) - g(2:n+1))./h;
b = g(1:n);
0
pp = mkpp(x',[ d c b y(1:n) ]);
end
0 6
!0.02
!1 0 1
1 + 3$ 2
max f ""(x) # s""(x) ! h max f """"(x)
x0 ! x! xn 12 x0 ! x! xn
!=
max hi
min hi
• boundary conditions: natural, correct, not-a-knot, periodic
rules of thumb
• use small knot spacing where |f!!| is large Next: Chebyshev approximation (§9.7, 9.9)
• avoid rapidly changing knot spacing
Numerical Analysis 2, lecture 4, slide! 13 Numerical Analysis 2, lecture 4, slide! 14