You are on page 1of 65

CURVE FITTING

LECTURE 3

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

MOTIVATION
Data is usually given as a discrete set of values of a continuous domain. One
usual problem that one faces in this situation is when asked to extract data when
no value is given in that point.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Curve Fitting
1) Generate a single curve that will follow the pattern taken as a group. Not all
points will be points on the curve.

2) Generate a single curve/or a series of curves that will contain each point of the
group.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Linear Regression Line


Fit a line, that will best approximate the data set.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Linear Regression Line

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Linear Regression Line

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Linear Regression Line

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Error of the Linear Regression LIne

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Regression Coefficient

is at times called the regression coefficient. The closer r it is to 1, the more linear
the data is. r =1 implies that the regression line totally contains all the data set.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Regression Coefficient
is at times called the regression coefficient. The closer r it is to 1, the more linear
the data is. r =1 implies that the regression line totally contains all the data set.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Regression Coefficient
is at times called the regression coefficient. The closer r it is to 1, the more linear
the data is. r =1 implies that the regression line totally contains all the data set.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Failure of Linear Regression

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Polynomial Regression
We can extend the technique we used in the linear case to derive a formula for
quadratic regression (and for higher order regression)
This involves solving a system of 3 equations and 3 unknowns.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Polynomial Regression
Find a quadratic formula that can fit the following data.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Polynomial Regression

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Polynomial Regression

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Polynomial Regression

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Interpolation
Given n+1 points, there exists only one nth degree polynomial that passes
through points. Interpolation is the process of identifying the specific polynomial
that passes through all of the n+1 points.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Interpolation

Linear
Interpolation: Given the formula for the line containing these two points
is:

Find the line connecting the following points: (1,2), (3, 5)

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Interpolation
Quadratic Interpolation: The standard form of a quadratic polynomial is:
:
This can be written to:
Where:

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Interpolation

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Interpolation (example)
Find the quadratic function that passes through the following points: -(2,-3), (1,2), (2,-7)

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Interpolation (example)
Find the quadratic function that passes through the following points: -(2,-3), (1,2), (2,-7)

Quadratic Interpolation: The standard form of a quadratic polynomial is:


:
This can be written to:
Where:

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

General Newton Interpolation

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

General Newton Interpolation

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

General Newton Interpolation

Newtons Divided Difference Interpolating Polynomial

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Lagrange Interpolating Polynomial


Simply a reformulation of the Newtons Polynomial that avoids the computation of
the divided differences.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Lagrange Interpolating
Polynomials

Linear version (n = 1):


Used for 2 points of data: (xo,f(xo)) and (x1,f(x1)),

x x0
x x1
f1 ( x )
f ( x0 )
f ( x1 )
x0 x1
x1 x0
Lo (x)

L1 ( x)

Lagrange Interpolating Polynomials


Second order version (n = 2):

x x1 x x2
f 2 ( x)
f ( x0 )
x0 x1 x0 x 2

x x0 x x2

f ( x1 )
x1 x0 x1 x 2

x x0 x x1

f ( x2 )
x2 x0 x2 x1

Lo ( x) , j 0

L1 ( x) , j 1

L2 ( x) , j 2

Lagrange Interpolating
Polynomials - Example
Use a Lagrange interpolating polynomial of
the first and second order to evaluate ln(2)
on the basis of the data:

x0 1
x1 4
x2 6

f ( x 0) ln(1) 0
f ( x1) ln( 4) 1.386294
f ( x 2) ln(6) 1.791760

Lagrange Interpolating Polynomials


Example (contd)
First order polynomial:

x x1
x x0
f 1( x)
f ( x 0)
f ( x1)
x 0 x1
x1 x 0
24
2 1
f 1(2)
0
1.386294 0.4620981
1 4
4 1

Lagrange Interpolating Polynomials


Example (contd)
Second order polynomial:

x x1 x x2
x 4 x 6
Lo ( x )

xo x1 xo x2 0 4 0 6
x xo x x2 x 0 x 6
L1 ( x )

x1 xo x1 x2 4 0 4 6
x xo x x1 x 0 x 4
L2 ( x )

x2 xo x2 x1 6 0 6 4

Lagrange Interpolating Polynomials


Example (contd)
n

f n ( x ) Li ( x ) f ( xi )
i 0

Li ( x )
j 0

x xj
xi x j

(2 4)( 2 6)
f 2 (2)
0
(1 4)(1 6)
(2 1)( 2 6)

1.386294
(4 1)( 4 6)
(2 1)( 2 4)

1.791760 0.5658444
(6 1)(6 4)

( j i)

Lagrange Interpolating Polynomials


Example (contd)

Secret Sharing
An (n, k) secret sharing scheme is a scheme where n people receives a part of a
secret, If atleast k persons agree to share their part of the secret, then the secret
should be easily recovered.

1) Knowledge of less than k shares wont make it easier to determine the original
secret.
2) Knowledge of at least k shares would make it easy to compute/determine the
original secret.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Secret Sharing

8 people

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Secret Sharing

6 shares

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Secret Sharing

Having less than 6 shares should not make


it easier to discover the secret.
Having
any
6
should
make
it
computationally easy to compute for the
secret.

6 shares

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Illustration

Give each person a single digit


A group which has 6 digits can know the
secret.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Secret Sharing
The Trusted Third Party (who
knows the secret d) will generate
a (k-1) degree polynomial whose
constant term is d, i.e.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Secret Sharing
The Trusted Third Party (who
knows the secret d) will generate
a (k-1) degree polynomial whose
constant term is d, i.e.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Secret Sharing
The Trusted Third Party (who
knows the secret d) will generate
a (k-1) degree polynomial whose
constant term is d, i.e.

Each member of the group


receives his/her share of the
secret. The TTP will send p(i) to
each of the member (where i is
the members ID)

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Secret Sharing
Any 6 people who shares i and p(i)
can reconstruct the polynomial
either using Lagrange Polynomial
or Newtons Polynomial.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Secret Sharing
New members can easily be
added because the trusted third
party can just generate new
shares
from
the
original
polynomial.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Spline Interpolation
In the previous section, we fitted n data sets on a
single n-1 degree polynomial.
Spline functions connecting polynomials between
consecutive subsets of point.

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Spline Functions
Usually we use polynomials as spline functions
Easy to evaluate
Easy to differentiate
Easy to integrate

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Linear Splines
Given x0 , y0 , x1 , y1 ,......, x n 1 , y n 1 x n , y n , fit linear splines to the data. This simply involves
forming the consecutive data through straight lines. So if the above data is given in an ascending
order, the linear splines are given by yi f ( xi )
Figure : Linear splines

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Linear Splines

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Linear Splines
The upward velocity of a rocket is given as a function of time in Table 1. Find the
velocity at t=16 seconds using linear splines.

time (s)
0
10
15
20
22.5
30

vel (m/s)
0
227.04
362.78
517.35
602.97
901.67

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Quadratic Splines
Given x0 , y0 , x1 , y1 ,......, x n 1 , y n 1 , x n , y n , fit quadratic splines through the data. The splines
are given by
f ( x ) a1 x 2 b1 x c1 ,
a 2 x 2 b2 x c2 ,

x 0 x x1
x1 x x 2

.
.
.
a n x 2 bn x cn ,

x n1 x x n

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Quadratic Splines
Each quadratic spline goes through two consecutive data points
2

a1 x 0 b1 x 0 c1 f ( x0 )
a1 x12 b1 x1 c1 f ( x1 )

.
.
2

a i xi 1 bi xi 1 ci f ( xi 1 )
2

a i xi bi xi c i f ( xi )

.
.
2

a n x n 1 bn x n1 c n f ( xn 1 )
2

a n x n bn xn cn f ( x n )
This condition gives 2n equations
2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Quadratic Splines
The first derivatives of two quadratic splines are continuous at the interior points.
For example, the derivative of the first spline
a1 x 2 b1 x c1 is

2 a1 x b1

The derivative of the second spline


a 2 x 2 b2 x c 2 is

2 a2 x b2

and the two are equal at x x1 giving


2 a1 x1 b1 2a 2 x1 b2
2 a1x1 b1 2a 2 x1 b2 0

2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Quadratic Splines
Similarly at the other interior points,
2a 2 x 2 b2 2a3 x 2 b3 0
.
.
.
2ai xi bi 2ai 1 xi bi 1 0
.
.
.
2a n 1 x n 1 bn 1 2a n x n1 bn 0
We have (n-1) such equations. The total number of equations is (2n) (n 1) (3n 1) .
We can assume that the first spline is linear, that is a1 0
2/20/16

CS 131: NUMERICAL METHODS - PHILIP


CHRISTIAN ZUNIGA

Quadratic Spline Example


THE UPWARD VELOCITY OF A
ROCKET IS GIVEN AS A
FUNCTION OF TIME. USING
QUADRATIC SPLINES

t
s
0

a) FIND THE VELOCITY AT T=16


SECONDS

10

b) FIND THE ACCELERATION AT


T=16 SECONDS

15

c) FIND THE DISTANCE COVERED


BETWEEN T=11 AND T=16
SECONDS

20
22.5
30

v(t)
m/s
0
227.0
4
362.7
8
517.3
5
602.9
7
901.6
7

Data and Plot


t
s
0
10
15
20
22.5
30

v(t)
m/s
0
227.0
4
362.7
8
517.3
5
602.9
7
901.6
7

Solution

v(t ) a1t b1t c1 ,


2

0 t 10

a 2 t b2 t c 2 , 10 t 15
2

a3t b3t c3 , 15 t 20
2
a 4 t b4 t c 4 , 20 t 22.5
2

a5 t b5 t c5 ,
2

22.5 t 30

Let us set up the

Each Spline Goes Through Two


Consecutive Data Points

v(t ) a1t b1t c1 , 0 t 10


2

a1 (0) b1 (0) c1 0
2
a1 (10) b1 (10) c1 227.04
2

Each Spline Goes Through


Two Consecutive Data
Points
t
v(t)
2
s

m/s

10

227.04

15

362.78

20
22.5
30

517.35
602.97
901.67

a 2 (10) b2 (10) c 2 227.04


a 2 (15) 2 b2 (15) c 2 362.78
a3 (15) b3 (15) c3 362.78
2

a3 (20) b3 (20) c3 517.35


a4 (20) 2 b4 (20) c4 517.35
a4 (22.5) 2 b4 (22.5) c4 602.97
2

a5 (22.5) b5 (22.5) c5 602.97


2
a 5 (30) b5 (30) c5 901.67
2

Derivatives are Continuous at Interior Data


Points
v(t ) a1t b1t c1 , 0 t 10
2

a 2 t b2 t c 2 ,10 t 15
2

d
2
a1t b1t c1
dt

t 10

d
2

a2t b2t c2
dt

2a1t b1 t 10 2a2t b2 t 10
2a1 10 b1 2a2 10 b2
20a1 b1 20a2 b2 0

t 10

Derivatives are continuous


at Interior Data Points
At t=10

2a1 (10) b1 2a 2 (10) b2 0

At t=15

2a 2 (15) b2 2a3 (15) b3 0

At t=20

2a3 ( 20) b3 2a 4 (20) b4 0

At t=22.5

2a 4 (22.5) b4 2a5 (22.5) b5 0

Last Equation

a1 0

Final Set of Equations

bcaii

Coefficients of Spline
i
1
2
3
4
5

ai

bi

ci

0
22.704
0
0.8888 4.928 88.88
35.66
0.1356
141.61
1.6048
554.55
33.956
0.2088 28.86
9
152.13