100% found this document useful (1 vote)
432 views5 pages

Trapezoidal Rule and Simpson's Rule

The document introduces numerical integration methods for computing approximations of definite integrals, including: 1) The trapezoidal rule, which approximates the integral using trapezoids formed by the function values at evenly spaced intervals. Octave/MATLAB uses the trapz function for this. 2) Simpson's rule, which uses quadratic approximations and produces exact results for quadratic functions. An implementation in Octave is provided. 3) Other methods available in MATLAB like adaptive quadrature and Gaussian quadrature are also mentioned.

Uploaded by

Sai Vandana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
432 views5 pages

Trapezoidal Rule and Simpson's Rule

The document introduces numerical integration methods for computing approximations of definite integrals, including: 1) The trapezoidal rule, which approximates the integral using trapezoids formed by the function values at evenly spaced intervals. Octave/MATLAB uses the trapz function for this. 2) Simpson's rule, which uses quadratic approximations and produces exact results for quadratic functions. An implementation in Octave is provided. 3) Other methods available in MATLAB like adaptive quadrature and Gaussian quadrature are also mentioned.

Uploaded by

Sai Vandana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

8.

6NumericalIntegration
Thispageintroducesmethodsforcomputingnumericalapproximationsofdefiniteintegrals.
Octave/MATLAB
Mathematica
Sage

Octave/MATLAB
Trapezoidalapproximations
Supposex

givesthepartitionofinterval[a, b]inton evenlyspacedsubintervals,i.e.supposex = a + ixfor


0, , n withx =
.Thenifystoresthevaluesoff atx , , x inarowvectorandhholdsthecommonlength
0

i =

, , xn

ba

ofallsubintervals(namelyx
forthepartitionx

, , xn

ba

),thenh*trapz(y)givesthetrapezoidalmethodapproximationto

f (x) dx

Thecodebelowcomputesthetrapezoidalapproximationsto
subintervals,thenusing100subintervals.

3
0

dx

firstusingthepartitionwith20evenlyspaced

>>a=0;b=3;n=20;
>>h=(ba)/n;
>>x=a:h:b;
>>y=x.^2;
>>h*trapz(y)
ans=
9.0113
>>n=100;
>>h=(ba)/n;
>>x=a:h:b;
>>y=x.^2;
>>h*trapz(y)
ans=
9.0004
Notsurprisinglyusingmoresubintervalsyieldsaresultclosertotheexactvalue9.
Ifxstoresanonevenlyspacedpartititionof[a, b],andyholdsthecorrespondingvaluesoff ,thentrapz(x,y)computes
b

thetrapezodialapproximationto

f (x) dx

forthegivenpartition.(Thissyntaxcanbeusedforevenlyspacedpartitionsas

well,butit'sslightlylessefficientthanourfirstapproachforthatcommonspecialcase.)

>>x=[0.05.1.3.32.4.55.711.31.41.451.61.81.922.22.32.62.82.913];
>>y=x.^2;

>>trapz(x,y)
ans=
9.0217
It'snotdifficulttowriteyourownimplementationofthetrapezoidalmethodinMATLAB.Feelfreetodosoasanexercise.

Simpson'srule
Although(asofthiswriting)MATLABdoesnotofferabuiltinimplementationofSimpson'sruleinitsmostbasicform,you
candownloadonefromtheMATLABCentralfileexchange.Thecodeinsimp.misabarebonesimplementationof
Simpson'srule.

functionarea=simp(fcn,a,b,n)
%UsesSimpson'sruletoapproximatetheintegralofthefunction
%withhandlefcnfromatobusingn*pairs*ofevenlyspaced
%subintervals.
%Author:RSmyth
%Version1.08/22/2013
h=(ba)/(2*n);%Subintervalspacing
x=a:h:b;%Partitionpoints
y=fcn(x);%Functionvaluesatpartitionpoints
i=0:2*n;
coeffs=3+(1).^(i+1);%Kludgetogeneratepatternofalternating2sand4s
coeffs(1)=1;coeffs(end)=1;
area=h/3*sum(coeffs.*y);%Simpson'sruleformula
octave:1>simp(@(x)x.^2,0,3,1)
ans=9
octave:2>formatlong
octave:3>simp(@(x)1./x,1,exp(1),3)
ans=1.00018687674140
octave:4>simp(@(x)1./x,1,exp(1),22)
ans=1.00000007582682
3

NoticethatSimpson'sruleproducedtheexactvaluefor x dxusingjustonepairofsubintervals.Isthisasurprise?
Shouldn'tbe.Simpson'sruleusesquadraticapproximationsandthusproducesexactvaluesforquadraticintegrands.
2

Othermethods
MATLABhasseveralbuiltinmethodsfornumericallyapproximatingintegralsincludingtheintegralmethod(introduced
earlier)whichusesglobaladaptivequadratureandthequadgkmethod(alsointroducedearlier)whichusesadaptiveGauss
Kronradquadrature.FollowthelinkstotheMATLABdocsformoreinformationaboutthesemethods.

Mathematica
Trapezoidalapproximations
Mathematica'snumericalintegrationroutineNIntegrate[](introducedearlier)canbemadetousetrapezoidal

approximationsbysettingtheMethodoptionto"TrapezoidalRule".

Ifyouwishtocontrolthepartitionyoucanimplementthetrapezoidalmethodasbelow.

Simpson'srule

Sage
Trapezoidalapproximations
Ordinarilyyouwouldusethenumerical_integral()command(introducedearlier)tocomputenumerical
approximationstodefiniteintegralsinSage.Ifyouwishtoimplementthetrapezoidalmethodthereareofcoursemanyways
todoso,oneofwhichisillustratedbelow.
1
2
3
4
5
6

g(x)=x^2
f=Piecewise([[(0,3),g]])
ft=f.trapezoid(20)#Createpiecewiselinearapproximationtooriginalfunction.
n(ft.integral(definite=True))#Theexactvalueoftheintegralofthepiecewiselinear
#functionisthetrapezoidalestimatetotheintegralof
#theoriginalfunction.

Evaluate

Simpson'srule

1
2
3
4
5
6
7

#ImplementationofSimpson'srule
f(x)=1/x;a=1;b=exp(1);n=3;#Editthislinetochangeintegrand,intervalornumberofsubdivisions
h=(ba)/(2*n)
vals=[f(a+i*h)foriinrange(0,2*n+1)]
coeffs=[3(1)^iforiinrange(0,2*n+1)]
coeffs[0]=1;coeffs[2*n]=1;
float(h/3*vector(coeffs).dot_product(vector(vals)))

Evaluate

You might also like