Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
06-07 Chapter interpolation in MATLAB
1. Chapter 06
& 07
LEAST SQUARES
FITTING OF CURVES
TO DATA
INTERPOLATION
4/5/2016 DR. MOHAMMED DANISH 1
2. Curve fitting
Curve fitting is also called regression analysis, is a process
of fitting a function to a set of data points.
The function can be used as a mathematical model of the
data.
There are many types of functions linear, polynomial,
power, exponential, etc, the curve fitting can be a
complicated process.
4/5/2016 DR. MOHAMMED DANISH 2
3. Least square curve fitting
The best line has minimum error between line and data
points:
This is called the least squares approach, when the square
of the error is minimum.
To find the minimum error, the derivative of the error with
respect to each a and b must be zero.
y ax b
2
1
[ ( )]
N
i i
i
Error y ax b
4/5/2016 DR. MOHAMMED DANISH 3
4. 2
1
1
2 2
1 1 1 1 1 1
2
1
[ ( )]
( )
0
( )
2 [ ( )] 0
0
[ ( )]
( )
0
( )
2 [ ( )] 0
N
i i
i
N
i i i
i
N N N N N N
i i i i i i i i
i i i i i i
N
i i
i
i i
y ax b
Error
a a
Error
x y ax b
a
x y a x b x x y a x b x
y ax b
Error
b b
Error
y ax b
b
1 1 1
,
N N N
i i
i i i
y a x bN
4/5/2016 DR. MOHAMMED DANISH 4
5. 2
1 1 1
1 1
N N N
i i i i
i i i
N N
i i
i i
a x b x x y
a x bN y
1 1
2
1 1 1
N N
i i
i i
N N N
i i i i
i i i
matrix form
N x y
b
a
x x x y
4/5/2016 DR. MOHAMMED DANISH 5
6. 1 1 1
2
2
1 1
2
1 1 1 1
2
2
1 1
N N N
i i i i
i i i
N N
i i
i i
N N N N
i i i i i
i i i i
N N
i i
i i
N x y x y
a
N x x
y x x x y
b
N x x
Linear least square fitting is a method to determine the best
coefficients in a linear model for given set of data.
4/5/2016 DR. MOHAMMED DANISH 6
7. Example 1
V
(m/s)
F
(N)
i xi yi (xi)2 xiyi
1 10 25 100 250
2 20 70 400 1400
3 30 380 900 11400
4 40 550 1600 22000
5 50 610 2500 30500
6 60 122
0
3600 73200
7 70 830 4900 58100
8 80 145
0
6400 116000
Σ 360 513
5
20400 312850
2 22
2
2 22
1
8 312850 360 5135
19.47024
8 20400 360
5135(20400) 360(312850)
8(20400) (360)
104754000 112626000 7872000
234.285
163200 129600 33600
641.
i i i i
i i
i i i i i
i i
N x y x y
a
N x x
y x x x y
b
N x x
b y a x
875 19.47024 45 234.2857
4/5/2016 DR. MOHAMMED DANISH 7
8. MATLAB function for least
square fitting of data
MATLAB programming 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 the polynomial to fit
p: coefficients of polynomial
1
1 2 1( ) ............n n
n nf x p X p X p X p
4/5/2016 DR. MOHAMMED DANISH 8
9. MATLAB function for least
square fitting of data
MATLAB’s polyval command can be used to compute a
value using the coefficients.
>>y=polyval(p, x)
4/5/2016 DR. MOHAMMED DANISH 9
10. Example 2
No x y
1 0.9 0.9
2 1.5 1.5
3 3 2.5
4 4 5.1
5 6 4.5
6 8 4.9
7 9.5 6.3
Write the MATLAB programing for the 3rd order curve fitting
For the given data.
Solution:
%create vectors x and y with the coordinates of
the data points.
>>x=[0.9 1.5 3 4 6 8 9.5]
>>y=[0.9 1.5 2.5 5.1 4.5 4.9 6.3]
%create a vector p using polyfit command
>>p=polyfit(x,y,3)
For plot, >>xp=0.9:0.1:9.5;
>>yp=polyval(p,xp)
>>plot(x,y, ‘o’, xp,yp)
>>xlabel(‘x’); ylabel(‘y’)
4/5/2016 DR. MOHAMMED DANISH 10
11. 07: Interpolation
Interpolation is the estimation of values between the data points.
You will frequently have occasions to estimate intermediate values
between precise data points.
The function you use to interpolate must pass through the actual data
points - this makes interpolation more restrictive than fitting.
In one dimensional interpolation each point has one independent
variable (say x) and one dependent variable (say y).
In two dimensional interpolation each point has two independent
variable (say x and y) and one depend variable (say z).
4/5/2016 DR. MOHAMMED DANISH 11
12. One dimensional interpolation
If only two data points are available, the points can be connected with
a straight line, and linear equation (polynomial of first order) can be
used to estimate the values between the data points.
A more accurate interpolation can be obtained if instead of
considering all the points in the data set(by using one polynomial that
passes through all the points), only a few data points in the
neighborhood where the interpolation is needed is considered. This
method is called spline interpolation, where many low order
polynomials are used, each is valid only in a small domain of the data
set.
The simplest method of spline interpolation is called linear spline
interpolation
4/5/2016 DR. MOHAMMED DANISH 12
13. One dimensional interpolation
In spline interpolation method every two adjacent points are connected
with a straight line (a polynomial of first degree).
The equation of straight line that passes through two adjacent points
(xi,yi)and (xi+1,yi+1) and that can be used to calculate the value of y for any x
between the points
In linear interpolation the line between two data points has a constant
slope, and there is change in the slope at every point.
A smoother interpolation curve can be obtained by using quadratic or
cubic polynomials. In these methods, called quadratic splines and cubic
splines, a second order or third order polynomial used to interpolate
between every two points.
1 1 1
1 1
i i i i i i
i i i i
y y y x y x
y x
x x x x
4/5/2016 DR. MOHAMMED DANISH 13
14. MATLAB command for
Interpolation
One dimensional interpolation in MATLAB is done with the interp1
function, which has the form:
Yi=interp1(x,y,xi, ‘method’)
Yi=interpolated value
The x & y is the horizontal and vertical co-
ordinates of input data.
The xi is the horizontal coordinate of the
interpolation point independent variable
‘method’ method of interpolation such as
‘nearest’ ‘linear’ ‘spline’ ‘pchip’
4/5/2016 DR. MOHAMMED DANISH 14
15. MATLAB command for
Interpolation
Nearest- return the value of the
data point that is nearest to the
interpolated point.
Linear- uses linear spline
interpolation.
Spline- uses cubic spline
interpolation.
Pchip-uses piecewise cubic
hermite interpolation, also called
cubic
When ‘nearest’ and ‘linear’
methods are used , the values of xi
must be within the domain of x.
If the ‘spline’ or ‘pchip’ methods
are used, xi can have values
outside the domain of x and the
function interp1 perform
extrapolation.
If no method is specified, the
default is ‘linear’.
4/5/2016 DR. MOHAMMED DANISH 15
16. Computing the interpolating
polynomial in MATLAB
Suppose, x= 1.1, 1.2, 1.3
f(x)=0.8912, 0.9320, 0.9636
Interms of MATLAB command,
>>x=[1.1; 1.2; 1.3]
>>y=[0.8912; 0.9320;
0.9636]
>>v=vander(x)
1.2100 1.1000 1.0000
1.4400 1.2000 1.0000
1.6900 1.3000 1.0000
>>a=vf
-0.4600
1.46660
-0.1648
>>polyval(a, 1.15)
Ans=
0.91275
4/5/2016 DR. MOHAMMED DANISH 16
17. Example 3
For function, following data were given. Use linear, spline,
and pchip interpolation methods to calculate the value of y between the
points.
the following program written in the script file that solve
the problem:
>>x=0:1:5
>>y=[1.0 -0.6242 -1.4707 3.2406 -0.7366 -
6.3717]
>>xi=0:0.1:5;
>>yilin=interp1(x,y,xi, ‘linear’)
>>yispl=interp1(x,y,xi, ‘spline’)
( ) 1.5 cos(2 )x
f x x
No x y
1 0 1.0
2 1 -0.6242
3 2 -1.4707
4 3 3.2406
5 4 -0.7366
6 5 -6.3717
4/5/2016 DR. MOHAMMED DANISH 17
18. Slide ends here
Dr. Mohammed Danish
Sr. Lecturer, Malaysian Institute of Chemical and
bioengineering Technology (MICET), UniKL, Alor
Gajah 78000 Melaka, Malaysia
4/5/2016 DR. MOHAMMED DANISH 18