Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scientific Computing Linear and Quadratic Splines.

Similar presentations


Presentation on theme: "Scientific Computing Linear and Quadratic Splines."— Presentation transcript:

1 Scientific Computing Linear and Quadratic Splines

2 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.

3 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!

4 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.

5 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 = t 0 < t 1 < … < t n = b The numbers t i (i = 1 to n-1) are called knots

6 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 [t i, t i+1 ]

7 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, [t i, t i+1 ], the linear spline is defined by a linear function: Then, S(t i )= y i and S(t i+1 ) = y i+1 (verify this)

8 Linear Spline Example:

9 Linear Spline Piece-Wise Linear Formula for Spline: Note: The fractions here are just the first divided differences for the data.

10 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 % Evaluate spline at u s = u - x(k); v = y(k) + s*delta(k);

11 Matlab Linear Spline Example % x,y data in vector form x = 1:6; y = [16 18 21 17 15 12]; % A series of values along the x-axis u = 1.0:.05:6.0; [m n] = size(u); % polyvals stores the linear spline values for the u-values splinevals = 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,'-')

12 Matlab Linear Spline Example

13 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 [t i, t i+1 ]

14 Quadratic Spline Example: Class Exercise: Verify that Q satisfies all parts of the definition of a quadratic spline.

15 Formula for Quadratic Spline Need to find constants a i, b i, c i, such that

16 Formula for Quadratic Spline

17 Need to find constants a i, b i, c i, such that Thus, we need to determine 3n different constants.

18 Formula for Quadratic Spline We know that: – Q i (t i ) = y i – Q i (t i+1 ) = y i+1 – Q i ’ (t i+1 ) = Q i+1 ’ (t i+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 – a 0 = 0 (Q 0 is a line) – Q’(t 0 ) = z 0 (slope at start) – Q’’(t 0 ) = w 0 (curvature at start) We will use the second condition.

19 Formula for Quadratic Spline Let z i = Q i ’ (t i ) Recall: Q i (x) = a i (x-t i ) 2 + b i ( x-t i ) + c i Then, Q i (t i ) = y i -> c i = y i Also, Q i ’ (t i ) = z i -> b i = z i So, Q i (x) = a i (x-t i ) 2 + z i (x-t i ) + y i Since Q i ’(t i+1 ) = z i+1 we get 2a i (t i+1 -t i ) + z i = z i+1 Thus, a i = (z i+1 -z i )/2(t i+1 -t i )

20 Formula for Quadratic Spline We then get a formula for Q i (x) in terms of data points t i and y i and the derivative values z i Now, z 0 is given as data. How do we get other z i ?

21 Formula for Quadratic Spline

22 Algorithm: Given data (t i, y i ) and z 0 = derivative at Q(a), compute – 1) for i = 1, 2, …,n – 2)

23 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 Q i (u) for i = interval in which u lies. Our example, get – Q 0 (x)= -3x 2 + 1 – Q 1 (x) = 7/3 (x-1) 2 – 7 (x-1) -2 (Verify this is correct!)

24 Quadratic Spline Example: Plot this using Matlab: x = [0 1 4]; y = [1 -2 1]; % A series of values along the x-axis u = 0.0:.05:4.0; [m n] = size(u); % polyvals stores the quadratic spline values for the u-values splinevals = 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,'-')

25 Quadratic Spline Example:


Download ppt "Scientific Computing Linear and Quadratic Splines."

Similar presentations


Ads by Google