MATLAB EXAMPLES Interpolation and Integration by kp0vUawa

VIEWS: 47 PAGES: 8

• pg 1
```									58:110 Computer-Aided Engineering                          Spring 2005

MATLAB EXAMPLES
Interpolation and Integration

Department of Mechanical and industrial engineering
January 2005
Some useful functions

Polynomial curve fitting
polyfit
p = polyfit(x,y,n), where p is the coefficient vector.
Polynomial evaluation
polyval
y = polyval(p,x).
One-dimensional data interpolation
interp1        yi = interp1(x,y,xi,method),
where method could be ‘linear’, ‘cubic’ ,’spline’ and ‘nearest’

interp2, interp3   Two- and Three-dimensional interpolation

trapz         Trapezoidal numerical integration

cumtrapz        Cumulative trapezoidal numerical integration

Interpolation
Here is the example to get the polynomial fitting by Lagrange interpolation:
Suppose the original data set is:
x        -2          -1       0        1     2
y        -39         -15     -5        -3   39
There are five sets of (x,y) above, polyfit can give the 4th order polynomial form by
Lagrange interpolation. To compare, we also use interp1 to give the more smooth
fitting curve by piecewise cubic Hermite interpolation.
The M-file (L_interperlation.m) is given to plot the fitting curve in the following:
% Lagrange interpolating polynomial fitting   x1=linspace(-2,2,50);

x=[-2 -1 0 1 2];                              y1=polyval(p,x1);

y=[-9 -15 -5 -3 39];                          y2=interp1(x,y,x1,'cubic');

[m n]=size(x)                                 plot(x,y,'o',x1,y1,'-',x1,y2,'.');

p=polyfit(x,y,n-1)                            xlabel('x'),ylabel('y=f(x)')
title ('Lagrange and Piecewise cubic Hermite interpolation')
Interpolation
To run this example in MATLAB:
>> L_interpolation
p=
3.0000 2.0000 -7.0000 4.0000 -5.0000
That means, the 4th order polynomial is:

f ( x)  3 x 4  2 x 3  7 x 2  4 x  5

The right figure shows the fitting curve
and the original points (circle).
The solid line is the 4th order
polynomial by lagrange interpolation.
The dot curve is fitted by piecewise
cubic Hermite interpolation.
Integration
Consider the following integration:
1

2
xe x dx
0

1
The accurate integration is:       I    e  1  0.859140914
2
Use the numerical integration, here only try trapezoidal method and Simpson method.
Assuming the step size is h = 0.1. In MATLAB, first use the trapezoidal method:
>>x=linspace(0,1,11);
>> y=x.*exp(x.^2);
>>s=trapz(x,y);
>> num2str(s,'%20.9f')
ans =
0.865089832
Integration
To try Simpson’s method, we need write some codes (I_simpson.m):

% Simpson's method of Numerical integration
x=linspace(0,1,11);
s=0.0;
f = inline('x.*exp(x.^2)');
for k=1:5
f0=f(x(k*2-1));
f1=f(x(k*2));                         Run this M-file in command window:
f2=f(x(k*2+1));                       >> I_simpson
s=s+(f0+4*f1+f2)*0.1/3.;              ans =
end
0.859193792
num2str(s,'%20.9f')
Integration
quadrature. The default error tolerance is 10e-6.

>> f=inline('x.*exp(x.^2)')
>> num2str(s,'%20.9f')
ans =

0.859140934
Integration-Summary
Compare the results of different methods (all keep 9 digits) in the following table:

Method                      Solution                  Error
Exact Integration              0.859140914                   /
Trapezoidal (h = 0.1)            0.865089832                0.690%
Simpson’s (h = 0.1)             0.859193792               0.00615%