Docsity

Natural Cubic Splines, Lecture Notes - Advanced Calculus, Study notes of Calculus

Toggle sidebar
bg1
Math 128A Computer Lab 5: Natural Cubic
Splines
Adrian Down
March 09, 2006
Section 1
Discussion
General algorithm (part a)
I use the matrix formulation of the natural cubic spline algorithm presented
in lecture on March 3rd. In this framework, a polynomial is created in
each interval between pairs of successive points that are to be interpolated.
The algorithm reduces the interpolation problem to solving a tri-diagonal
system to determine the values of the fist derivative of the cubic splines at
the interpolated points. With the values and first derivatives of the splines
at each interpolated point, the interpolating polynomials are created using
the Hermite basis polynomials. Further documentation appears in the code
below.
Fiberglass rod (part b)
I find that the interpolating polynomial does tend to 0 away from the origin,
as expected. The ratio of the value of the first derivative of the interpolating
polynomial at successive data points appears to be exactly equal (within the
numerical precision of MatLab) to the expected value of √3−2 near to the
origin. Towards the end of the interval, the ratio of successive slopes tends
away from √3−2. This divergent behavior is the result of imperfections
in the interpolating polynomial due to the finite interval over which the
interpolation is performed.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Natural Cubic Splines, Lecture Notes - Advanced Calculus and more Study notes Calculus in PDF only on Docsity!

Math 128A Computer Lab 5: Natural Cubic

Splines

Adrian Down

March 09, 2006

Section 1

Discussion

General algorithm (part a)

I use the matrix formulation of the natural cubic spline algorithm presented in lecture on March 3rd. In this framework, a polynomial is created in each interval between pairs of successive points that are to be interpolated. The algorithm reduces the interpolation problem to solving a tri-diagonal system to determine the values of the fist derivative of the cubic splines at the interpolated points. With the values and first derivatives of the splines at each interpolated point, the interpolating polynomials are created using the Hermite basis polynomials. Further documentation appears in the code below.

Fiberglass rod (part b)

I find that the interpolating polynomial does tend to 0 away from the origin, as expected. The ratio of the value of the first derivative of the interpolating polynomial at successive data points appears to be exactly equal (within the numerical precision of MatLab) to the expected value of

3 − 2 near to the origin. Towards the end of the interval, the ratio of successive slopes tends away from

3 − 2. This divergent behavior is the result of imperfections in the interpolating polynomial due to the finite interval over which the interpolation is performed.

Code

Spline algorithm (part a)

function [polyvals, yp, polyco] = spline(x,y,z) % Returns value of interpolating polynomial at points z, derivative values % af each point x, and coefficients of interpolating polynomial. % Takes mesh of points x, values y of function to be interpolated at each % point, and array z of points at which the polynomial is to be evaluated. % %"equation" refers to the matrix formulation of the spline problem %developed in lecture on March 3rd nt = size(x); n = max(nt); h = zeros(1,n-1); % x differences, x(i+1) - x(i) A = zeros(n,n); % matirx of coefficients on the left side of the equation B = zeros(n,n); % matrix of coefficients on the right side of the equation polyco = zeros(n-1,4); % matrix of coeffificents of the final interpolating polynomial. The ith % row gives the coefficients of the interpolating polynomial between x(i) % and x(i+1). Polynomial is of the form p(x) = polyco(i,1)x^3 + % polyco(i,2)x^2 + ... for i = 1:n- h(i) = abs(x(i+1) - x(i)); % Compute differences end A(1,1) = 2 ./ h(1); % construct coefficient matrices as shown in class A(n,n) = 2 ./ h(n-1); B(1,1) = -1 ./ h(1)^2; B(n,n) = 1 ./ h(n-1)^2; for i = 2:n- A(i,i) = 2 ./ h(i-1) + 2 ./ h(i); B(i,i) = 1 ./ h(i-1)^2 - 1 ./ h(i)^2; end for i = 1:n- A(i+1,i) = 1 ./ h(i); A(i,i+1) = 1 ./ h(i); B(i+1,i) = -1 ./ h(i)^2; B(i,i+1) = 1 ./ h(i)^2;

close all; plot(z,p); % plot the results hold on; title(’Interpolation of elastic rod’); axis([-5 5 -.5 1]); slope = zeros(1,60); % compute the ratio of slopes for i = 1: slope(i) = yp(i) / yp(i+1); end for i = 32: slope(i) = yp(i+1) / yp(i); end figure; n = size(slope); % plot the ratio of slopes xp = linspace(0,62,1000); yp = (sqrt(3)-2)*ones(1,1000); plot(xp,yp,’b’,1:29,slope(1:29),’r’,32:60,slope(32:60),’r’) hold on; title(’Ratio of derivatives’); axis([1 61 -.268 -.2679]) legend(’3^{1/2}-2’,’\gamma_{k+1} / \gamma_{k}’)

Results

slope =

Columns 1 through 4

Columns 5 through 8

Columns 9 through 12

−0.5−5 −4 −3 −2 −1 0 1 2 3 4 5

0

1

Interpolation of elastic rod

Columns 13 through 16

Columns 29 through 32

The ratio of slopes is symmetric about the point where y = 1.

% I wrote part 2 of lab 5 as a function that takes as input the nummber of % divisions of the interval z = linspace(0,2pi,10000); % points to evaluate the function; a plotting mesh x = 2pi(0:1/n:(1-1/n)); % interpolation mesh y = sin(x); px = spline(x,y,z); % find spline for x points y = sin(2x); py = spline(x,y,z); % find spline for y points close all; plot(px,py,’r’,sin(z),sin(2*z),’b’) % plot the results title([’Spline interpolation of x=sin(t), y=sin(2t) with n=’ int2str(n) ’ mesh poi

Results

−3 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−2.

−

−1.

−

−0.

0

1 Spline interpolation of x=sin(t), y=sin(2t) with n=6 mesh points

(a)

−1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−0.

−0.

−0.

−0.

0

1 Spline interpolation of x=sin(t), y=sin(2t) with n=15 mesh points

(b)

−1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−0.

−0.

−0.

−0.

0

1 Spline interpolation of x=sin(t), y=sin(2t) with n=40 mesh points

(c)

−1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−0.

−0.

−0.

−0.

0

1 Spline interpolation of x=sin(t), y=sin(2t) with n=60 mesh points

(d)

Results

(e)

(^00) 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

1

2

3

Adrian Down,s initials

(f)