Upload
omar-khan
View
223
Download
0
Embed Size (px)
Citation preview
7/28/2019 Lecture7-CurveFitting
1/56
ENGG 407ENGG 407
Numerical Methods in EngineeringNumerical Methods in Engineering
Lecture 7Lecture 7
NMsfor Curve Fitting and
Yingxu Wang, Prof., PhD, PEng, FWIF, FICIC, SMIEEE, SMACM
Visiting Professor: MIT (2012), Stanford Univ. (2008), UC Berkeley (2008), Oxford Univ. (1995)
President, International Institute of Cognitive Informatics and Cognitive Computing (ICIC)
.Schulich School of Engineering, University of Calgary
Office: ICT542, Tel: 403 220 6141
http://www.enel.ucalgary.ca/People/wangyx/
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-1
7/28/2019 Lecture7-CurveFitting
2/56
1. Introduction1. Introduction
1. Introduction2. Linear curve fittin
- The least-square linear regression method3. Nonlinear curve fitting
-
- The logarithmic linear regression method
- The spline methods
4. Inter olation- Methods based on curve fitting
- The Lagrange polynomial methods
- Spline-based methods
5. MATLAB applications in curve fitt ing and interpolation6. Summary
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-2
7/28/2019 Lecture7-CurveFitting
3/56
Curve Fitt ing and InterpolationCurve Fitt ing and Interpolation
urve ng
To find a numerical
(approximate) equation
that fits given data.
Interpolation
To find a numerical
approximation of an
internal oint based on
given data.
Extrapolation
To find a numerical approximation of an
external (extended) point based on given
data assumin that the trend is monotonic
orinvariant.
7/28/2019 Lecture7-CurveFitting
4/56
NMs for Curve Fitting and InterpolationNMs for Curve Fitting and Interpolation
- Linearcurve fitting
- The least-square linear regression method
- Nonlinearcurve fitting
- Polynomial regression methods
- The spline methods Series of polynomial fits- The most important and practical method
-
Interpolation- Methods based on curve fitting- The Lagrange polynomial methods
- Spline-based methods
1 0- Straight lines: ( )y x a x a
Typical formsof functions
11 1 0- Polynomials: ( ) ...
- Power functions: ( )
-
n nn n
p
px
y x a x a x a x a
y x kx
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-4
7/28/2019 Lecture7-CurveFitting
5/56
TheThe MATLABMATLAB Curve Fitting InterfaceCurve Fitting Interface
140
100
Shape-preserving interpolant
y = 1.1*x - 22
y = 0.0079*x2 + 0.11*x + 0.7
- * 3 * 2 *
60
80. . . .
y = 3.5e-021*x4 - 1e-018*x3 + 0.0079*x2 + 0.11*x + 0.7
y = - 1.2e-029*x10 + 8.4e-027*x9 - 2.7e-024*x8 + 4.9e-022*x7 -
5.7e-020*x6 + 4.3e-018*x5 - 2.2e-016*x4 + 7.5e-015*x3 +
20
40
0.0079*x + 0.11*x + 0.7 data 1
spline
shape-preserving
linear
0
quadraticcubic
4th degree
10th degree
20 30 40 50 60 70 80 90 100 110 120-20
7/28/2019 Lecture7-CurveFitting
6/56
Inductive Methodologies based on Curve Fitting andInductive Methodologies based on Curve Fitt ing and Extrapolation
The inductive methodology- To create a generic function based on limited observations of data.
The procedure of experimental case studies- Design a hypothesis (conceptual model) of a subject and related
- Collect data
- Inductively elicit the mathematical model
Usages
- Prove or validate a model (hypothesis)
- Calibrate (specify) parameters for a model
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-6
7/28/2019 Lecture7-CurveFitting
7/56
2. Linear Curve Fitting2. Linear Curve Fitting
1. Introduction
2. Linear curve fitt in- The least-square linear regression method
3. Nonlinear curve fitting-
- The logarithmic linear regression method
- The spline methods
4. Inter olation- Methods based on curve fitting
- The Lagrange polynomial methods
- Spline-based methods
5. MATLAB applications in curve fitt ing and interpolation6. Summary
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-7
7/28/2019 Lecture7-CurveFitting
8/56
LeastLeast--Square Regression for Linear FittingSquare Regression for Linear Fitting
Given a set of points ( , ) in a plain,i in x y
1 0by a linear function ?
- The residual: ( )
( )
i i i
n
wha
r
t is the best fit y f x a x a
y f x
n
1
- The total error: (t ii
E r
11
0)
- The absolute total error: ( )
i i
i
n n
y a x a
E r y a x a
2 2
1 01 1
1 1
The squares of to- tal error: ( )
n n
s
i
i i ii
i
iE r y a x a
where the minimum or of total error ithe s
with a san o et ofptimization of the linear fit 2, , paires of data ( , ).i in x yn
r+/- may be
canceled inEt
Regression of
Eat is not unique
Es is proportional to eachri
and has no disadvantages asthose ofEtandEat;
7/28/2019 Lecture7-CurveFitting
9/56
Mathematical Model of the Least Square Regression MethodMathematical Model of the Least Square Regression Method
2 21 0G iven [ ( )] ,
n n
i i iE r y a x a
1 010
the minimums or of the total errors are:
2 ( ) 0n
i ii
the
Ey a x a
a
min
1 011
2 ( ) 0
as an
ls n
i i ii
Ey a x a xa
optimiz
of the linear fit 2with a set of paires of data ( , ), .i in x yation n
1 01 1 1
2
0 1
0 1
n n n
i ii i i
ls n n n
x y
x xx xy
naa x a y
SL E
a x a x x
S a S
S a S a S
1 1 1
SPL et = M wh
i i i
lsSL E
1
0ere , ,yx
xyx xx
Sn SaP S M
SS Sa
01
Solving for , obtain:(1) ( )\
ls
P S a aMSL E P
f x x
xx y x xyS S S Sa
-1(2) P S 1 0
1 2
1( )
| |
xx x
xy
xx x
x x y
xx x
na a
nS S Sa nS S
M f x xS nS
M
7/28/2019 Lecture7-CurveFitting
10/56
1 0G iven , 4,p T na a
E.g.1 The Least Square Linear Regression MethodE.g.1 The Least Square Linear Regression Method
4
[0 30 70 100]and
[0.94 1.05 1.17 1.28]
0 30 70 100 200
T
p
S x
1
4
1
0.94 1.05 1.17 1.28 4.44
i
y ii
S y
2 2 2 2 2
1
0 30 70 100 15800
xx ii
xy i
S x
S x y
4
0 0.94 30 1.05 70 1.17 100 1.28 241.40i 4 200 4.44
,200 15800 241.40
S olution 1:
i
yx
xyx xx
T T
Sn SS M
SS S
0
0 1, . , .
S olution 2:
xx
a aS
a
2 2
15800 4.44 241.40 2000.9428
4 15800 200
y xy x
xx x
S S S
nS S
1
1 0
2 2
. .0.0033
4 1580
0.00
0 200
T hus, the 33 0.942is: 8
xy x y
xx x
na
nS S
p a T a T
l east squ ar e r egression
7/28/2019 Lecture7-CurveFitting
11/56
MATLAB: The Least Square Linear Regression MethodMATLAB: The Least Square Linear Regression Method
=, ,nx = length(x);Sx = sum (x);Sy = sum (y);Sxy = sum (x .* y);
Sxx = sum (x . 2);a1 = (nx*Sxy - Sx*Sy)/(nx*Sxx - Sx 2);
= * - * * - ^
% To print the calibrated function: f(x) = a1 x + a0;
fprintf ('%s %8.4f %s %8.4f \n', 'f(x) =', a1, 'x +', a0);f = x a1*x + a0 % To built the eneral functionplot(x, y,'*r-','markersize', 6); hold on;fplot(f, [0, 100]);xlabel('x'); ylabel('f(x)'); legend ('Sample data', 'Linear regression');
% General matrix solutionS = [nx, Sx; Sx, Sxx]; M = [Sy; Sxy];A = S \ M;
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-11
7/28/2019 Lecture7-CurveFitting
12/56
Application ofApplication ofLeastSquareRegressionLeastSquareRegression
>> p = [0.94 0.96 1.0 1.05 1.07 1.09 1.14 1.17 1.21 1.24 1.28];
>> LeastSquareRegression (T, p)
T = 0.0034 T + 0.9336
a0 = 0.9336
a1 = 0.00341.25
1.3
1.1
1.15
1.2
re(a
tm)
1
1.05Pressu
0 10 20 30 40 50 60 70 80 90 1000.9
0.95
Sample dataLinear regression
emperature
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-12
7/28/2019 Lecture7-CurveFitting
13/56
3. Nonlinear Curve Fitt ing3. Nonlinear Curve Fitt ing
1. Introduction
2. Linear curve fittin
- The least-square linear regression method 3. Nonlinear curve fitt ing-
- The logarithmic linear regression method
- The spline methods
4. Inter olation- Methods based on curve fitting
- The Lagrange polynomial methods
- Spline-based methods
5. MATLAB applications in curve fitt ing and interpolation6. Summary
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-13
7/28/2019 Lecture7-CurveFitting
14/56
3.13.1 Polynomial Regression Methods
The uniform description of polynomials
11 1 0
An -order polynomial:
( ) ...m mm m
m
f x a x a x a x a
Types of polynomials- =
can fit a curve with , 1, points of sample data.n n m
, , .
- A least square linear regression is a special case of polynomial
regression where m = 1
- 2nd order: A quadratic parabola concave up/down determined byat east po nts o ata, m = , n > .
- 3rd order: m = 3, n > 4
A cubic polynomial
with one or more
bends determinedby at least 5 points of data.
Ver hi h order ol nomialmay result in wanders
in curve ends Not the higherthe better.
7/28/2019 Lecture7-CurveFitting
15/56
Mathematical Model of the Polynomial Regression MethodMathematical Model of the Polynomial Regression Method
1
-m m
1 1 0with , , points of sample data,
...1
m m
n
n mn
1 2
1 1 01the square of total error is: [ ( .. )].
m m
i m i m i iiE y a x a ax ax
'
0, 0,1,2,...,
,
ls
E
E i n
In general LS formulation:S themodel matri
Based on , an SLE can be obtained as:lsE SP=MP theparameter vectorM themeasurementvector
.cf Ax=b
,1
- where is called ,, )(T
TTS S S ro -1T -1 T pseudo inversP=( M SS MS) S e
0 1- ... ,m
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-15
7/28/2019 Lecture7-CurveFitting
16/56
Case 1: QuadraticCase 1: Quadratic Polynomial RegressionPolynomial Regression2
G iven a ( 2, 3) :m nQ uadratic P ol nomial a x a ax 2 2
2 1 01
[ ( )] , 3n
i i ii
E y a x a x a n
2 02 1 0
1
2 1
2 ( ) 0
2 0
i i i iio
n
y a x a x a xaE
a x a x a xE
11
2
22 2 (
i
i i
a
E
y a xa
2
1 01 ) 0
n
i ii a x a x
2
2
0 1 2
R ewri te , the following SL E is obtained:
x yx
ls
na S a S a S
E
2 3
2 3 4 2
0 1 2
0 1 2
2L E = x xyx x
x xx x y
a a aS a S a S a S
n n n n
w rehe xS x1 1 1
1
1
0 2
, , , and
thus [ , ]
ki y i i xy i ixi i i i
Ta a a
S y S x S x y
P = S \ MSP = M
7/28/2019 Lecture7-CurveFitting
17/56
Case 2: HigherCase 2: Higher Order Polynomial RegressionsOrder Polynomial Regressions
4 3 2
Givenan 4 5 :m nmorder ol nomial a x a x xa x a a 4 3 2 2
4 3 2 1 01
[ ( )] , 5
n
i i i i ii
E y a x a x a x a x a n
0, 0,1,2,...,lsi
i nE
E
a
m ,sl
2 3 40 1 2 3 4 x yx x x
na S a S a S a S a S
2 3 4 5
2 3 4 5 6 2
0 1 2 3 4
0 1 2 3 4mSLE =
x xyx x x x
x x x x x x yS a S a S a S a S a S
3 4 5 6 7 3
4 5 6 7 8 4
0 1 2 3 4
0 1 3 42
x x x x x x y
x x x x x x yS a S a S a S a S a S
T
0 1 2 3 4
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-17
7/28/2019 Lecture7-CurveFitting
18/56
MATLAB: The Polynomial Regression MethodMATLAB: The Polynomial Regression Method
, ,n = length (X);
for i = 1 : 2*m
= ^
endS(1,1) = n;
M(1,1) = sum(Y);, 2E.g.: m SP = M
for j = 2 : m+1
S(1, j) = xsum (j-1);
end
2
2 3
0 1 2
0 1 2
x yx
x xyx x
na S a S a S
S a S a S a S
for i = 2 : m+1
for j = 1 : m+1
S(i, j) = xsum (j+i-2);
2 3 4 20 1 2x x x x ya a a
enM(i,1) = sum (X .^ (i-1).* Y);
end
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-18
7/28/2019 Lecture7-CurveFitting
19/56
Application ofApplication ofPolyRegressionPolyRegression
n =>> X = 0:0.4:6;
>> Y = [0 3 4.5 5.8 5.9 5.8 6.2 7.4 9.6 15.6 20.7 26.7 31.1 35.6 39.3 41.5];
>> [p] = PolyRegression (X, Y, m)p =
- . - a
12.8780 - a1
-10.1927 - a2. -
-0.2644 - a4
4 3 24 3 2 1 0
4 3 2
( )
0.2644 3.1185 10.1927 12.8780 0.2746
f x a x a x a x a x a
x x a x x
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-19
7/28/2019 Lecture7-CurveFitting
20/56
3.2 The Logarithmic Linear Regression Method
A nonlinear problem may be transformed into a linear oneon the logarithmic space.
Treatment of axes
X : Y = linear : linear | linear : log | log : linear | log : log
Disadvantages
The accuracy / errorof results may be decreased / increased.
log
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-20
7/28/2019 Lecture7-CurveFitting
21/56
Mathematical Model of the Logarithmic Linear Regression MethodMathematical Model of the Logarithmic Linear Regression Method
m
,its linear form is:
rebuilt:
ln( ) ln( ) ln( )
a x a
y m x b
0
1
( ) lnwhere anda
a my y
mx ,
its linear form is:
rebu
ln
il
( )
t:
ln( )
a x a
y mx b
1
( )where and
ln a my y
0a
0
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-21
7/28/2019 Lecture7-CurveFitting
22/56
E.g.2 TheE.g.2 The LogarithmicLogarithmic Linear Regression MethodLinear Regression Method
1 Given the equation ( ) ,
1
RCr cexponential v t v e
1 0
rebuilt:
r c
r
RC
v a t a
ln( )
where and
r rav v
t t
1
1
1 1or C
RC a R
0
1 0
0 n or
I.e.: ( )
c c
rv t a t a
a v v e
1 1 aRC
0 c
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-22
7/28/2019 Lecture7-CurveFitting
23/56
Application of theApplication of the LogarithmicLogarithmic Linear Regression MethodLinear Regression Method
% Lo arithmicLo arithmic linear re ressionlinear re ressiontexp = 2:2:30;
vexp = [9.7 8.1 6.6 5.1 4.4 3.7 2.8 2.4 2.0 1.6 1.4 1.1 0.85 0.69 0.6];
vexpLOG = log (vexp);
R = 5e6;
[a1, a0] = LinearRegration (texp, vexpLOG)Vc = exp(a0)
C = -1 / (R*a1)
t = 0 : 0.5 : 30;
Vr = Vc * exp(a1 * t);' ', , , ,
% Solutions
a1 = -0.1002
a0 = 2.4776Vc = 11.9131
C = 1.9968e-006 [uF]
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-23
7/28/2019 Lecture7-CurveFitting
24/56
3.33.3 The PolynomialThe Polynomial SplineSpline FittingFitting Methods
- A long curve may be better fitted as multiple splines (pieces)
- Splines can be easily fitted by lower order polynomials, usually m= 3 or 2
- The wander roblem in curve ends can be avoided in iecewise fit tin
Types of polynomial splines- Linear spilines: m = 1, n > 2
- Quadratic spilines: m = 2, n > 3
- Cubic spilines: m = 3, n > 4
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-24
7/28/2019 Lecture7-CurveFitting
25/56
Mathematical Model of PolynomialMathematical Model of Polynomial SplinesSplines
11 1 0( ) ...,
, 1m mm mk
n mf x a x a x a x a
1 2 11 1 21 2 11
[ , ,..Let [( ,..., ),( ,..., ),...,( ,..., )]., ]
k
k ki
n n k knxX X x x x x xX X X
1 i i
i
1 1 11 1
( ), // i.e.:n
f x x X x x x
2 2,... - are obtainedLow order polynomial fits
x x
:,
where knk k
otskn xots 1 1, 1,1 ,={ }i n i kx x x i x
: - = - -o skn to a n no a a po ns n
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-25
7/28/2019 Lecture7-CurveFitting
26/56
The PolynomialThe Polynomial SplineSpline Fitting MethodsFitting Methods
function [ ] = PolySplineFitting (X1, Y1, X2, Y2)
p1 = polyfit (X1, Y1, 2);
fprintf ('fspline1(x) = %10.4f %s %10.4f %s %10.4f %s\n', p1(1), '*x^2 + (',
p1(2),')*x + (', p1(3), ')');
* ^ *
p2 = polyfit (X2, Y2, 2);fprintf ('fspline2(x) = %10.4f %s %10.4f %s %10.4f %s\n', p2(1), '*x^2 + (',
' * ' ' ', , ,
fspline2 = @(x2) p2(1)*x2^2 + p2(2)*x2 + p2(3);
grid on; hold on;
for xs1 = X1 1 : 0.01 : X2 1
ys1 = fspline1(xs1);
plot (xs1, ys1, 'k');
end
or xs = : . : eng
ys2 = fspline2(xs2);
plot (xs2, ys2, 'g');
end
xlabel ('x - Segments');
ylabel ('y(x) - Splines');
legend ('Spline 1','Spline 2');
7/28/2019 Lecture7-CurveFitting
27/56
% For iven x = 8 11 15 18 22 = 5 9 10 8 7
Application ofApplication ofPolySplineFittingPolySplineFitting
% Spline to 2 pieces
>> X1 = [8 11 15]
>> X2 = [15 18 22] 10
11
Data 1
Spline 1
>> Y1 = [5 9 10]
>> Y2 = [10 8 7]8
9
lines
Spline 2
6
7y(x)-
S
8 10 12 14 16 18 20 224
5
x - Se ments
>> PolySplineFitting (X1, Y1, X2, Y2)
fspline1(x) = -0.1548 *x^2 + ( 4.2738 )*x + ( -19.2857 )
* *. - . .
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-27
7/28/2019 Lecture7-CurveFitting
28/56
4. Interpolation4. Interpolation
1. Introduction
2. Linear curve fittin- The least-square linear regression method
3. Nonlinear curve fitting-
- The logarithmic linear regression method
- The spline methods
4. Inter olation- Methods based on curve fitting
- The Lagrange polynomial methods
- Spline-based methods
5. MATLAB applications in curve fitt ing and interpolation6. Summary
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-28
7/28/2019 Lecture7-CurveFitting
29/56
4.1 Interpolation Methods Based on Curve Fitting4.1 Interpolation Methods Based on Curve Fitting
The result of a curve fitting, f(x) in [a, b], such asPolyRegression (X, Y, m), has already provided a solution
, , . . .
- This is known as the interpolation method based oncurve fitting.
- This also establishes the relationship between the
methods of curve fitt ing and interpolation.
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-29
7/28/2019 Lecture7-CurveFitting
30/56
4.24.2 The Lagrange Polynomial Methods
The Lagrange polynomials are a form of polynomials forinterpolation for a given set of data.
- .
f(x) should be calculated forall x; so do forx int x afteran interpolation.
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-30
7/28/2019 Lecture7-CurveFitting
31/56
Mathematical Model of the Lagrange MethodMathematical Model of the Lagrange Method
int 1Given ( ) at [ , ],nf x x x x
The representation of ( ) is:
( )n n n j
Lagrange polynomial f x
f x xx x
L xx
n11 1
where ( ) is called the .
( )
i j j i ji i
i
x x
Lagrange fuL nc ionx t
.g.:
2 1
1 2
( ) ( )
- 1st order LP: ( )
x x x x
f x y yx x x x
2 3 1 31 2
1 2 1 3 2 1 2 3
( )( ) ( )( )- 2nd order LP: ( )
( )( ) ( )( )
x x x x x x x xf x y y
x x x x x x x x
1 23
3 1 3 2
( )( )( )( )
x x x x yx x x x
...
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-31
7/28/2019 Lecture7-CurveFitting
32/56
E.g.3 A 4E.g.3 A 4thth Order Lagrange Polynomial FittingOrder Lagrange Polynomial FittingGiven x = [1 2 4 5 7]
2 3 4 5 1 3 4 5
y = [52 5 -5 -40 10]
T he 4th order Lagrange polynomila is:
( )( )( )( ) ( )( )( )( )x x x x x x x x x x x x x x x x 1
1 2 1 3 1 4 1 5 2 1 2 3 2
( )( )( )( ) ( )( )(x x x x x x x x x x x x x 24 2 5
1 2 4 5 1 2 3 53 4
3 1 3 2 3 4 3 5 4 1 4 2 4 3 4 5
)( )
( )( )( )( ) ( )( )( )( )( )( )( )( ) ( )( )( )( )
x x x
x x x x x x x x x x x x x x x xy y
x x x x x x x x x x x x x x x x
1 2 3 45
5 1 5 2 5 3 5 4
( )( )( )( )
( )( )( )( )
x x x x x x x xy
x x x x x x x x
52( 2)( 4)( 5)( 7) 5( 1)( 4)( 5)( 7)x x x x x x x x (1 2)(1 4)(1 5)(1 7) (2 1)(2 4)(2 5)(2 7)
5( 1)( 2)( 5)( 7) 40( 1)( 2)( 4)( 7)
(4 1)(4 2)(4 5)(4 7) (5 1)(5 2)(5 4)(5 7)
x x x x x x x x
10( 1)(
x x
2)( 4)( 5)
(7 1)(7 2)(7 4)(7 5)
0.72( 2)( 4)( 5)( 7) 0.17( 1)( 4)( 5)( 7)
x x
x x x x x x x x
int int
. .
0.06( 1)( 2)( 4)( 5)
L et 3, (
x x x x x x x x
x x x x
x f x
) 6.00
7/28/2019 Lecture7-CurveFitting
33/56
MATLAB: The Lagrange Polynomial MethodMATLAB: The Lagrange Polynomial Method
function [Yint] = LagrangeINT(x, y, Xint)
for i = 1:nL(i) = 1;
or = :n
if j ~= i
L i = L i * Xint - x / x i - x ;end
end ( )n n n jx x
Yint = sum(y .* L);n
11 1 ( )i i j j i i jx x
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-33
7/28/2019 Lecture7-CurveFitting
34/56
Application of the Lagrange MethodApplication of the Lagrange Method
40
60
0
20
-40
-20
y
1 2 3 4 5 6 7-80
-60
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-34
x
4 34 3 Th S li B d M th d f I t l ti
7/28/2019 Lecture7-CurveFitting
35/56
4.34.3 The Spline-Based Methods for Interpolation
Inter olation b Pol nomial S lines
- Strategy: Polynomial spline fitting spline-based interpolation
Mathematical models
3 23 2 1 0
a) Polynomial spline fitting
, 1( )k
n mf x a x a x a x a
11 1 211 21
, 1,
2
1
1e , ..., , ...,
=
, , ,..., ,..., ,...,
where { } - =,
k ki
knots i n i knots
n n k knx x x x x x
data pointsx x x N kn n kn
- ( -1)
k
k
1 1
23 2
1
2
sp nes:
( ),
( ),
i ii
k
f x x X
f x x X
x xx
13 2 1 0
...( ),
i i
k k
ii i
f x x
a x a x x
X
a a
int i
b) Interpolation in a s
(
pline
( ) i xx ff 3nt int 2int 3 2 1 0int int| )ix X a a x a ax x
MATLAB: The PolynomialMATLAB: The Polynomial SplineSpline MethodMethod
7/28/2019 Lecture7-CurveFitting
36/56
MATLAB: The PolynomialMATLAB: The Polynomial SplineSpline MethodMethod (1/2)(1/2)
function [Yi1, Yi2 ] = SplineFittingBasedInterpolation (X1, Y1, X2, Y2, Xi1, Xi2)
% Spline fitting
p1 = polyfit (X1, Y1, 2);
fprintf ('fspline1(x) = %10.4f %s %10.4f %s %10.4f %s\n', p1(1), '*x^2 + (',
' * ' ' ', , ,
fspline1 = @(x1) p1(1)*x1^2 + p1(2)*x1 + p1(3);
p2 = polyfit (X2, Y2, 2);
fprintf ('fspline2(x) = %10.4f %s %10.4f %s %10.4f %s\n', p2(1), '*x^2 + (',
p2(2),')*x + (', p2(3), ')');
fspline2 = @(x2) p2(1)*x2^2 + p2(2)*x2 + p2(3);
grid on; hold on;or xs = X : . : X
ys1 = fspline1(xs1);
plot (xs1, ys1, 'k');
end
for xs2 = X2(1) : 0.01 : X2(length(X2))ys2 = fspline2(xs2);
plot (xs2, ys2, 'g');
en
xlabel ('x - Segments'); ylabel ('y(x) - Splines');
legend ('Spline 1','Spline 2'); ...
MATLAB: The PolynomialMATLAB: The Polynomial SplineSpline MethodMethod
7/28/2019 Lecture7-CurveFitting
37/56
MATLAB: The PolynomialMATLAB: The Polynomial SplineSpline MethodMethod (2/2)(2/2)
=, , , , ,Xi1, Xi2)
% Inter olation
for xint1 = X1(1) : 0.5 : X2(1)
yint1 = fspline1(xint1);plot (X1, Y1, 'r*', xint1, yint1, 'b.')
end
for xint2 = X2(1) : 0.5 : X2(length(X2))y n = sp ne x n ;
plot (X2, Y2, 'r*', xint2, yint2, 'b.')
end
% Determine interpolation values
Yi1 = fspline1(Xi1);
= sp ne ;
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-37
Application ofApplication of SplineFittingBasedInterpolationSplineFittingBasedInterpolation
7/28/2019 Lecture7-CurveFitting
38/56
% For iven x = 8 11 15 18 22 = 5 9 10 8 7
Application ofApplication ofSplineFittingBasedInterpolationSplineFittingBasedInterpolation
>> X1 = [8 11 15]
>> X2 = [15 18 22]
>> Y1 = [5 9 10] 10
11
Spline 1
Spline 2
>> Y2 = [10 8 7]
>> Xi1 = 10;Xi2 = 20 8
9
ines
6
7y(x)-
Spli
8 10 12 14 16 18 20 224
5
>> [Yi1, Yi2] = SplineFittingBasedInterpolation (X1, Y1, X2, Y2, Xi1, Xi2)
fs line1 x = -0.1548 *x^2 + 4.2738 *x + -19.2857
x - Segments
fspline2(x) = 0.0595 *x^2 + ( -2.6310 )*x + ( 36.0714 )
Yi1 = 7.9762Yi2 = 7.2619
4 44 4 D I t l ti dD I t l ti d C Fitti
7/28/2019 Lecture7-CurveFitting
39/56
4.4 n4.4 n--D Interpolation andD Interpolation and Curve Fittingg
Matlab 2-D interpolation
Zint = interp2 (X, Y, Z, Xint, Yint)
Example[X, Y] = meshgrid(-3: .25 :3);
= pea s , ;
[Xint, Yint] = meshgrid(-3: .125 :3);
Zint = interp2 (X, Y, Z, Xint, Yint);
, , ,
hold,
mesh(Xint, Yint, Zint+15)
axis([-3 3 -3 3 -5 20])
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-39
Improved 3Improved 3 D Interpolation andD Interpolation and Curve Fittingg
7/28/2019 Lecture7-CurveFitting
40/56
Improved 3Improved 3--D Interpolation andD Interpolation and Curve Fittingg
h0 = 0.25 h1 = 0.125 h2 = 0.01
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-40
Taylor Series Method for InterpolationTaylor Series Method for Interpolation
7/28/2019 Lecture7-CurveFitting
41/56
Taylor Series Method for InterpolationTaylor Series Method for Interpolation
-200
0
200
400, - .>> F = 3*X.^2 + 4*X.*Y - 2*Y.^2
>> mesh (X, Y, F)
-10
-5
0
5
10
-10
-5
0
5
10
-400
>> Taylor1 = 20*(Y+3)-30
,x y x xy y
>> mes , , ay or
* ^ * ^10
-200
-100
0
100
200
200
400
- . - .
+ 4*(X-2).*(Y+3) + 20*(Y+3)-30
>> mesh (X, Y, Taylor2)
-10
-5
0
5
-10
-5
0
-5
0
5
10
-5
0
5
10
-400
-200
0
1 ! ( , )( , ) [ ( ) ( ) ]
nnk n kn f xyf x x a b
-10-10
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-41
0 ,k x a yn n x y
5 MATLAB A li ti i C Fitti d I t l ti5 MATLAB A li ti i C Fitti d I t l ti
7/28/2019 Lecture7-CurveFitting
42/56
5. MATLAB Applications in Curve Fitting and Interpolation5. MATLAB Applications in Curve Fitting and Interpolation
1. Introduction
2. Linear curve fittin- The least-square linear regression method
3. Nonlinear curve fitting-
- The logarithmic linear regression method
- The spline methods
4. Inter olation- Methods based on curve fitting
- The Lagrange polynomial methods
- Spline-based methods
5. MATLAB applications in curve fitting and interpolation6. Summary
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-42
5 1 MATLAB B ilt5 1 MATLAB B ilt i F tii F ti
7/28/2019 Lecture7-CurveFitting
43/56
5.1 MATLAB Built5.1 MATLAB Built--in Functionsin Functions
Curve fitting
polyfit (x, y, m)
interp1 (x, y, xi, method)
- Method: nearset linear default s line cubic chi- The last two may be used for extrapolation
interp2 (x, y, z, xi, yi, method)
, , , , , , ,
interpn (x1, x2, x3, , v, y1, y2, y3, , method)
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-43
Application ofApplication of polyfitpolyfit
7/28/2019 Lecture7-CurveFitting
44/56
Application ofApplication ofpolyfitpolyfit
50
30
10
0 1 2 3 4 5 6
-10
0
Application ofApplication of interp1interp1
7/28/2019 Lecture7-CurveFitting
45/56
Application ofApplication ofinterp1interp1
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-45
Application ofApplication of interp2interp2
7/28/2019 Lecture7-CurveFitting
46/56
Application ofApplication ofinterp2interp2
Syntax
Zint = interp2 (X, Y, Z, Xint, Yint)
Example
[X, Y] = meshgrid(-3: .25 :3);
= pea s , ;
[Xint, Yint] = meshgrid(-3: .125 :3);
Zint = interp2 (X, Y, Z, Xint, Yint);
, , ,
hold,
mesh(Xint, Yint, Zint+15)
axis([-3 3 -3 3 -5 20])
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-46
Application ofApplication of interp3interp3
7/28/2019 Lecture7-CurveFitting
47/56
Application ofApplication ofinterp3interp3
Syntax
VI = interp3 (X, Y, Z, V, XI, YI, ZI)
Example
[x,y,z,v] = flow(10);
[xi,yi,zi] = meshgrid
- -. . , . , .vi = interp3(x,y,z,v,xi,yi,zi);
slice(xi,yi,zi,vi,[6 9.5],2,[-2 .2]),
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-47
Application ofApplication of interpninterpn
7/28/2019 Lecture7-CurveFitting
48/56
Application ofApplication ofinterpninterpn S ntax
VI = interpn (X1, X2, X3, ..., V, Y1, Y2, Y3, ...)
f = @(x,y,z,t) t.*exp(-x.^2 - y.^2 - z.^2);[x,y,z,t] = ndgrid(-1:0.2:1,-1:0.2:1,-1:0.2:1,0:2:10);
, , ,
[xi,yi,zi,ti] = ndgrid(-1:0.05:1,-1:0.08:1,
-1:0.05:1, ... 0:0.5:10);
= ' ', , , , , , , , ,nframes = size(ti, 4);
for j = 1:nframes
slice i :,:,:, , xi :,:,:, , zi :,:,:, , ...
vi(:,:,:,j),0,0,0);caxis([0 10]);
M(j) = getframe;
end
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-48
5 2 MATLAB Applications5 2 MATLAB Applications
7/28/2019 Lecture7-CurveFitting
49/56
5.2 MATLAB Applications5.2 MATLAB Applications
Curve fitt ing
- Build a model of stopping distance of cars
- Build a model of bacteria growth over time
Interpolation
- Build a model of ocean water tem erature descent
Extrapolation
- Predicate ocean water temperature beyond sample data
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-49
E.g.4 Stopping Distances of CarsE.g.4 Stopping Distances of Cars
7/28/2019 Lecture7-CurveFitting
50/56
E.g.4 Stopping Distances of CarsE.g.4 Stopping Distances of Cars
clear; clc
v=20:20:120;
d=[6 18 36 60 91 128];p = polyfi t (v, d, 2)
kModel=@ (x) p(1)*x.^2+p(2)*x+p(3);
vp = 20:120;
dp = kModel(vp);plot (vp, dp, v, d, '*')
xlabel('v (km/h)'), ylabel('d (m)')
>> p = 0.0079 0.1123 0.7000
% d(v) = 0.0079v
2
+ 0.1123v + 0.7000
E.g.5 Bacteria Growth RateE.g.5 Bacteria Growth Rate
7/28/2019 Lecture7-CurveFitting
51/56
t0=0;
t=0:2:8;
y=log(Nt);
p=polyfit(t,y,1);
mu=p(1) 14
16
18
20
N0=exp(p(2)+mu*t0)
>> mu = 1.8837
>> =8
10
12
ln(Nt)
.
% f(t) = mu*t + ln(N0)
= 1.8837t + 3.65190 1 2 3 4 5 6 7 8
2
4
t
E.g.6 Ocean Water Temperature PredicationE.g.6 Ocean Water Temperature Predication
7/28/2019 Lecture7-CurveFitting
52/56
>> D = [0 100 300 400 500 750 1000 1250 1500 1750];
= . . . . . .
>> WT_Int50 = interp1(D, WT, 50)
WT_Int50 = 21.8033
>> WT Int600 = inter 1 D WT 60020
22
_WT_Int600 = 13.4668
>> WT_Int1400 = interp1(D, WT, 1400)
WT_Int1400 = 4.7598 14
16
18
(oC)
>> WT_Extrap1800 =interp1(D, WT, 1800, 'pchip')
WT_Extrap1800 = 4.37258
10
12WT
_
interp1(D, WT, 2000, 'pchip')
WT_Extrap2000 = 4.32000 200 400 600 800 1000 1200 1400 1600 1800 2000
4
6
D (m)
E.g.7 Estimate the Absolute Zero by ExtrapolationE.g.7 Estimate the Absolute Zero by Extrapolation
7/28/2019 Lecture7-CurveFitting
53/56
1 0G i v en , an dp a T a
1 0
[ 0 3 0 7 0 1 0 0 ][ 0 . 94 1 . 05 1 . 17 1 .28 ]
0 .0 0 3 3 0 .9 4 2 8
T
p
p a T a T
0
1
- - 0 . 9428L et 0 , - 2 8 5 .7 0 -2 7 3 .1 5 C
0 . 0 0 3 3
ap T
a
>> T = 0 10 20 30 40 50 60 70 80 90 100
>> P = [0.94 0.96 1.0 1.05 1.07 1.09 1.14 1.17 1.21 1.24 1.28];
>> p = polyfit (T, P, 1)
p = 0.0034 0.9336
= - . .
T0 = -274.5882 % Extrapolation
6 Summary6 Summary
7/28/2019 Lecture7-CurveFitting
54/56
6. Summary6. Summary
1. Introduction
2. Linear curve fittin- The least-square linear regression method
3. Nonlinear curve fitting-
- The logarithmic linear regression method
- The spline methods
4. Inter olation- Methods based on curve fitting
- The Lagrange polynomial methods
- Spline-based methods
5. MATLAB applications in curve fitt ing and interpolation 6. Summary
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-54
Summary of Lecture 7Summary of Lecture 7
7/28/2019 Lecture7-CurveFitting
55/56
Lecture 7. NMs for Curve Fitting and Interpolation
Overview Interpolation-- InductiveInductive modelingmodel ing o unctionso unctionsbased on sample databased on sample data
- Curve fitting- Interpolation
- Methods based on curve fitting
- The Lagrange polynomial methods- Spline-based methods
- Extrapolation
Linear curve fitting
- Least-squares estimation
MATLAB functions
- polyfit (x, y, m)
- inter 1 x xi method- e eas -square near
regression method
Nonlinear curve fitting
- interp2 (X, Y, Z, XI, YI)
- interp3 (X, Y, Z, V, XI, YI, ZI)
- interpn (X1, X2, X3, ..., V,-
method- Polynomial regression methods
- The spline method
, , , ...
Engineering applications- Case studies
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-55
Further ReadingFurther Reading
7/28/2019 Lecture7-CurveFitting
56/56
gg
Numerical Methods for Engineers and Scientists- .
ENGG407 - Numerical Methods in Engineering, Dr. Y. Wang, 25/02/13 L7-56