Sie sind auf Seite 1von 8

Cubic Spline Interpolation

MAE 5093
Charles ONeill
28 May 2002
Abstract
A cubic spline routine as !e"elope! #or une$ually space! se$uential !ata points% Cubic spline
theory is re"iee!% A &isual 'asic co(puter pro)ra( in E*cel as create! to #it a spline to input
!ata points% +hree testcases are use! to "ali!ate the routine% Conclusions re)ar!in) the cubic
spline routine are (a!e%
Introduction
+he ob,ecti"e is to #it a cubic spline to !ata points% A typical cur"e #it in"ol"es #or(in) one
e$uation throu)h all n points% In contrast- a spline alloin) each se)(ent to ha"e a uni$ue
e$uation hile still constrainin) the cur"e #it to the !ata properties%
+his paper ill !e"elop the )o"ernin) e$uations #or a cubic spline% A co(puter i(ple(entation
usin) &isual 'asic ill be presente!% +hree testcases ill "ali!ate the spline (etho! an! the
co(puter co!e% .inally- conclusions ill be !iscusse!%
Theory
Spline theory is si(ple% O"er n inter"als- the routine #its n e$uations sub,ect to the boun!ary
con!itions o# n+1 !ata points% +he !eri"ations o# /illey012 an! 3heatly022 are use!% +he
!eri"ation assu(es a #unctional #or( #or the cur"e #it% +his e$uation #or( is si(pli#ie! an! then
sol"e! #or the cur"e #it e$uation%
+he assu(e! #or( #or the cubic polyno(ial cur"e #it #or each se)(ent is-
( ) ( ) ( )
i i i
2
i i
3
i i
! * * c * * b * * a y + + +
here the spacin) beteen successi"e !ata points is
i i i
x x h
+1
+he cubic spline constrains the #unction "alue- 1
st
!eri"ati"e an! 2
n!
!eri"ati"e% +he routine (ust
ensure that
4 * 5 y
-
4 * 5 y
an!
4 * 5 y
are e$ual at the interior no!e points #or a!,acent se)(ents%
Substitutin) a "ariable S #or the polyno(ials secon! !eri"ati"e re!uces the nu(ber o# e$uations
#ro( a, b, c, d #or each se)(ent to only S #or each se)(ent%
.or the i
th
se)(ent- the S )o"ernin) e$uation is-
( )

,
_

+ + +

+
+
1 i
1 i i
i
i 1 i
1 i i i i 1 i 1 i 1 i
h
y y
h
y y
6 S h S h 2 h 2 S h
In (atri* #or(- the )o"ernin) e$uations re!uce to a tri7!ia)onal #or(%
( )
( )
( )
1
1
1
1
1
1
]
1

1
1
1
1
]
1

1
1
1
1
]
1

+
+
+

2
2 1
1
1
1
1 2
2
2 3
1
2
1 2 2
2
3 2 2
2 2 1
6
2
2
2
n
n n
n
n n
n
i
n n n
n
h
y y
h
y y
h
y y
h
y y
S
S
S
h h h
h
h h h
h h h



S
1
an! S
n
are 8ero #or the natural spline boun!ary con!ition% I# !i##erent boun!ary con!itions are
nee!e!- the appropriate chan)es can be (a!e to the )o"ernin) e$uations%
.inally- the cubic spline properties are #oun! by substitutin) into the #olloin) e$uations% +hese
a, b, c an! d "alues correspon! to the polyno(ial !e#inition #or each se)(ent%
( )
i i
i i i i
i
i i
i
i i
i i i i
y d
S h S h
h
y y
c
S b
h S S a


+ +
+
6
2
2 9
6 9
1 1
1
Solution Method
A &isual 'asic pro)ra( as ritten in E*cel to #it a cubic spline as !escribe! in the +heory
section% +he pro)ra( is liste! in the Appen!i*% +he )eneral pro)ra( steps are )i"en belo%
1% Problem Initialization +he pro)ra( initiali8es the "ariables%
2% Read in Data Values +he !ata "alues are rea! an! the in!i"i!ual inter"als are calculate!%
3% Determine S matrices +he in#luence coe##icient "alues #or the S (atri* are !eter(ine!
#or a natural spline% +he constant (atri*- C- is !eter(ine!%
:% Matrix Solver A )eneric +ri7;ia)onal7Matri*7Al)orith( 5+;MA4 sol"er !eter(ines the
S "alue at each inter"al%
5% Calculate Cubic Parameters +he cubic para(eters a, b, c an! d are calculate! at each
inter"al #ro( S an! h%
6% Write out +he pro)ra( rites out the polyno(ial speci#ication ter(s a, b, c an! d%
Results
+he cubic spline cur"e #it routine as "ali!ate! ith three testcases% +he #irst is an e$ually
space! polyno(ial #unction% Ne*t- an une$ually space! e*ponential #unction is #it% .inally- the
cubic spline routine is teste! on a bump #unction% +he results are co(pare! ith <non e*a(ples
here possible%
Equal Data Spacing
+his testcase #its a cubic spline to 8 * 4 * 5 #
3
alon) the * inter"al #ro( 0 to :% +his proble(
is sol"e! as an e*a(ple in /illeys012 notes% +he cubic spline routine !eter(ines the sa(e S
"alues as the e*a(ple% .i)ure 1 shos a plot o# the #unction an! the cubic spline%
-10
0
10
20
30
40
50
60
0 1 2 3 4 5
Data Points
Cubic Spline
Analytic
8 * 4 * 5 #
3

Figure 1. E$ual Spacin)
+he spline as #it to 5 !ata points% +he spline is natural- S e$uals 8ero at the en!s% O"erall #it is
)oo! e*cept beteen * "alues o# 3 an! :% +his !i##erence is cause! by the natural spline
boun!ary con!itions at *=0 an! :% Chan)in) the spline to re#lect the correct 2
n!
!eri"ati"e at *=:
oul! help the #it% Also- the a, b, c an! d para(eters oul! e*actly reco"er the polyno(ial
#unction%
Unequal Data Spacing
+his testcase #its a cubic spline to
2 *
* e 2 4 * 5 # ith une$ual !ata spacin)% >eral! an!
3heatly022 sol"e the proble( in E*a(ple 3%6% +he cubic spline routine #in!s the sa(e S- a- b- c
an! ! para(eters as >eral! an! 3heatly% .i)ure 2 shos a plot o# the #unction an! the cubic
spline%
Notice that the spline creates a )oo! #it beteen #or se)(ents ith 2
n!
!eri"ati"es near 8ero% +he
thir! se)(ent 5*= 1%5 to 2%254 has the orst #it because o# the natural spline boun!ary con!ition
at point :% +his is the sa(e 2
n!
!eri"ati"e proble( as !iscusse! in the e$ual spacin) testcase%
0
5
10
15
0 0.5 1 1.5 2 2.5
Data Points
Cubic Spline
Analytic
2 *
* e 2 4 * 5 #
Figure . ?ne$ual Spacin)
Bump Function
+he bu(p testcase #its a cubic to
10
4 * cos5 4 * 5 # ith se"en !ata points% >eral!022 use! this
#unction to illustrate proble(s ith other interpolation (etho!s% .i)ure 3 shos the #unction an!
the cubic spline #it%
0
0.5
1
-3 -2 -1 0 1 2 3
Data Points
Cubic Spline
Analytic
10
4 * cos5 4 * 5 #
Figure !. 'u(p +estcase
&isually- the cubic spline !ips belo into ne)ati"e "alues beteen *= @2 to @1 an! *= 1 to 2%
+he actual #unction re(ains positi"e% It appears that the best #it occurs in the (i!!le% +his (a<es
sense because the en! points are less constraine! to the actual #unction hen co(pare! to the
(i!!le points%
Conclusions
A cubic spline cur"e #it routine as success#ully i(ple(ente!% A cur"e #it pro)ra( as ritten
ith &isual 'asic insi!e E*cel% +hree testcases ere use! to "ali!ate the cur"e #it routine%
+he results sho that the cubic spline pro"i!es an a!e$uate cur"e #it #or (ost !ata sets% Aroble(s
occurre! at the en! se)(ents because o# !i##erences beteen the natural spline boun!ary
con!ition an! the #unctions actual 2
n!
!eri"ati"e% Inclu!in) 2
n!
!eri"ati"e in#or(ation #or the
en!points oul! !oubtlessly i(pro"e the cur"e #it%
A!!itionally- the spline (etho! also allos #or har(onic solutions by #orcin) the sa(e #unction
properties onto the en! points% &ariations on this har(onic (etho! oul! be nee!e! #or
un<non perio! oscillations or si(ultaneous (ulti7(o!e !ata points% .ar #ro( the en! points-
e"en the current co!e oul! yiel! )oo! cur"e #its #or har(onic !ata points%
+he cubic spline is an easy to i(ple(ent cur"e #it routine% 'ecause the (etho! in"ol"es
connectin) in!i"i!ual se)(ents- the cubic spline a"oi!s oscillation proble(s in the cur"e #it%
O"erall- the cubic spline pro"i!es a )oo! cur"e #it #or arbitrary !ata points%
References
012 /illey- ;% >%- Numerical Methods- Stillater- OB- 2002% NO+ A&AI/A'/E
022 >eral!- C%- an! 3heatley- A%- Applied Numerical Analysis- A!!ison73esley- 199:%
Comments and Thanks (July 2008)
Special than<s to C% Selo"er an! ;r% M% Mai*ner #or #in!in) an! correctin) typos an! bu)s%
.or a #or)otten pro,ect in a lon)7a)o su((er nu(erical (etho! course- this pro,ect has attracte!
a surprisin) nu(ber o# co((ents%
Alease in"est in a )oo! nu(erical (etho!s boo<% +he /illey012 re#erence as a series o# class
notes hich are not currently a"ailable% I su))est that you either use the >eral! re#erence or buy
/illeys #orthco(in) boo<% Ca((in)s Nu(erical Metho!s publishe! by ;o"er is nice% Another
reasonable re#erence is the Nu(erical Decipes series by Aress- et al%
&isual 'asic !e#ines the array S5104 ith ele(ents #ro( 0 to 10% .or S- I as only usin)
ele(ents 1 to 10- hich accounts #or the EShi#t to +;MA coor!inate syste(F co!e 5+he +;MA
(atri* in"ersion starts at ele(ent 04% +his in!e*in) (etho! is sub7opti(al%
Aendi!" Comuter Code
Option Explicit
' Cubic Spline
' Project 1 MAE 5093
' 5-28-02
' Copyright (c) 2002 Charles O'Neill
'
' Permission is hereby granted, free of charge, to any person
' obtaining a copy of this software and associated documentation
' files (the !oftware), to deal in the !oftware without
' restriction, including without limitation the rights to use,
' copy, modify, merge, publish, distribute, sublicense, and"or sell
' copies of the !oftware, and to permit persons to whom the
' !oftware is furnished to do so, sub#ect to the following
' conditions$
'
' %he abo&e copyright notice and this permission notice shall be
' included in all copies or substantial portions of the !oftware'
'
' %() !O*%+,-) .! P-O/.0)0 ,! .!, +.%(O1% +,--,N%2 O* ,N2 3.N0,
' )4P-)!! O- .5P6.)0, .NC610.N7 81% NO% 6.5.%)0 %O %() +,--,N%.)!
' O* 5)-C(,N%,8.6.%2, *.%N)!! *O- , P,-%.C16,- P1-PO!) ,N0
' NON.N*-.N7)5)N%' .N NO )/)N% !(,66 %() ,1%(O-! O- COP2-.7(%
' (O60)-! 8) 6.,86) *O- ,N2 C6,.5, 0,5,7)! O- O%()- 6.,8.6.%2,
' +()%()- .N ,N ,C%.ON O* CON%-,C%, %O-% O- O%()-+.!), ,-.!.N7
' *-O5, O1% O* O- .N CONN)C%.ON +.%( %() !O*%+,-) O- %() 1!) O-
' O%()- 0),6.N7! .N %() !O*%+,-)'
Sub Main()
'Data sizing
Dim x(10) As Double, y(10) As Double, norder As Integer
Dim h(10) As Double
Dim i As Integer, j As Integer
'Cubic sizing
Dim S(10) As Double
'TDMA sizing (share A,B,C,D with cubic)
Dim B(10) As Double, D(10) As Double, A(10) As Double, C(10) As Double, R As Double
Dim ntdma As Integer
'Setup Lagrange polynomial --------------------------------------------
'Read in data-point order
norder = ActiveSheet.Cells(3, 3)
'Read in Data Values
For i = 1 To norder
x(i) = ActiveSheet.Cells(4 + i, 3)
y(i) = ActiveSheet.Cells(4 + i, 4)
Next i
'Determine the width of the ith interval
For i = 1 To (norder - 1)
h(i) = x(i + 1) - x(i)
ActiveSheet.Cells(4 + i, 5) = h(i)
Next i
'Set S matrix influence coefficients for natural spline
For i = 2 To (norder - 1)
j = i - 1 'Shift to TDMA coordinate system
D(j) = 2 * (h(i - 1) + h(i))
A(j) = h(i) 'Ignore A(norder)
B(j) = h(i - 1) 'Ignore B(0)
Next i
'Set Constant Matrix C
For i = 2 To (norder - 1)
j = i - 1 'Shift to TDMA coordinate system
C(j) = 6 * ((y(i + 1) - y(i)) / h(i) - (y(i) - y(i - 1)) / h(i - 1))
Next i
' Max tdma length
ntdma = norder - 2
'TDMA -------------------------------------
' Upper Triangularization
For i = 2 To ntdma
R = B(i) / D(i - 1)
D(i) = D(i) - R * A(i - 1)
C(i) = C(i) - R * C(i - 1)
Next i
' Directly set the last C
C(ntdma) = C(ntdma) / D(ntdma)
' Back Substitute
For i = (ntdma - 1) To 1 Step (-1)
C(i) = (C(i) - A(i) * C(i + 1)) / D(i)
Next i
'End of TDMA -------------------------------
'Switch from C to S
For i = 2 To (norder - 1)
j = i - 1 'Shift from TDMA coordinate system
S(i) = C(j)
Next i
'End conditions
S(1) = 0
S(norder) = 0
'Calculate cubic ai,bi,ci and di from S and h
For i = 1 To (norder - 1)
A(i) = (S(i + 1) - S(i)) / (6 * h(i))
B(i) = S(i) / 2
C(i) = (y(i + 1) - y(i)) / h(i) - (2 * h(i) * S(i) + h(i) * S(i + 1)) / 6
D(i) = y(i)
Next i
'Write out S,a,b,c,d Values
For i = 1 To norder
ActiveSheet.Cells(4 + i, 7) = S(i)
ActiveSheet.Cells(4 + i, 8) = A(i)
ActiveSheet.Cells(4 + i, 9) = B(i)
ActiveSheet.Cells(4 + i, 10) = C(i)
ActiveSheet.Cells(4 + i, 11) = D(i)
Next i
End Sub
Sub plotter()
'Data sizing
Dim x(10) As Double, y(10) As Double, norder As Integer, nstep As Integer
Dim h(10) As Double
Dim i As Integer, j As Integer, step As Integer
Dim B(10) As Double, D(10) As Double, A(10) As Double, C(10) As Double
Dim xs As Double, ys As Double
'Read in data-point order
norder = ActiveSheet.Cells(3, 3)
'Read in Cubic properties
For i = 1 To norder
x(i) = ActiveSheet.Cells(4 + i, 3)
h(i) = ActiveSheet.Cells(4 + i, 5)
A(i) = ActiveSheet.Cells(4 + i, 8)
B(i) = ActiveSheet.Cells(4 + i, 9)
C(i) = ActiveSheet.Cells(4 + i, 10)
D(i) = ActiveSheet.Cells(4 + i, 11)
Next i
'Read in steps
nstep = ActiveSheet.Cells(18, 3)
'Determine and write out x,Y
step = 0
For i = 1 To (norder - 1) 'Discrete function step
For j = 1 To nstep
step = step + 1
xs = x(i) + (h(i) / nstep) * (j - 1)
ys = A(i) * (xs - x(i)) ^ 3 + B(i) * (xs - x(i)) ^ 2 + C(i) * (xs - x(i)) +
D(i)
ActiveSheet.Cells(step, 15) = xs
ActiveSheet.Cells(step, 16) = ys
Next j
Next i
End Sub

Das könnte Ihnen auch gefallen