58:110 Computer-Aided Engineering
MATLAB EXAMPLES
Interpolation and Integration
Department of Mechanical and industrial engineering
January 2005
Spring 2005
Some useful functions
polyfit
Polynomial curve fitting
p = polyfit(x,y,n), where p is the coefficient vector.
polyval
Polynomial evaluation
y = polyval(p,x).
interp1
One-dimensional data interpolation
yi = interp1(x,y,xi,method),
where method could be ‘linear’, ‘cubic’ ,’spline’ and ‘nearest’
interp2, interp3
trapz
cumtrapz
quad
Two- and Three-dimensional interpolation
Trapezoidal numerical integration
Cumulative trapezoidal numerical integration
Numerically evaluate integral, adaptive Simpson quadrature
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
0
2
xe x dx
1
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:
The accurate integration is:
>>x=linspace(0,1,11);
>> y=x.*exp(x.^2);
>>s=trapz(x,y);
>> num2str(s,'%20.9f')
ans =
0.865089832
I
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));
Run this M-file in command window:
f1=f(x(k*2));
>> I_simpson
f2=f(x(k*2+1));
s=s+(f0+4*f1+f2)*0.1/3.;
ans =
end
0.859193792
num2str(s,'%20.9f')
Integration
Use the built-in function quad, which uses more accurate adaptive Simpson
quadrature. The default error tolerance is 10e-6.
>> f=inline('x.*exp(x.^2)')
>> s=quad(f,0,1);
>> 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%
Adaptive Simpson
0.859140934
0.0000023%
To estimate error by step size h:
For Trapezoidal method, it’s O(h2)=O(0.01)
For Simpson’s method, it’s O(h4)=O(0.0001)
© Copyright 2026 Paperzz