Interpolation and Curve fitting -...

30
Interpolation and Curve fitting Spring 2019 Interpolation and Curve fitting Spring 2019 1 / 19

Transcript of Interpolation and Curve fitting -...

Page 1: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Interpolation and Curve fitting

Spring 2019

Interpolation and Curve fitting Spring 2019 1 / 19

Page 2: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Interpolation vs Curve fitting

Given some data points {xi , yi}ni=1 and assuming there is some functionf (x) describes the quantity of interest at all points.

Interpolation

Find a function satisfying

P(xi ) = f (xi ), i = 1, . . . , n

that allows us to approximate f (x) such that the function values betweenthe data sets may be estimated.

Curve fitting

Find a function that is a good fit to the original data points The functiondoes not have to pass through the original data points.

Interpolation and Curve fitting Spring 2019 2 / 19

Page 3: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Interpolation vs Curve fitting

Given some data points {xi , yi}ni=1 and assuming there is some functionf (x) describes the quantity of interest at all points.

Interpolation

Find a function satisfying

P(xi ) = f (xi ), i = 1, . . . , n

that allows us to approximate f (x) such that the function values betweenthe data sets may be estimated.

Curve fitting

Find a function that is a good fit to the original data points The functiondoes not have to pass through the original data points.

Interpolation and Curve fitting Spring 2019 2 / 19

Page 4: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Piecewise Linear Interpolation

Simplest case: 2 points

Basic algebra problem

Given two points (x1, f (x1)) and (x2, f (x2)), find P1(x) such that

P1(x1) = f (x1)

P2(x2) = f (x2)

P1 can be written in the form

P1(x) =

(x − x2

x1 − x2

)f (x1) +

(x − x1

x2 − x1

)f (x2)

Interpolation and Curve fitting Spring 2019 3 / 19

Page 5: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Interpolation in MATLAB interp1

vq = interp1(x,v,xq,method)

x – sample points v – values f (x)

xq – query points on which the polynomial will be evaluated

method – method of interpolation (e.g. linear, cubic, e.t.c)

Exercise: Interpolate 10 random data points with values on [0, 10] andevaluate the polynomial on 1:0.1:10 and plot.

x

1 2 3 4 5 6 7 8 9 10

y

1

2

3

4

5

6

7

8

9

Linear Interpolation

Interpolation and Curve fitting Spring 2019 4 / 19

Page 6: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Interpolation in MATLAB interp1

vq = interp1(x,v,xq,method)

x – sample points v – values f (x)

xq – query points on which the polynomial will be evaluated

method – method of interpolation (e.g. linear, cubic, e.t.c)

Exercise: Interpolate 10 random data points with values on [0, 10] andevaluate the polynomial on 1:0.1:10 and plot.

x

1 2 3 4 5 6 7 8 9 10

y

1

2

3

4

5

6

7

8

9

Linear Interpolation

Interpolation and Curve fitting Spring 2019 4 / 19

Page 7: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Higher order interpolation : Lagrange interpolant

Lagrange (1736 - 1813)Idea:

Given n + 1 data points{

(x1, f (x1)), (x2, f (x2)), . . . , (xn, f (xn)), (xn+1, f (xn+1))}

,define a polynomial

P(x) =n+1∑i=1

f (xj)ϕj(x)

where

ϕj(xi ) = δij =

{0 if j = i

1 if j 6= iInterpolation and Curve fitting Spring 2019 5 / 19

Page 8: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

What does ϕ(x) look like?

In the linear case:

P1(x) =

(x − x2

x1 − x2

)f (x1) +

(x − x1

x2 − x1

)f (x2)

Notice that:

P1(x1) =��

����*1(

x1 − x2

x1 − x2

)f (x1) +

������*0(

x1 − x1

x2 − x1

)f (x2) = f (x1)

P1(x2) =��

����*

0(x2 − x2

x1 − x2

)f (x1) +

����

��*1(

x2 − x1

x2 − x1

)f (x2) = f (x2)

Interpolation and Curve fitting Spring 2019 6 / 19

Page 9: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

What does ϕ(x) look like?

In the linear case:

P1(x) =

(x − x2

x1 − x2

)f (x1) +

(x − x1

x2 − x1

)f (x2)

Notice that:

P1(x1) =��

����*1(

x1 − x2

x1 − x2

)f (x1) +

������*0(

x1 − x1

x2 − x1

)f (x2) = f (x1)

P1(x2) =��

����*

0(x2 − x2

x1 − x2

)f (x1) +

����

��*1(

x2 − x1

x2 − x1

)f (x2) = f (x2)

Interpolation and Curve fitting Spring 2019 6 / 19

Page 10: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

What does ϕ(x) look like?

In the linear case:

P1(x) =

(x − x2

x1 − x2

)f (x1) +

(x − x1

x2 − x1

)f (x2)

Notice that:

P1(x1) =��

����*1(

x1 − x2

x1 − x2

)f (x1) +

������*0(

x1 − x1

x2 − x1

)f (x2) = f (x1)

P1(x2) =��

����*

0(x2 − x2

x1 − x2

)f (x1) +

����

��*1(

x2 − x1

x2 − x1

)f (x2) = f (x2)

Interpolation and Curve fitting Spring 2019 6 / 19

Page 11: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

What does ϕ(x) look like?

Therefore:P1(x) = ϕ1(x)f (x1) + ϕ2(x)f (x2)

where

ϕ1(x) =

(x − x2

x1 − x2

), ϕ2(x) =

(x − x1

x2 − x1

)

In the case of 3 data points{

(x1, f (x1)), (x2, f (x2)), (x3, f (x3))}

P2(x) = ϕ1(x)f (x1) + ϕ2(x)f (x2) + ϕ3(x)f (x3)

where:

ϕ1(x) =(x − x2)(x − x3)

(x1 − x2)(x1 − x3), ϕ2(x) =

(x − x1)(x − x3)

(x2 − x1)(x2 − x3), ϕ3(x) =

(x − x1)(x − x2)

(x3 − x1)(x3 − x2)

In the general case, given n + 1 data points we can write down theLagrange interpolating polynomial of degree n of this form

Interpolation and Curve fitting Spring 2019 7 / 19

Page 12: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

What does ϕ(x) look like?

Therefore:P1(x) = ϕ1(x)f (x1) + ϕ2(x)f (x2)

where

ϕ1(x) =

(x − x2

x1 − x2

), ϕ2(x) =

(x − x1

x2 − x1

)

In the case of 3 data points{

(x1, f (x1)), (x2, f (x2)), (x3, f (x3))}

P2(x) = ϕ1(x)f (x1) + ϕ2(x)f (x2) + ϕ3(x)f (x3)

where:

ϕ1(x) =(x − x2)(x − x3)

(x1 − x2)(x1 − x3), ϕ2(x) =

(x − x1)(x − x3)

(x2 − x1)(x2 − x3), ϕ3(x) =

(x − x1)(x − x2)

(x3 − x1)(x3 − x2)

In the general case, given n + 1 data points we can write down theLagrange interpolating polynomial of degree n of this form

Interpolation and Curve fitting Spring 2019 7 / 19

Page 13: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

What does ϕ(x) look like?

Therefore:P1(x) = ϕ1(x)f (x1) + ϕ2(x)f (x2)

where

ϕ1(x) =

(x − x2

x1 − x2

), ϕ2(x) =

(x − x1

x2 − x1

)

In the case of 3 data points{

(x1, f (x1)), (x2, f (x2)), (x3, f (x3))}

P2(x) = ϕ1(x)f (x1) + ϕ2(x)f (x2) + ϕ3(x)f (x3)

where:

ϕ1(x) =(x − x2)(x − x3)

(x1 − x2)(x1 − x3), ϕ2(x) =

(x − x1)(x − x3)

(x2 − x1)(x2 − x3), ϕ3(x) =

(x − x1)(x − x2)

(x3 − x1)(x3 − x2)

In the general case, given n + 1 data points we can write down theLagrange interpolating polynomial of degree n of this form

Interpolation and Curve fitting Spring 2019 7 / 19

Page 14: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Lagrange interpolating polynomial

x

1 2 3 4 5 6 7 8 9 10

y

-15

-10

-5

0

5

10

15

Lagrange Interpolant

Disadvantage

As the polynomial degree increases, the error near the endpointsincreases (MA 427)

Interpolation and Curve fitting Spring 2019 8 / 19

Page 15: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Cubic spline interpolants

A cubic spline interpolant is a piecewise cubic that interpolates thefunction f at x1, x2, x3, . . . , xn and has two continuous derivatives.Why Splines?

Useful in applications that require smooth surfaces, e.g. design ofairplane parts or cars.

x

1 2 3 4 5 6 7 8 9 10

y

1

2

3

4

5

6

7

8

9

Cublic spline Interpolation

Interpolation and Curve fitting Spring 2019 9 / 19

Page 16: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Least Squares curve fitting

A least squares curve fit can be used to obtain a curve such that thesquared distance from each point to the curve is minimized.

The curve can be1 Polynomials of degree n2 Trigonometric3 Exponential

Interpolation and Curve fitting Spring 2019 10 / 19

Page 17: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Least Squares curve fitting

A least squares curve fit can be used to obtain a curve such that thesquared distance from each point to the curve is minimized.

The curve can be1 Polynomials of degree n2 Trigonometric3 Exponential

Interpolation and Curve fitting Spring 2019 10 / 19

Page 18: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Linear Regression

Given N data points (xk , yk), k = 1, . . . ,N we seek a linef (x) = mx + b so that the mean square error (MSE):

MSE (m, b) =1

N

N∑k=1

(yk − f (xk)

)2

=1

N

N∑k=1

(yk − (mxk + b)

)2

We can compute partial derivatives with respect to m and b to findvalues of m and b that minimize the error.

Interpolation and Curve fitting Spring 2019 11 / 19

Page 19: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Polynomial Regression

Definef (x) = a1x

n + a2xn−1 + · · ·+ anx + an+1

that fits the data.

In MATLAB, the function polyfit performs polynomial regression

Usage:

a = polyfit(X,Y,N)

1 a - row vector of coefficients of the regression, i.e.2 X and Y - supplied data points3 N - polynomial degree

a(1)*X^N + a(2)*X^(N-1)+. . .+ a(N)*X + a(N+1)

To evaluate the polynomial, a at any query points xq

Y = polyval(a,xq)

Interpolation and Curve fitting Spring 2019 12 / 19

Page 20: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Polynomial Regression

Definef (x) = a1x

n + a2xn−1 + · · ·+ anx + an+1

that fits the data.

In MATLAB, the function polyfit performs polynomial regression

Usage:

a = polyfit(X,Y,N)

1 a - row vector of coefficients of the regression, i.e.2 X and Y - supplied data points3 N - polynomial degree

a(1)*X^N + a(2)*X^(N-1)+. . .+ a(N)*X + a(N+1)

To evaluate the polynomial, a at any query points xq

Y = polyval(a,xq)

Interpolation and Curve fitting Spring 2019 12 / 19

Page 21: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Polynomial Regression

Definef (x) = a1x

n + a2xn−1 + · · ·+ anx + an+1

that fits the data.

In MATLAB, the function polyfit performs polynomial regression

Usage:

a = polyfit(X,Y,N)

1 a - row vector of coefficients of the regression, i.e.2 X and Y - supplied data points3 N - polynomial degree

a(1)*X^N + a(2)*X^(N-1)+. . .+ a(N)*X + a(N+1)

To evaluate the polynomial, a at any query points xq

Y = polyval(a,xq)

Interpolation and Curve fitting Spring 2019 12 / 19

Page 22: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Linear fit

x

1 2 3 4 5 6 7 8 9 10

y

1

2

3

4

5

6

7

8

9

Linear fit

Interpolation and Curve fitting Spring 2019 13 / 19

Page 23: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Quadratic fit

x

1 2 3 4 5 6 7 8 9 10

y

1

2

3

4

5

6

7

8

9

Quadratic fit

Interpolation and Curve fitting Spring 2019 14 / 19

Page 24: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Exercise

Generate the following plot for x =0:5; and y=[0 10 25 36 52 59];

0 1 2 3 4 5

x

-10

0

10

20

30

40

50

60

70

y

Linear fit

0 1 2 3 4 5

x

-10

0

10

20

30

40

50

60

70

y

Quadratic fit

0 1 2 3 4 5

x

-10

0

10

20

30

40

50

60

70

y

Fifth degree fit

0 1 2 3 4 5

x

-20

0

20

40

60

80

100

120

140

y

Tenth degree fit

Interpolation and Curve fitting Spring 2019 15 / 19

Page 25: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Interpolation in 2D

Vq = interp2(X,Y,V,Xq,Yq,method)

Returns Vq, the values of a function of two variables at query pointsXq,Yq

The default method is linear interpolation

Other methods include1 cubic2 spline

Interpolation and Curve fitting Spring 2019 16 / 19

Page 26: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Interpolation in 2D

Vq = interp2(X,Y,V,Xq,Yq,method)

Returns Vq, the values of a function of two variables at query pointsXq,Yq

The default method is linear interpolation

Other methods include1 cubic2 spline

Interpolation and Curve fitting Spring 2019 16 / 19

Page 27: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Interpolation in 2D

Vq = interp2(X,Y,V,Xq,Yq,method)

Returns Vq, the values of a function of two variables at query pointsXq,Yq

The default method is linear interpolation

Other methods include1 cubic2 spline

Interpolation and Curve fitting Spring 2019 16 / 19

Page 28: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Sampling and interpolating a Gaussian distribution

Z = peaks(X,Y) – evaluates the peaks function at X and Y.We can use Z as data points and visualize interpolation using variousmethods

42

0

Original sample

-2-4-5

0

0

10

5

-5

5

42

Linear Interpolation using fine grid

0-2

-4-5

0

5

10

0

-5

5

42

Cubic Interpolation using fine grid

0-2

-4-5

0

-5

10

5

0

5

42

Spline Interpolation using fine grid

0-2

-4-5

0

10

5

0

-5

5

Interpolation and Curve fitting Spring 2019 17 / 19

Page 29: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Interpolation and image manipulation

Starting with a random image pic = rand(10,10);

Interpolate the image using 64 times as many points in each direction.

Interpolation and Curve fitting Spring 2019 18 / 19

Page 30: Interpolation and Curve fitting - math.loyola.edumath.loyola.edu/~chidyagp/sp19/interpolation.pdf · Interpolation in MATLAB interp1 vq = interp1(x,v,xq,method) x { sample points

Extrapolation

DefinitionMaking estimates beyond the obsevation range.

1D – vq = interp1(x,v,xq,method,extrapolation) allows xq tocontain points outside the range of x.

2D – q = interp2(X,Y,V,Xq,Yq,method,extrapval) allows for ascalar value extrapval to be assigned to all queries outside thedomain of sample points.

Interpolation and Curve fitting Spring 2019 19 / 19