291184041 05/28/16 1 Mid point trap rule Evaluate the function and its second derivatives at an evenly spaced set of values 1. (Code accurate to order h2 is in the box to the left. Full code in MPTrap.zip) h=(end-beg)/N ai=0 for I=1, N x=beg+i*h-h/2 ai=ai+fun(x) enddo ai=ai h h (end beg ) / N xi ih h / 2 i 1,..., N (1) The integral from beg to end can be written as N f x dx end beg i 1 xi h /2 f x dx xi h /2 (2) Expand the function about the values xi f ( x) f ( xi ) x xi f '( xi ) x xi 2! 2 f ''( xi ) x xi 3! 3 f '''( xi ) So that end beg N f x dx f xi f ''' xi i 1 3! dx f ' xi xi h / 2 i 1 N N xi h / 2 xi h / 2 xi h / 2 x xi N f iv xi i 1 4! dx i xi h / 2 xi h / 2 x xi In each of the N integrals let t=x-xi end beg N f x dx f xi h / 2 i 1 N i 1 f iv xi 4! N dt f ' xi h/2 h/2 i 1 h/2 h / 2 4 f '' xi i 1 2 f iv ( xi ) ... (3) xi h / 2 x xi x h / 2 2 dx i (4) dx N f '' xi i 1 2 tdt 4 4! N x xi dx x h / 2 i 1 3 xi h / 2 x xi h/2 h / 2 N f ''' xi i 1 3! t 2 dt h/2 h / 2 t 3 dt (5) t dt 4 h / 2 When integrated from -h/2 to h/2, all of the odd terms integrate to zero and the rest are only a little harder so that by expanding about the midpoint of each region an integral in general can be written as end beg N N i 1 i 1 f x dx hf xi f '' xi 2 h 3 N f iv xi 2 h 5 2 3 2 4! 5 2 i 1 (6) The end points are never evaluated. Simplifying a bit end beg N f x dx h f xi i 1 N h2 N h4 h f '' xi h f iv xi 24 i 1 1920 i 1 (7) Integrate the second derivative end beg N h 2 N iv h4 h f xi h f vi xi 24 i 1 1920 i 1 i 1 f '(end ) f '(beg ) N f '' x dx h f '' xi (8) So that through h4 end beg f '(end ) f '(beg ) 4 N h 2 N h f x dx h f xi h h f iv xi iv 24 h f xi 1920 i 1 i 1 24 i 1 N 2 (9) 291184041 05/28/16 1 1 1 1 1 1 7 10 3 5760 24 24 1920 24 8 3 10 24 8 30 So that end beg N f x dx h f xi i 1 h2 7h 4 f '(end ) f '(beg ) f '''(end ) f '''(beg ) O(h6 ) (10) 24 5760 Testing Let h=1 1 1. dx 1 h 1 1 0 1 1 x2 2. xdx 2 0 0 3 1 1 x 3. x dx 3 0 2 1 4. x dx 3 0 1 x dx 4 5. 0 0 4 1 1 h 2 1 3 2 1 1 h f 1 1 2 2 24 1 h2 1 1 8 1 h 2 Checks the 1/24 in the h2 term 4 24 4 12 24 3 x 4 1 4 1 1 7 1 1 1 3 6 6 8 24 5760 8 8 4 x 5 1 5 1 1 7 1 1 7 1 1 1 7 4 24 16 24 5760 16 6 240 2 8 3 120 0 5 1 0 1 48 1 15 40 7 2 120 240 5 Checks the -7/5760 in the h4 term 1 x6 x dx 6 6. 0 1 5 0 1 6 1 1 7 1 5 7 1 1 5 7 5 60 1 32 24 5760 32 24 96 8 4 3 12 1 16 1 3 20 7 8 12 8 12 6 This means that a Lagrange polynomial through 6 points will integrate exactly. A sample integral with all derivatives is y 1 AiGau y exp x 2 dx 0 2 291184041 05/28/16 3 Figure 1 e(-x*x) and its first through fourth derivatives as functions of x. PlotGaussAndDers.zip The even derivatives are out of phase with the odd derivatives. The first and third derivatives are both nearly 0 at x equal to zero, the fourth will thus me a maximum at x=0 and the fifth a minimum. This will produce locations with dramatically smaller error estimates. The coefficient 1 / is designed to make the integral from –infinity to infinity equal to 1. The term e36 is close enough to zero that the integral from -6 to 6 is equivalent to –infinity to infinity. The integral to the last few points is 9.97767801705156E-01 ±8.01121047431118E-03. This may seem fair for 10-point integration, but the expected error is needed. Each term beyond the first in the integration from -6 to 6 is 0 owing to the fact that the functions and its derivatives are effectively zero at both ends of the integration region. As can be seen in Trap rule error in periodic functions.docx, this does not mean that the integral error is zero, but it converges rapidly a a function of N. The code is in MPTrap.zip 291184041 05/28/16 MPTrap.zip Value=1.00002 +- 0.002 err≅sqrt(.00232+0.00052+0.000112 + 0.0000192) = 0.0024 the bulk of the error estimate comes from the 10 point evaluation. The 13 point evaluation has almost converged. Figure 2 The error bars are from the 10 point integration. 4 291184041 05/28/16 In the code I use the square root of the sum of the squares of the last term in the integral estimates for each region to estimate the standard deviation of the value. X=(I-0.5D0)*H+BEG FMID=RPIIGAUSS(X,P,P3) T1=H*FMID T2=H2*(P-PINIT)/24 T4=-7*H4*(P3-P3INIT)/5760 SUM=SUM+T1+T2+T4 ERREST=H2*3*T4 estimated size of omitted T6 term ERAN=SQRT(I*1D0)*1D-15*SUM ERR=ERREST+ERAN RELERR=ABS(ERR/SUM) WRITE(1,'(1P,4E24.14)')X+H/2,SUM,ERR,RELERR Figure 3 Values/(error estimates) In figure 2 it can be seen that the error estimate decreases rapidly as the derivatives at the ends of the integration range become equal. These equalities also occur for some locations in the middle of the interval. 5 291184041 05/28/16 With 100 points, the error estimate is the same for all three values. Figure 4 Relative error vs x for 100 integration points. 6 291184041 05/28/16 7 Figure 5 relative error vs x for 1000, 10000, and 100000 points. The standard deviation on the far right is dominated by the truncation error in adding so many terms. On the left, the function has contributions from the derivative terms in the integration equation and h needs to be small. The curves in figure 5 imply that the best estimates are from 4000 points for which the relative error is less than 10-13. 1 Beware of the ½, in Fortran this must be written either as 1d0/2 or as 0.5d0. If it is written as ½ integer arithmetic truncates it to zero. If it is written as 0.5, it is only accurate to 7 digits as it is a real number.