DOIG PHYSICS WITH MATLAB MATHEMATICAL ROUTIES COMPUTATIO OF OE-DIMESIOAL ITEGRALS In Cooper School of Physics, University of Sydney in.cooper@sydney.edu.u DOWLOAD DIRECTORY FOR MATLAB SCRIPTS mth_integrtion_1d.m Demonstrtion mscript evluting the integrl of two functions using number of different methods simpson1d.m Function to give the integrl of function using Simpson s 1/3 rule. UMERICAL ITEGRATIO COMPUTATIO OF OE-DIMESIOAL ITEGRALS The function simpson1d.m is very verstile, ccurte nd esy to implement function tht cn be used to evlute definite integrl of function between lower bound nd n upper bound. It is esier to use thn the stndrd Mtlb integrtion functions such s qud. The function simpson1d.m is described in detil below. We wnt to compute number expressing the definite integrl of the function f(x) between two specific limits nd b b I f ( x)dx The evlution of such integrls is often clled qudrture. Doing Physics with Mtlb 1
We will consider the following integrnds to test the ccurcy of different integrtion procedures: (1) f ( x) cos( x) (2) ( ) e jx f x The integrls re evluted over qurter cycle in n ttempt to find minimum number of prtitions of the domin tht produce ccurte results within resonble time. For the testing procedures, the following integrls re to be evluted from = to b = /2 /2 I cos( x)dx nd /2 jx I e dx These integrls cn be evluted nlyticlly nd the exct nswers re /2 /2 I cos( x)dx sin( x) sin( / 2) sin() 1 /2 /2 jx 1 jx 1 j /2 j j I e dx e e 1 1 j The numericl procedures ssume tht neither the integrnd f(x) nor ny of its derivtives become infinite t ny point over the domin of the integrtion [, b], nd tht the limits of integrtion nd b re finite. Furthermore, we ssume tht f(x) cn be computed, or its vlues re known t points x c where c = 1, 2,, tht re distributed in some mnner over the domin [, b] with x 1 = nd x = b. The qulifier closed signifies tht the integrtion involves the vlues of the integrnd t both the end-points. If only one or none of the end points re included, the qulifiers used re semi-closed nd open respectively. A mjor problem tht rises with non-dptive methods is tht the number of prtitions of the function required to provide given ccurcy is initilly unknown. One pproch to this problem is to successively double the number of prtitions, nd compre the results s the number of prtitions increse. Doing Physics with Mtlb 2
Closed Rectngle Rule The region from to b is divided into rectngles of equl width x h where x h b 1 with ech rectngle centre occurring t the points x 1 =, x 2 = x 1 + h, x = b. For exmple, the plot below shows the domin divided into 4 rectngles, = 4. However, the sub-division of the domin by this mens extends from - h/2 to b + h/2 nd not simply from to b. 12 1 8 6 4 2 1 2 3 4 The integrl is pproximted by the contribution from ech rectngle, such tht I f ( x ) x i1 i For the cse shown in the plot bove where = 4, the integrl is pproximted by I f ( x ) f ( x ) f ( x ) f ( x ) h where 1 2 3 4 b h. 3 Doing Physics with Mtlb 3
Open Midpoint Rule The mid-point rule is the first member of fmily of open ewton-cotes rules corresponding to qudrtic, cubic nd higher-order interpolting polynomils with evenly spced points. In the open midpoint rule, the re of ech rectngle is dded to find the integrl. The domin to b in divided into prtitions, with the width of ech prtition being h = (b )/. The function is evluted t the midpoint x i of ech rectngle where x i = + (h/2)(2-1). The vlue of the integrl is then given by b I f ( x)d x h f ( xi ) 1 Doing Physics with Mtlb 4
Trpezoidl Rule The function f(x) is pproximted by stright line segment connecting djcent points. The re under the curve is then pproximted by dding the re of ech trpezium. The intervl to b is divided into -1 prtitions of width h (h x) where h = (b )/( - 1). The re of ech prtition is simply the re of trpezium which is its bse times its men height. The re of the i th trpezium is h fi f 2 i1 where f i f(x i ) nd i = 1, 2,..-1 Summing ll the trpezi gives the composite, closed trpezoidl rule 1 f1 f f1 f I h f i h fi 2 2 1 2 For evenly distributed spcings, the composite rule is equivlent to the trpezoidl version of the closed ewton-cotes rule. The Mtlb function, trpz implements procedure to clculte the integrl by the trpezoidl rule. For exmple, the integrtion of the function y 1 w.r.t the vrible x Integrl_1 = trpz(x,y1) % estimte of the integrl Doing Physics with Mtlb 5
Simpson s 1/3 rule This rule is bsed on using qudrtic polynomil pproximtion to the function f(x) over pir of prtitions. -1 is the number of prtitions where must be odd nd h = (b ) / (-1). The integrl is expressed below nd is known s composite Simpson s 1/3 rule. h I f1 f 4( f2 f4... f2 2( f3 f5... f1) 3 Simpson s rule cn be written vector form s h I cf 3 T where f f f c f. 14 2 4... 2 41 nd 1 2... Simpson s rule is n exmple of closed ewton s-cotes formul for integrtion. Other exmples cn be obtined by fitting higher degree polynomils through the pproprite number of points. In generl we fit polynomil of degree through +1 points. The resulting polynomils cn them be integrted to provide n integrtion formul. Becuse of the lurking oscilltions ssocited with the Gibbs effect, higher-order formuls re not used for prcticl integrtion. simpson1d.m The function f nd the lower bound nd the upper bound b re pssed onto the function (in the order f,, b) nd the function returns the vlue of the integrl function integrl = simpson1d(f,,b) % [1D] integrtion - Simpson's 1/3 rule % f function = lower bound b = upper bound % Must hve odd number of dt points % Simpson's coefficients 1 4 2 4... 2 4 1 nums = length(f); % number of dt points sc = 2*ones(numS,1); sc(2:2:nums-1) = 4; sc(1) = 1; sc(nums) = 1; h = (b-)/(nums-1); integrl = (h/3) * f * sc; Doing Physics with Mtlb 6
EXAMPLES (1) /2 I cos( x)dx 1 (2) /2 I e dx 1 j jx Method Estimte Closed Rectngle (1) 1.5 (2) 1.5 + 1.5i (1) 1.8 (2) 1.8 + 1.8i Open-Midpoint (1).78 (2).78 +.78i (1).68 (2).733 +.733i Trpezoidl (1) 1. (2) 1. + 1.i (1) 1. (2) 1. + 1.i Simpson s 1/3 (1) 1. (2) 1. + 1.i All the integrtions took less thn one second on fst Windows computer. Even with only points, the Simpson s 1/3 rule estimte ws equl to the exct vlues. Doing Physics with Mtlb 7