0% found this document useful (0 votes)
5 views

ICT 2102-Curve-fitting and Interpolation.pptx

The document discusses curve-fitting and interpolation techniques in engineering, focusing on methods such as least-squares regression and polynomial fitting. It provides MATLAB code examples for linear regression, nonlinear regression, and interpolation methods, including the use of built-in functions like polyfit and interp1. Additionally, it covers the generalization of least squares and various regression models to analyze experimental data effectively.
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)
5 views

ICT 2102-Curve-fitting and Interpolation.pptx

The document discusses curve-fitting and interpolation techniques in engineering, focusing on methods such as least-squares regression and polynomial fitting. It provides MATLAB code examples for linear regression, nonlinear regression, and interpolation methods, including the use of built-in functions like polyfit and interp1. Additionally, it covers the generalization of least squares and various regression models to analyze experimental data effectively.
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/ 29

ICT 2102: Engineering

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

Testing the goodness of fit


n

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

F = −234.2857 +19.4702v 400

200

3.5
0

-200
10 20 30 40 50 60 70 80
3

2.5 Log-transformed data


>> [a, r2] =
2 linregr(log10(v),log10(F)
) a =
1.5 1.9842 -0.5620
r2 =
1
1.1 1.2 1.3 1.4 1.5 1.6 1.7 2
0.9481
1
1.8 1.9
Example:

Input :

>>rainfall = [10 15 20 25 30 35 40 45 50 55];

erosion = [0.8 1.1 1.4 2.0 1.8 2.5 2.3 3.0 2.7 3.3];

>>[a, r2] = linregr(rainfall, erosion)

Output:

>>a = 0.0525 0.3842

>> 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

Multiple linear regression


y = a0 + a1x1 + a2 x2 +L am
xm n 2 n
Sr ∑ e ∑( i yi − a 0 −1 a1,ix 2 −2,i −L am
)
2
i=1 i=1 ax x m,i
= =
Generalizing least squares
All the regression models you've seen (straight line, polynomials, multiple variables) can be
described using a unified mathematical framework based on basis functions and matrix
algebra.
Generalizing least squares
Linear, polynomial, and multiple linear regression all
belong to the general linear least-squares model:
y = a0 z0 + a1z1 + a2 z2 +L amzm + e
where z0, z1, …, zm are a set of m+1 basis functions and
e is the error of the fit
y = Z a + e
{ } [ ]{ } { }
⎡z z11 L z ⎤
z ⎥
01 m1
z12 L
⎢ ⎥
m2
[Z ]= M M O
⎢z ⎢⎣z z M
02 0 z1n L mn

zji represents the then value of the⎥⎦jth basis function


calculated at the ith point.
Generalizing least squares
{y}= [Z]{a}+{e}
Applying the condition of least squares


n n m

∑ 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

We can solve for the coefficients {a} using matrix


manipulations

To calculate residuals: {y} - [Z]{a}


Example: general least squares
Fit the wind tunnel data to a 2nd order polynomial
v, m/s 10 20 30 40 50 60 70 80
F, N 25 70 380 550 610 1220 830 1450

Create the Z matrix:


>>v=[10, 20, 30,
40, 50,60,70, 80];
>>v=v(:);
>>Z=[ones(size(v)) v v.^2]
Z =
1 10 100
1 20 400
1 30 900
1 40 1600
1 50 2500
1 60 3600
1 70 4900
1 80 6400
Example: general least squares
Determine the coefficient matrix:
>>Z’*Z
ans =
8 360 20400
360 20400 1296000
20400 1296000 87720000

>>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

MATLAB’s polyval command can be used to


compute a value using the coefficients.
y = polyval(p, x)
Nonlinear regression
Not all fits are linear equations of coefficients and
basis functions.

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:

-Write down the basis function.


-use MATLAB’s fminsearch function to find the
values of the coefficients where a minimum occurs
Example: nonlinear regression
For the wind tunnel data, determine the coefficients
for the fit: F = a v a2 1

The m-file function: This function computes


function f = fSSR(a, xm, ym) the Sum of Squared
Residuals (SSR) between
yp = a(1)*xm.^a(2); the observed data and
f = sum((ym-yp).^2); model predictions:

Then, use fminsearch to obtain the values of a that


minimize fSSR:
a = fminsearch(@fSSR, [1, 1], [], v, F)
a = Initial guess for a1 and a2 Option Data
placeholder
2.5384 1.4359
Comparison between regressions
F = 0.2741v1.9842 transformed
a2
F = a 1v
F = 2.5384v1.4359 untransformed
2000

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

(2) Newton’s interpolating polynomial

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)

Also try using:


(a) Newton’s interpolating
polynomial
(b) Using a 1st order
polynomial using the
1980 and 1990 data
(c) Using a 2nd order
polynomial using the
1980,1990 and 1970
data
Piecewise interpolation in MATLAB
The spline command performs cubic interpolation, generally
using not-a-knot method
yy=spline(x, y, xx)

Example: interpolate the Runge’s function f(x) = 1/(1+25x2)


taking 9 equidistant points between -1 and +1 using the spline
command.

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

MATLAB’s interp1 function can perform several


different kinds of interpolation:

yi = interp1(x, y, xi, ‘method’)


x. and y contain the original data
xi. contains the points at which to interpolate
‘method’ is a string containing the desired method:
‘nearest’ - nearest neighbor interpolation ‘linear’
- connects the points with straight lines ‘spline’ -
not-a-knot cubic spline interpolation
‘pchip’ or ‘cubic’ - piecewise cubic Hermite interpolation
Example: interp1 function
Time series of spot measurements of velocity:
t 0 20 40 56 68 80 84 96 104
v 0 20 20 38 80 80 100 100 125

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

You might also like