scientific computing
DESCRIPTION
Scientific Computing. Linear and Quadratic Splines. Splines Overview. We have looked at two methods of finding interpolating polynomials for a set of data points – Lagrange polynomials and Newton’s method . - PowerPoint PPT PresentationTRANSCRIPT
Scientific Computing
Linear and Quadratic Splines
Splines Overview
• We have looked at two methods of finding interpolating polynomials for a set of data points – Lagrange polynomials and Newton’s method.
• While these methods are fairly simple to program, they have a serious drawback – for a large number of data points, the interpolating polynomial can be quite unstable.
Splines Overview
• Here is a set of 11 data points and the computer interpolating polynomial. Note the high degree of oscillation near the ends. This is an unfortunate property of interpolating polynomials of high degree!
Splines Overview
• Here is a picture of the same data, but with spline curves interpolating the data. Note how this interpolation is so much more smooth and stable than the previous example.
What is a Spline Curve?
• A spline curve is a curve that is made of a set of simple curves (lines, quadratics, cubics) that are joined together.
• A spline is piece-wise defined. That is, it is defined over a set of sub-intervals of a given interval. This set is called a partition.
• Definition: A partition of an interval [a,b] is a sequence of points between a and b such that
a = t0 < t1 < … < tn = b
The numbers ti (i = 1 to n-1) are called knots
niit 0}{
Linear Spline
• Definition: A function S is a linear spline on [a,b] if – The domain of S is [a,b]– S is continuous on [a,b]– There is a partition of points on [a,b] such
that S is a linear function on each sub-interval [ti , ti+1 ]
niit 0}{
Linear Spline
• A linear spline is defined by its values at the set of knots. Given the table of values
there is a unique linear spline with those values. • On each sub-interval, [ti , ti+1 ] , the linear spline is
defined by a linear function:
Then, S(ti)= yi and S(ti+1) = yi+1 (verify this)
Linear Spline
• Example:
Linear Spline
• Piece-Wise Linear Formula for Spline:
• Note: The fractions here are just the first divided differences for the data.
nnnnn
nnn txttx
tt
yyy
txttxtt
yyy
txttxtt
yyyxS
11
1
11
21112
121
10001
010
),(
),(
),()(
Matlab Linear Spline Function
function v = piecelin2(x,y,u)%Piecewise linear interpolation. (Slightly modified from Moler text)% v = piecelin2(x,y,u) finds the piecewise linear value of S(x)% with S(x(j)) = y(j) and returns v = S(u).% First divided difference – “diff” is a built-in Matlab command delta = diff(y)./diff(x);% Find subinterval index k so that x(k) <= u < x(k+1) n = length(x); k = 1; for j = 2:n-1 if x(j) <= u; k = j; end end% Evaluate spline at u s = u - x(k); v = y(k) + s*delta(k);
Matlab Linear Spline Example
% x,y data in vector formx = 1:6;y = [16 18 21 17 15 12];% A series of values along the x-axisu = 1.0:.05:6.0;[m n] = size(u);% polyvals stores the linear spline values for the u-valuessplinevals = zeros(n);for i = 1:n % Compute each polynomial value splinevals(i) = piecelin2(x,y,u(i));end% Plot the x-y data as circles ('o') and the polynomial data as '-'plot(x,y,'o',u,splinevals,'-')
Matlab Linear Spline Example
Quadratic Spline
• Definition: A function Q is a quadratic spline on [a,b] if – The domain of Q is [a,b]–Q is continuous on [a,b]–Q’ is continuous on [a,b]– There is a partition of points on [a,b]
such that Q is a polynomial of degree <= 2 on each sub-interval [ti , ti+1 ]
niit 0}{
Quadratic Spline
• Example:
• Class Exercise: Verify that Q satisfies all parts of the definition of a quadratic spline.
Formula for Quadratic Spline
• Need to find constants ai, bi, ci, such that
nnnnnnn txtctxbtxa
txtctxbtxa
txtctxbtxaxQ
11112
11
211112
11
100002
00
,)()(
,)()(
,)()()(
Formula for Quadratic Spline
Formula for Quadratic Spline
• Need to find constants ai, bi, ci, such that
• Thus, we need to determine 3n different constants.
nnnnnnn txtctxbtxa
txtctxbtxa
txtctxbtxaxQ
11112
11
211112
11
100002
00
,)()(
,)()(
,)()()(
Formula for Quadratic Spline
• We know that:– Qi (ti ) = yi
– Qi (ti+1 ) = yi+1
– Qi’ (ti+1 ) = Qi+1’ (ti+1 ) (Q’ is continuous at knots)
• This gives n + n + (n-1) =3n -1 equations for 3n unknowns! • Need one more condition on Q. Could use one of these– a0 = 0 (Q0 is a line)
– Q’(t0 ) = z0 (slope at start)
– Q’’(t0 ) = w0 (curvature at start)
• We will use the second condition.
Formula for Quadratic Spline
• Let zi = Qi’ (ti)
• Recall: Qi(x) = ai (x-ti)2 + bi (x-ti) + ci
Then, Qi (ti ) = yi -> ci = yi
Also, Qi’ (ti ) = zi -> bi = zi
• So, Qi(x) = ai (x-ti)2 + zi (x-ti) + yi
• Since Qi’(ti+1 ) = zi+1 we get 2ai(ti+1-ti) + zi = zi+1
• Thus, ai = (zi+1-zi)/2(ti+1-ti)
Formula for Quadratic Spline
• We then get a formula for Qi(x) in terms of data points ti and yi and the derivative values zi
• Now, z0 is given as data. How do we get other zi ?
iiiiii
iii ytxztx
tt
zzxQ
)()()(2
)( 2
1
1
Formula for Quadratic Spline
iiii
ii
iii ytxztx
tt
zzxQ
)()()(2
)( 2
1
1
Formula for Quadratic Spline
Algorithm: Given data (ti , yi) and z0 = derivative at Q(a), compute – 1) for i = 1, 2, …,n
– 2) iiiiii
iii ytxztx
tt
zzxQ
)()()(2
)( 2
1
1
Quadratic Spline
• Example: Data x = [0 1 4], y = [1 -2 1]• Create Matlab Program piecequad. This is
one of your homework problems. We add another input variable z0, so we have piecequad(x,y,z0,u)
• Output is Qi(u) for i = interval in which u lies. • Our example, get – Q0 (x)= -3x2 + 1 – Q1 (x) = 7/3 (x-1)2 – 7 (x-1) -2
(Verify this is correct!)
Quadratic Spline
• Example: Plot this using Matlab: x = [0 1 4];y = [1 -2 1];% A series of values along the x-axisu = 0.0:.05:4.0;[m n] = size(u);% polyvals stores the quadratic spline values for the u-valuessplinevals = zeros(n);for i = 1:n
splinevals(i) = piecequad(x,y,1,u(i));end% Plot the x-y data as circles ('o') and the polynomial data as '-'plot(x,y,'o',u,splinevals,'-')
Quadratic Spline
• Example: