You are on page 1of 3

Mathcad Tutorial on Curve Fitting - Cubic Spline

N 20 := x 0. 15 :=
i 0 N .. := f x ( ) sin 2. 5 x ( ) 1. 2 x + :=
vx
i
i x := vy
i
f vx
i
( ) :=
0 1 2 3
0
1
2
3
4
vy
vx
Cubic Spline Fit
vs cspline vx vy , ( ) := g x ( ) interp vs vx , vy , x , ( ) :=
vs pspline vx vy , ( ) := h x ( ) interp vs vx , vy , x , ( ) :=
vs lspline vx vy , ( ) := j x ( ) interp vs vx , vy , x , ( ) :=
0 1 2 3
0
2
4
6
8
vy
g x ( )
h x ( )
j x ( )
vx x ,
Middle East Technical University, METU, Ankara/TURKEY
1
Dr. Ender CEROLU
Mathcad Tutorial on Curve Fitting - Cubic Spline
Regression Fit by Linear Combination of Functions
F x ( )
sin 2. 5 x ( )
x
|

\
|
|
.
:= V linfit vx vy , F , ( ) := V
1
1. 2
|

\
|
|
.
= k x ( ) V F x ( ) :=
0 1 2 3
0
1
2
3
4
5
vy
k x ( )
vx x ,
F x ( )
sin 2. 2 x ( )
x
|

\
|
|
.
:= V linfit vx vy , F , ( ) := V
0. 934
1. 306
|

\
|
|
.
= l x ( ) V F x ( ) :=
0 1 2 3
0
2
4
6
vy
k x ( )
l x ( )
vx x ,
Differentiation and Integration of Data Sets
After cubic spline fitting we have an analytical equation for the function in terms of
3rd order polynomials, for which we can calculate the derivative or which we can
Middle East Technical University, METU, Ankara/TURKEY
2
Dr. Ender CEROLU
Mathcad Tutorial on Curve Fitting - Cubic Spline
p y
integrate.
0 1 2 3
2
0
2
4
Exact Derivative
Derivative from Spline Fit
x
f x ( )
d
d
x
g x ( )
d
d
x
0

2
x f x ( )
(
(
(
]
d 2. 1632833726 = Numerical Integration of Exact Function
0

2
x g x ( )
(
(
(
]
d 2. 1632800248 = Numerical Integration of Spline Fitted Function
Middle East Technical University, METU, Ankara/TURKEY
3
Dr. Ender CEROLU

You might also like