1 / 8

MATLAB EXAMPLES Interpolation and Integration

MATLAB EXAMPLES Interpolation and Integration. 58:110 Computer-Aided Engineering Spring 2005. Department of Mechanical and industrial engineering January 2005. Some useful functions. Interpolation.

gino
Télécharger la présentation

MATLAB EXAMPLES Interpolation and Integration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB EXAMPLESInterpolation and Integration 58:110 Computer-Aided Engineering Spring 2005 Department of Mechanical and industrial engineering January 2005

  2. Some useful functions

  3. Interpolation Here is the example to get the polynomial fitting by Lagrange interpolation: Suppose the original data set is: • 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 • 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 That means, the 4th order polynomial is: • 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.

  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. Integration-Summary Compare the results of different methods (all keep 9 digits) in the following table: 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)

More Related