0% found this document useful (0 votes)
107 views47 pages

Interpolation and Curve Fitting: Pierre Bézier

Wen-Chieh Lin presents a MATLAB assignment to solve a tridiagonal system. He discusses Polynomial interpolation Bezier curves and b-splines Least square approximation Robust curve fitting.

Uploaded by

y2j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views47 pages

Interpolation and Curve Fitting: Pierre Bézier

Wen-Chieh Lin presents a MATLAB assignment to solve a tridiagonal system. He discusses Polynomial interpolation Bezier curves and b-splines Least square approximation Robust curve fitting.

Uploaded by

y2j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Interpolation and

Curve Fitting

Pierre Bzier

De Casteljau construction of Bezier curve


From www.wikipedia.org

Administration
Assignment

2 is out; due on April 9 midnight


Chapter 2 Excises 17, 66, 81, 82 (20% each)
Chapter 2 Excises 35 (20%)
write

MATLAB code to solve a tridiagonal system

send

your code to TA
([email protected]), EC229b

Numerical Methods Wen-Chieh Lin

Outline
Overview

of interpolation problem
Polynomial interpolation
Bezier curves and b-splines
Least square approximation
Robust curve fitting

Numerical Methods Wen-Chieh Lin

Interpolation
Basic

problem: for given data

( xi , yi ),

i 1,, m

with x1 < x2 < < xm determine function


f:
such that

f ( xi ) yi ,

i 1,, m

f is interpolating function, or interpolant, for


given data

Numerical Methods Wen-Chieh Lin

Interpolation (cont.)
Additional

data might be specified, such as


slope of interpolation function at given points

Additional

constraints might be imposed, such


as smoothness, monotonicity, or convexity of
interpolation function

could be function of more than one variable


Numerical Methods Wen-Chieh Lin

Purposes of Interpolation
Plotting

smooth curve through discrete data

points
Reading

between lines of table

Differentiating

Replacing

or integrating tabular data

complicated function by simple one


Numerical Methods Wen-Chieh Lin

Interpolation vs Approximation
Interpolation function fits given data points
exactly
Interpolation is inappropriate if data points
subject to significant errors
Approximation is usually preferable for
smoothing noisy data

Numerical Methods Wen-Chieh Lin

Issues in Interpolation
There

are arbitrarily many functions


interpolating given data points

What

form should function have?

How should function behave between data points

Should

function inherit properties of data, such


as montonicity, convexity, or periodicity?

Are

parameters that define interpolating


function meaningful?
Numerical Methods Wen-Chieh Lin

Choosing Interpolant
Choice of function for interpolation based on
How

easy function is to work with

determining

its parameters
evaluating function
differentiating or integrating function
How

well properties of function match


properties of data to be fit (smoothness,
monotonicity, convexity, periodicity, etc.)
Numerical Methods Wen-Chieh Lin

Functions for Interpolation


Families

of functions commonly used for


interpolation include
Polynomials
Piecewise

polynomials
Trigonometric functions
Exponential functions
Rational functions
We

will focus on interpolation by polynomial


and piecewise polynomials for now
Numerical Methods Wen-Chieh Lin

10

Polynomial Interpolation
Simplest

type of interpolation uses


polynomials

Unique

polynomial of degree at most n-1


passes through n data points (xi, yi), i = 1, , n,
where xi are distinct

There

are many ways to represent or compute


polynomial, but in theory all must give same
result
Numerical Methods Wen-Chieh Lin

11

Lagrangian Polynomials
For

a cubic polynomial case,

P3 ( x )
( x x1 )( x x2 )( x x3 )
( x x0 )( x x2 )( x x3 )
y0
y1
( x0 x1 )( x0 x2 )( x0 x3 )
( x1 x0 )( x1 x2 )( x1 x3 )
( x x0 )( x x1 )( x x3 )
( x x0 )( x x1 )( x x2 )

y2
y3
( x2 x0 )( x2 x1 )( x2 x3 )
( x3 x0 )( x3 x1 )( x3 x2 )

Numerical Methods Wen-Chieh Lin

12

Error of Polynomial Interpolation


The

error term of polynomial interpolation can


be expressed as
f
()
E ( x ) ( x x0 )( x x1 ) ( x xn )
( n 1)!
( n 1)

the smallest interval contains {x, x0 , x1 ,, xn }


Not very useful for computing real error as f is
usually unknown
Good for interpretation of some effects in error
analysis
Numerical Methods Wen-Chieh Lin

13

Error of Polynomial Interpolation


Let

E ( x ) f ( x ) Pn ( x ) ( x x0 )( x x1 ) ( x xn ) g ( x )

Define

auxiliary function

W (t ) f (t ) Pn (t ) (t x0 )(t x1 ) (t xn ) g ( x )
n+2 zeros, x0, x1, , xn, and x
W
(t) has n+1 zeros if W is a C1 function
W
(t) has n zeros if W is a C2 function
and so on..
W(n+1)(t) has at least one zero between ( x0 , xn , x )
has

( n 1)

() 0

Numerical Methods Wen-Chieh Lin

14

Error of Polynomial Interpolation


W ( n 1) () 0
n 1

d
n 1
f (t ) Pn (t ) (t x0 )( t x1 ) (t xn ) g ( x )
t
dt

f ( n 1) () 0 ( n 1)! g ( x )
f ( n 1) ()
E ( x ) ( x x0 )( x x1 ) ( x xn )
( n 1)!

Numerical Methods Wen-Chieh Lin

15

Interpretation of Error Term


f
()
E ( x ) ( x x0 )( x x1 ) ( x xn )
( n 1)!
( n 1)

Interpolation

Error

works better for x within xi


s

is smaller if x is centered within xi

Interpolationoutside

(x0 ,xn), called


extrapolation, will have large errors than
interpolation
Numerical Methods Wen-Chieh Lin

16

Disadvantages of Polynomial Interpolation


Need

to re-compute the interpolation function


if adding or removing a data point

Divided-differences

method avoids this


problem using fewer arithmetic operations

Divided-differences

gives the same polynomial


as Lagrangian interpolation
Numerical Methods Wen-Chieh Lin

17

Divided Differences
Given

data points (xi, yi), i = 0,,n, divided


differences, denoted by f[], defined recursively
by
f [ x1 , x2 , xn ] f [ x0 , x1 , xn 1 ]
f [ x0 , x1 , xn ]
xn x0
Some examples:
f [ x0 ] y0 f 0
f [ x1 ] f [ x0 ] f1 f 0
f [ x0 , x1 ]

x1 x0
x1 x0

f [ x1 , x2 ] f [ x0 , x1 ]
f [ x0 , x1 , x2 ]
x2 Wen-Chieh
x0
Numerical Methods
Lin

18

Divided differences (cont.)


Pn ( x ) a0 ( x x0 )a1 ( x x0 )( x x1 )a2
( x x0 )( x x1 ) ( x xn 1 )an
Let

a0 f 0 f [ x0 ]

then Pn ( x0 ) a0 f 0

a1 f [ x0 , x1 ]
then Pn ( x1 ) a0 ( x1 x0 )a1

Let

f 0 f1
f 0 ( x1 x0 )
f1
x0 x1
Numerical Methods Wen-Chieh Lin

19

Divided differences (cont.)


Let

then

f [ x1 , x2 ] f [ x0 , x1 ]
a2 f [ x0 , x1 , x2 ]
x2 x0

Pn ( x2 ) a0 ( x2 x0 )a1 ( x2 x0 )( x2 x1 )a2
f1 f 0
f 0 ( x2 x0 )
x1 x0
f 2 f1 f1 f 0

x2 x1 x1 x0
( x2 x0 )( x2 x1 )
x2 x0
f 2
Numerical Methods Wen-Chieh Lin

20

Divided differences (cont.)


One

can show in similar fashion that


Pn(xi) = fi if ai = f[x0, x1, , xi]

We

can then rewrite


Pn ( x ) a0 ( x x0 )a1 ( x x0 )( x x1 )a2

( x x0 )( x x1 ) ( x xn 1 )an
Pn ( x ) f [ x0 ] ( x x0 ) f [ x0 , x1 ]

( x x0 )( x x1 ) f [ x0 , x1 , x2 ]
( x x0 )( x x1 ) ( x xn 1 ) f [ x0 ,, xn ]
Numerical Methods Wen-Chieh Lin

21

Example: Divided Differences

Numerical Methods Wen-Chieh Lin

22

Example: Divided Differences (cont.)

Pn ( x ) f [ x0 ] ( x x0 ) f [ x0 , x1 ]
( x x0 )( x x1 ) f [ x0 , x1 , x2 ]
( x x0 )( x x1 ) ( x xn 1 ) f [ x0 ,, xn ]
Numerical Methods Wen-Chieh Lin

23

Example: Divided Differences (cont.)

If only first four points are used,

P3 ( x ) 22.0 ( x 3.2)
8.400
( x 3.2)( x 2.7)
2.856
( x 3.2)( x 2.7)( x 1.0)( 0.528)
Numerical Methods Wen-Chieh Lin

24

Example: Divided Differences (cont.)

If 5th point is added

P4 ( x ) P3 ( x )
( x 3.2)( x 2.7)( x 1.0)( x 4.8)(0.256)
Numerical Methods Wen-Chieh Lin

25

Example: Divided Differences


for a Polynomial

Numerical Methods Wen-Chieh Lin

26

Identical Polynomials
Every

polynomial of degree n that has the


same value at n+1 distinct data points is
exactly the same
Proof
Suppose

there are two different polynomials


Their difference D(x) = Pn(x) Qn(x) is a
polynomial of at most degree n
P and Q match at all n+1 points D(x) = 0 at
these n+1 points, i.e. D(x) has n+1 distinct zeros
However, D(x) is of at most degree n n zeros at
most
D(x) should be identically zero
Numerical Methods Wen-Chieh Lin

27

Different Forms of
Polynomial Interpolation
Polynomials

of degree n that interpolate n+1


data points are identical

Their

error terms are also identical

Only

need to derive the error term for one


form of interpolating polynomial
Numerical Methods Wen-Chieh Lin

28

Error EstimationNext Term Rule


It

can be shown in Chap 5 that

En(x) = the value of the next term that would be


added to Pn(x)

Numerical Methods Wen-Chieh Lin

29

Function Differences Method


If x-values of data points are evenly spaced,
divided differences method can be expressed as
Pn (x )

f [ x0 ] ( x x0 ) f [ x0 , x0 h ]
( x x0 )( x x0 h ) f [ x0 , x0 h, x0 2h ]
( x x0 )( x x0 h ) ( x x0 ( n 1)h ) f [ x0 ,, x0 nh ]

Numerical Methods Wen-Chieh Lin

30

Definition of Function Differences


First-order

differences
f i f i 1 f i
Second-order differences
2
f i ( f i ) ( f i 2 f i 1 ) ( f i 1 f i )

f i 2 2 f i 1 f i
Binomial coefficients
nth-order differences
n(n 1)
n
f i f i n nf i n 1
f i n 2 f i
2!
i 0,, ( N n )
Numerical Methods Wen-Chieh Lin

31

f1 f 0 f 0
f [ x0 , x0 h ]

h
h
f [ x0 , x0 h, x0 2h ]

f 2 f1
h

f1 f 0
h

2h

f0
2
2h

f 2 2 f1 f 0

2
2h

f [ x0 , x0 h, x0 2h, x0 3h ]

f 3 2 f 2 f1
2h2

f 2 2 f1 f 0
2h2

3h

3 f 0
3
3! h
n
f0
f [ x0 , x0 h,, x0 ( n 1)h ] n
Numerical Methods
n! h Wen-Chieh Lin

32

Function Differences (cont.)


Pn (x )
f [ x0 ] ( x x0 ) f [ x0 , x0 h ]
( x x0 )( x x0 h ) f [ x0 , x0 h, x0 2h ]
( x x0 )( x x0 h ) ( x x0 ( n 1)h ) f [ x0 ,, x0 nh ]

f 0
f 0 ( x x0 )

h
f0
( x x0 )( x x0 h )

2
2! h
2

n f 0
( x x0 )( x x0 h ) ( x x0 ( n 1)h )
n! h n
Numerical Methods Wen-Chieh Lin

33

Function Differences (cont.)


f 0

Pn (x ) f 0 ( x x0 )
h
2 f 0
( x x0 )( x x0 h )

2
2! h
n f 0
( x x0 )( x x0 h ) ( x x0 ( n 1)h )
n! h n
x x0
s
h
s( s 1) 2
s( s 1) ( s (n 1)) n
f 0 sf 0
f 0
f0
2!
n!
Numerical Methods Wen-Chieh Lin

34

Example: Function Difference


s( s 1) 2
s( s 1)( s 2) 3
P( x ) f 0 sf 0
f0
f 0
2!
3!
s( s 1)
s( s 1)( s 2)
x 0
3
0.25s
1 .5
1.5 0.25s 0.25

2
6
0.5
3

Numerical Methods Wen-Chieh Lin

35

Interpolating Data with a High-Degree


Polynomial is Bad!

Often causes undesirable wiggling in flat region!

Original Function

Fitting with P2(x)

Fitting with P4(x)

Fitting with P6(x)

Numerical Methods
Wen-Chieh
Fitting
with P (x) Lin
8

36

Remedy to High-Degree Polynomial


Break

up an interval into subintervals


Employ piecewise polynomial interpolation

Numerical Methods Wen-Chieh Lin

37

Spline Curves
Linear

spline

Slope is discontinuous!

Cubic

spline

Numerical Methods Wen-Chieh Lin

38

Cubic Spline
gi-1(x)

x0

x1

xi-1

gi(x)

xi

gi+1(x)

xi+1

knots

xn-1

xn

For each segment

gi ( x ) ai ( x xi ) bi ( x xi ) ci ( x xi ) d i
3

Unique solution exists if 4 constraints are specified


solving a system of 4 equations for 4 unknowns
Need additional constraints for g0(x) and gn-1(x)
Numerical Methods Wen-Chieh Lin

39

Cubic Spline
gi-1(x)

x0

x1

xi-1

gi(x)

xi

xi+1

gi+1(x)

knots

xn-1

xn

gi ( x ) ai ( x xi )3 bi ( x xi ) 2 ci ( x xi ) d i
gi ( xi ) yi
Continuous position
gi ( xi 1 ) gi 1 ( xi 1 )
g 'i ( xi 1 ) g 'i 1 ( xi 1 )
Continuous slope

Continuous curvature

g ' 'i ( xi 1 ) g ' 'i 1 ( xi 1 )

Numerical Methods Wen-Chieh Lin

40

Sketch for Computing Spline Coefficients


Define

hi xi 1 xi
Si g"i ( xi ), i 1,..., n 1
Sn g"n 1 ( xn )

We
ll compute

coefficients, ai, bi, ci, di by

Representing

ai, bi, ci, di as Si


Forming a matrix equation by stacking constraint
equations of all segments

HS Y
Numerical Methods Wen-Chieh Lin

41

Computing di , ai and bi
gi ( x ) ai ( x xi )3 bi ( x xi ) 2 ci ( x xi ) d i
From

gi ( xi ) yi , we know d i yi

From

gi ( xi 1 ) gi 1 ( xi 1 ) , we have

ai ( xi 1 xi ) bi ( xi 1 xi ) ci ( xi 1 xi ) d i yi 1
3

Using

hi xi 1 xi and d i yi , we obtain

ai hi bi hi ci hi yi yi 1
3

Numerical Methods Wen-Chieh Lin

42

Computing di , ai and bi (cont.)


gi ( x ) ai ( x xi ) bi ( x xi ) ci ( x xi ) d i
3

g"i ( x ) 6ai ( x xi ) 2bi ,


Si g"i ( xi ) 2bi

From

g"i ( xi 1 ) g"i 1 ( xi 1 ) , we get

Since

Si 1 g"i 1 ( xi 1 ) g"i ( xi 1 ) 6ai hi 2bi


Hence

Si
bi
2

Si 1 Si
ai
6hi
Numerical Methods Wen-Chieh Lin

43

Computing ci
Substitute

so far into
Then

the relations for ai, bi, ci, di obtained


ai hi bi hi ci hi yi yi 1
3

solve for ci
Si 1 Si 3 Si 2
hi hi ci hi yi yi 1
6hi
2

yi 1 yi 2hi Si hi Si 1
ci

hi
6
Numerical Methods Wen-Chieh Lin

44

Relating S and h
gi ( x ) ai ( x xi ) bi ( x xi ) ci ( x xi ) d i
2
g 'i ( x ) 3ai ( x xi ) 2bi ( x xi ) ci
3

From

g 'i ( xi 1 ) g 'i 1 ( xi 1 ) , we know that


g 'i 1 ( xi ) g 'i ( xi )

Plug

x = xi into gi
(x)

g 'i ( xi ) 3ai ( xi xi ) 2 2bi ( xi xi ) ci ci


Similarly,

we have

22
g 'i 1 ( xi))
33aai
(
(
x
x

x
x
)

22bibi1(1x( x
xi
x1i)1
)
ci
c1i 1
i
11
i
i
i1
1)
i

3ai 1hi21 2bi 1hi 1 ci 1


Numerical Methods Wen-Chieh Lin

45

Relating S and h (cont.)


Equating

results obtained from previous slide


ci 3ai 1hi21 2bi 1hi 1 ci 1

Substituting

for a, b, c, d their relationship in


terms of S and y

yi 1 yi yi yi 1

hi 1Si 1 ( 2hi 1 2hi ) Si hi Si 1 6

h
i 1
i

6f [ xi , xi 1 ] f [ xi 1 , xi ]
Numerical Methods Wen-Chieh Lin

46

Matrix for Computing Cubic Spline Coefficients

h
0

2(h0 h1 )
h1
h1
2(h1 h2 )
h2

h2
2( h2 h3 ) h3

f [ x , x ] f [ x , x ]
1
2
0
1

f [ x2 , x3 ] f [ x1 , x2 ]

6 f [ x3 , x4 ] f [ x2 , x3 ]

f
[
x
,
x
]

f
[
x
,
x
]
n 2
n 1
n 1 n

hn 2

S0

S2

S3

Sn 1
2( hn 2 hn 1 ) hn 1

Sn

hi xi 1 xi
Si g"i ( xi ), i 1,..., n 1
Sn g"n 1 ( xn )
Numerical Methods Wen-Chieh Lin

47

You might also like