ICT 2102-Curve-fitting and Interpolation.pptx
ICT 2102-Curve-fitting and Interpolation.pptx
Computation
Sessional
1.5 Credits, 3 hrs/week
Mahin Sultana
Lecturer
Department of Civil Engineering
Bangladesh University of Business &
Technology
Curve-fitting and Interpolation
Curve-fitting and engineering practice
sampling data are acquired
at discrete points.
-but the values at undefined points
are wanted in many engineering
applications
Interpolation
- Ex: density of water at different
temp.
Least-squares Regression
-Purpose is trend analysis or
hypothesis testing
Trend of experimental data
Experimental data for force (N) and velocity (m/s) from a
wind tunnel experiment
v, m/s 10 20 30 40 50 60 70 80
F, N 25 70 380 550 610 1220 830 1450
1500
Is the relationship
1000
-linear? Which order?
(Linear or polynomial
F (N)
regression)
500
-nonlinear?
0
(Nonlinear regression)
0 20 40 60 80
v (m/s)
Fitting the data with a straight line
Minimizing the sum of squares of the estimated residuals
n n
Sr ∑ ei∑2
(yi −)a 0 −1a
2
= i=1 =i=1 x i
xi yi − xi y2i
a1 = n
∑n∑ xi2 ∑∑∑ ( )
xi
−
a0 = y − a1x
S t−
St ∑i (y − y 2
2
r = i=1
)
r = S n
St −ax 2
Sr =i=1 (y −
i 0 1
a
∑ ) i
A MATLAB code for linear regression
function [a, r2] = linregr(x,y)
% input:
% x = independent variable
% y = dependent variable
% output:
% a = vector of slope, a(1), and intercept, a(2)
% r2 = coefficient of determination
n = length(x);
if length(y)~=n, error('x and y must be same length');
end x = x(:); y = y(:); % convert to column vectors
sx = sum(x); sy = sum(y);
sx2 = sum(x.*x); sxy = sum(x.*y); sy2 = sum(y.*y);
a(1) =
(n*sxy-sx*sy)/(n*sx2-sx^2); a(2)
= sy/n-a(1)*sx/n;
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;
% create plot of data and best fit
line xp = linspace(min(x),max(x),2);
yp = a(1)*xp+a(2);
plot(x,y,'o',xp,yp
) grid on
CE 206: Engg. Computation sessional
Fitting a straight line
1600
>> [a, r2] = linregr(v,F)
1400
a =
1200
19.4702 -234.2857
1000
r2 =
800
0.8805
600
200
3.5
0
-200
10 20 30 40 50 60 70 80
3
Input :
erosion = [0.8 1.1 1.4 2.0 1.8 2.5 2.3 3.0 2.7 3.3];
Output:
>> r2 = 0.9331
Variety of linear regressions
To fit a second order polynomial
n n
Sr ∑ ei2∑( ) yi − a 0 −1 ai x 2− 2 2
i=1 i=1 ax i
= =
Higher order polynomials
n n
Sr ∑ e∑( 2
i yi − a0 −1a x i− a 2x −L − am x
2 m 2
)
= i=1 = i=1 i
i
∑ e∑ ⎜ yi − a∑z
2
2
Sr i j ⎟
= i=1 = i=1 ⎝ j=0 ⎠ ji
We get the following:
[[Z ] [Z ]]{a}= {[Z ] {y}}
T T
>>F=F(:);
>>Z’*F
ans =
5135
>>a=(Z’*Z)\(Z’*F)
312850 a =
20516500 -178.4821
16.1220
0.0372
polyfit and polyval function
MATLAB has a built-in function polyfit that fits a
least-squares nth order polynomial to data:
p = polyfit(x, y, n)
x: independent data
y: dependent data
n: order of polynomial to fit
p: coefficients of polynomial f(x)=p1xn+p2xn-1+…+pnx+pn+1
Problems in transformation:
-Not all equations can be transformed easily or at all
-The best fit line represents the best fit for the transformed
variables, not the original variables.
Alternate approach: nonlinear regression.
Methodology:
1500
untransformed
F (N)
1000
500
transformed
0
0 20 40 60 80
v (m/s)
Polynomial interpolation
(1) Use MATLAB’s polyfit and polyval function
- the number of data points should be equal to the number of coefficients
fn−1 x = b1 + b2 x − x1 +L + bn x − x1 x − x2 L x − xn−1
() ( ) ( )( ) ( )
b1 = f x1
( )
b2 = f x2 , x1 Divided
b3 = f [x3, x2 ,] x1 Differences
[ ]
M
bn = f xn , xn−1,L , x2 , x1
[
(3) Lagrange’s interpolating
] polynomial
Polynomial interpolation
Use polyfit and polyval function to predict the population
of 2000 using a 7th order polynomial fitted with the first 8 points
Year 1920 1930 1940 1950 1960 1970 1980 1990 2000
Populat 106.46 123.08 132.12 152.27 180.67 205.05 227.23 249.46 281.42
i on
(mil)
x = linspace(-1, 1, 9);
y = 1./(1+25*x.^2);
xx = linspace(-1, 1);
yy = spline(x, y, xx);
yr = 1./(1+25*xx.^2)
plot(x, y, ‘o’, xx, yy, ‘-’, xx, yr, ‘--’)
MATLAB’s interp1 function
140
120
100
>> 80
tt=linspace(0,110);
>> 60
v1=interp1(t,v,tt);
40
>>
plot(t,v,'o',tt,v1)
20
0
0 20 40 60 80 100 120