Introduction to Computer Programming with MATLAB
CEE/MAE M20
Lecture 5b
Numerical Interpolation and Differentiation
CEE/MAE M20
Numerical Interpolation
Suppose we’re given as discrete set of data
2
x
0
0.5
1
1.5
2
2.5
y
2
1.5
1
0.5
0
0.5
1
1.5
2
Could be, for example
•
Measurement data
•
Evaluations of a function
•
Entries in a table
CEE/MAE M20
We’d like to ‘fill in the gaps’ between the discrete points. There
are many ways to do this.
3
CEE/MAE M20
We’d like to ‘fill in the gaps’ between the discrete points. There
are many ways to do this.
For example,
linear interpolation
4
x
0
0.5
1
1.5
2
2.5
y
2
1.5
1
0.5
0
0.5
1
1.5
2
CEE/MAE M20
We’d like to ‘fill in the gaps’ between the discrete points. There
are many ways to do this.
For example,
shapepreserving cubic interpolation
5
x
0
0.5
1
1.5
2
2.5
y
2
1.5
1
0.5
0
0.5
1
1.5
2
Slopes
are determined such that
the function values do not
overshoot the data values, at least
locally.
The resulting interpolant has
secondorder continuity.
CEE/MAE M20
We’d like to ‘fill in the gaps’ between the discrete points. There
are many ways to do this.
For example,
cubic spline interpolation
6
x
0
0.5
1
1.5
2
2.5
y
2
1.5
1
0.5
0
0.5
1
1.5
2
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
CEE/MAE M20
•
Each of these uses
piecewise polynomials
between neighboring
points. This is the most common interpolation approach.
•
Here, we’ll discuss two approaches and implement one of them
•
Linear interpolation
•
Shapepreserving piecewise cubic Hermite interpolation (‘PCHIP’)
•
Another approach would be to find a single polynomial that
passes through all of the points (a
curve fit
).
•
A curve fit is generally a
bad approach
, especially when there
are more than 2 or 3 points to fit.
7
CEE/MAE M20
Linear interpolation
You probably already know how to do this
8
In the interval
we form a linear polynomial
P
(
x
) =
y
k
+
y
k
+1

y
k
x
k
+1

x
k
(
x

x
k
)
x
k
≤
x < x
k
+1
This ensures that
P
(
x
k
) =
y
k
P
(
x
k
+1
) =
y
k
+1
The points
x
1
,
x
2
, …
x
k
,
…,
x
n
are called the
nodes
y
k
y
k+1
y
k1
x
k
x
k+1
x
k1
y
k+2
x
k+2
P(x)
x
CEE/MAE M20
Let’s define some quantities that will make this a bit tidier
9
y
k
y
k+1
y
k1
x
k
x
k+1
x
k1
y
k+2
x
k+2
x
δ
k
Divided difference
δ
k
=
y
k
+1

y
k
x
k
+1

x
k
h
k
Interval length
h
k
=
x
k
+1

x
k
Also the slope of the line
Local variable
s
s
=
x

x
k
P
(
x
) =
y
k
+
δ
k
s
So we can write our linear polynomial as
P(x)
CEE/MAE M20
We wish to construct a linear interpolation function,
LinInterp
Here is the specification:
Given an array of points
x
with an associated array of data values
y
, linearly
interpolate at a point
xi
to obtain the value
yi
of the polynomial at
xi
10
x
y
xi
yi
LinInterp
CEE/MAE M20
The pseudocode for
LinInterp
11
Check that x and y are the same length
Check that xi falls inside the range of x
Find the value of k such that x(k)
≤
xi < x(k+1)
Compute s = xi
−
x(k)
Compute h = x(k+1)
−
x(k)
Compute del = (y(k+1)
−
y(k))/h
Compute yi = y(k) + del*s
CEE/MAE M20
The pseudocode for
LinInterp
12
Set k = 1
Set n = length(x)
while
k < n
−
1 and xi
≥
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '06
 ArunPrakash
 Computer Science, Numerical Analysis, Computer Programming, Yi, Polynomial interpolation, Spline interpolation, CEE/MAE M20

Click to edit the document details