Download presentation
Presentation is loading. Please wait.
Published byDominic Weaver Modified over 9 years ago
1
MATLAB EXAMPLES Interpolation and Integration 58:111 Numerical Calculations Department of Mechanical and Industrial Engineering
2
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, interp3Two- and Three-dimensional interpolation trapzTrapezoidal numerical integration cumtrapzCumulative trapezoidal numerical integration quadNumerically evaluate integral, adaptive Simpson quadrature
3
Interpolation Suppose the original data set is: Here is the example to get the polynomial fitting by Lagrange interpolation: x-2012 y-39-15-5-339 There are five sets of (x,y) above, polyfit can give the 4 th 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 x=[-2 -1 0 1 2]; y=[-9 -15 -5 -3 39]; [m n]=size(x) p=polyfit(x,y,n-1) x1=linspace(-2,2,50); y1=polyval(p,x1); y2=interp1(x,y,x1,'cubic'); plot(x,y,'o',x1,y1,'-',x1,y2,'.'); xlabel('x'),ylabel('y=f(x)') title ('Lagrange and Piecewise cubic Hermite interpolation')
4
Interpolation To run this example in MATLAB: >> L_interpolation p = 3.0000 2.0000 -7.0000 4.0000 -5.0000 The right figure shows the fitting curve and the original points (circle). The solid line is the 4 th order polynomial by lagrange interpolation. The dot curve is fitted by piecewise cubic Hermite interpolation. That means, the 4 th order polynomial is:
5
Integration Consider the following integration: The accurate integration is: 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
6
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)); f2=f(x(k*2+1)); s=s+(f0+4*f1+f2)*0.1/3.; end num2str(s,'%20.9f') Run this M-file in command window: >> I_simpson ans = 0.859193792
7
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
8
Compare the results of different methods (all keep 9 digits) in the following table: MethodSolutionError Exact Integration0.859140914/ Trapezoidal (h = 0.1)0.8650898320.690% Simpson’s (h = 0.1)0.8591937920.00615% Adaptive Simpson0.8591409340.0000023% Integration-Summary To estimate error by step size h: For Trapezoidal method, it’s O(h 2 )=O(0.01) For Simpson’s method, it’s O(h 4 )=O(0.0001)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.