Professional Documents
Culture Documents
File 6
File 6
File 6
Mike Renfro
Outlines
Homework
Mike Renfro Programs for Natural Cubic Spline Interpolation
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Given a series of points (x0 , f (x0 )) (xn , f (xn )), nd a cubic equation for each of the n intervals that: passes through the endpoints (x , f (x )) at each end of the interval has continuous rst derivatives from one interval to the next has continuous second derivatives from one interval to the next has second derivatives at x0 and xn of exactly 0.
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
% Example 5.12 data x =[2 3 6.5 8 12]; f =[14 20 17 16 23]; n = length ( x ) -1;
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Method 1, p.395396
Equation 5.95 provides a system of n 1 equations describing the second derivative of the spline function at each knot:
f (xi 1 )(xi xi 1 ) + 2f (xi )(xi +1 xi 1 ) + f (xi +1 )(xi +1 xi ) =6 f (xi +1 ) f (xi ) f (xi ) f (xi 1 ) xi +1 xi xi xi 1 i = 1, 2, , n 1.
Since all the x and f (x ) values are known, the unknowns are the f (x ) values. This is a linear algebraic system of equations. Also, remember that f (x0 ) = 0 and f (xn ) = 0.
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
x2 x1
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
{p } = 6
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Notice that the rst row of the {p } vector uses the rst, second, and third f (x ) values, as well as the rst and second h values. The second row uses the second, third, and fourth f (x ) values, and also the second and third h values. We should be able to write a program to ll out all these matrices automatically.
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Note that MATLAB does not have a zeroth element of a vector; all vector locations start at 1. So x0 in math (the rst x value) is the same as x(1) in MATLAB.
Mike Renfro Programs for Natural Cubic Spline Interpolation
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
4.0000
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
We can easily create a for loop in MATLAB to ll out the {p } vector. The rst element of {p } is calculated using f (x0 ), f (x1 ), f (x2 ), h1 , and h2 . These correspond to f(1), f(2), f(3), h(1), and h(2) in MATLAB. For rows other than the rst, the h and f positions are simply incremented.
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
One version:
p = zeros (n -1 ,1); for i =2: n p (i -1)=6*(( f ( i +1) - f ( i ))/ h ( i ) -... ( f ( i ) - f (i -1))/ h (i -1)); end
Other variants are certainly possible if i looped from 1 to n-1, youd just have to shift the i, i-1, and i+1 expressions up to i+1, i, and i+2, respectively.
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
The [A] matrix is symmetric, and consists largely of zeros. Only the elements along the diagonal, and the elements immediately to the left or the right of the diagonal are lled. The i th element on the diagonal is built up from hi and hi +1 . The i th element immediately to the left or right of the diagonal are made up solely from hi +1 .
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Elements of matrices in MATLAB are referenced similarly to elements of vectors. Matrix elements can be selected by using two subscripts in parentheses; the rst subscript corresponds to the row of the matrix, and the second corresponds to the column.
Mike Renfro Programs for Natural Cubic Spline Interpolation
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
ypp = A \ p ;
The values in ypp represent f (x1 ) f (xn ). Remember that the natural cubic spline end conditions require that f (x0 ) and f (xn ) be exactly 0. To complete the list of f (x ) values, lets add zeros to the beginning and end of the ypp variable:
ypp =[0; ypp ; 0]
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
The above f (xi ) values can be substituted into Equation 5.93 fi (x ) = f (xi 1 ) + + (x xi 1 )3 (xi + x )3 + f (xi ) 6(xi + xi 1 ) 6(xi xi 1 ) f (xi 1 ) x i xi 1 f (xi 1 ) (xi x ) xi xi 1 6 f (xi ) xi xi 1 f (xi ) (x xi 1 ) xi xi 1 6
Mike Renfro Programs for Natural Cubic Spline Interpolation
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
For a given interval i , where i = 1 n and xi 1 x xi , fi (x ) = ai + bi x + ci x 2 + di x 3 This adds up to n cubic equations, with 4n unknowns. We need to nd a total of 4n independent equations to solve for those unknowns.
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
2 equations can be derived from the natural cubic spline end conditions (f (x0 ) = f (xn ) = 0): (2)c1 + (6x0 )d1 = 0 (2)cn + (6xn )dn = 0
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
we can establish a few patterns in the equations and the matrix version.
Mike Renfro Programs for Natural Cubic Spline Interpolation
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
2 x3 1 x 0 x0 0 2 x3 1 x 1 x1 1 0 0 0 0 0 0 0 0 . . .
0 0 1 x1 1 x2
2 x3 x1 1 2 x3 x2 2
0 0 0 0
a1 b1 c1 d1 . . .
y 0 y 1 y 1 = y2 . . .
On the rst and second row, column 1 has a 1 in it. On the third and fourth row, column 5 has a 1 in it. On the fth and sixth row, the 1 is in column 9, etc.
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
This code sets the col variable to values of 1, 5, 9, up to a limit not exceeding 4n. When col is 1, we insert a 1 into the (1, 1) element of A, and then insert a 1 into the (2, 1) element of A. At the end of the loop, we increment the row counter by 2 and continue. This loop automatically terminates at row 2n.
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
This code takes care of the upper rows of the x , x 2 , and x 3 values. Remember that the number x0 in math is the same as x(1) in MATLAB. So on the rst loop, we insert x(1), x(1)^2, and x(1)^3 into the rst row, columns 2, 3, and 4. These values correspond to 2 , and x 3 from the original equation. x 0 , x0 0
Mike Renfro Programs for Natural Cubic Spline Interpolation
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
This code takes care of the lower rows of the x , x 2 , and x 3 values, and all the coecients from the rst 2n equations are inserted now.
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
The last two equations, the natural cubic spline end conditions, applied to the c1 , d1 , cn , and dn variables:
A (4* n -1 ,3)=2; A (4* n -1 ,4)=6* x (1); A (4* n ,4* n -1)=2; A (4* n ,4* n )=6* x ( n +1);
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
2 6 6 6 6 6 6 6 6 6 6 6 6 6 [A ] = 6 6 6 6 6 6 6 6 6 6 6 6 4
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 3 0 0 0 0 0 0 1 0 0 0 0 0 0 0
4 9 0 0 0 0 0 0 6 0 0 2 0 0 2 0
8 27 0 0 0 0 0 0 27 0 0 18 0 0 12 0
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 3 61 2 0 0 0 0 1 1 0 0 0 0 0 0
0 0 9 42 1 4 0 0 0 0 6 13 0 2 2 0 0 0
0 0 27 5 274 8 0 0 0 0 27 3 126 4 0 18 39 0 0 0
0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0
1 62 8 0 0 0 1 1 0 0 0 0 0
0 0 0 0 42 1 4 64 0 0 0 13 16 0 2 2 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 8 12 0 0 1 0 0 0 0 0
0 0 0 0 0 0 64 144 0 0 16 0 0 2 0 2
3 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 5
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
The {p } Vector
Mike Renfro
{p } =
14 20 20 17 17 16 16 23 0 0 0 0 0 0 0 0
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Mathematics and Matrix Form Math-to-MATLAB Translation Filling Out the Matrix Equation Solution, Results
Mike Renfro
The Basics Program 1, Using Methods from p.395396 Program 2, Using Methods from p.393394 Homework
Homework
Continue with all problems assigned from Chapter 5. Have them all ready to turn in on Thursday, November 4.
Mike Renfro