Simple Test to Check Integration Routine

Total Page:16

File Type:pdf, Size:1020Kb

Simple Test to Check Integration Routine

SIMPSON’S RULE Simple Test to check integration routine: Assume r = constant, r = 1

a g(0) ejk d y  2 ae jk  2 a cos( k )  sin( k ) j (7) a q

a = 0.001 m and k = /2 m-1

g(0) (2)(0.001) cos( / 2)  j sin(  / 2)  0  (0.002) j (9)

a = 0.001 m and k = 2 m-1

g(0) (2)(0.001) cos(2 )  j sin(2  )  0.002  0 j (10)

TEST INTEGRALS

The following integrals are not easy to evaluate within the ranges specified. Each function, at some point changes rapidly with small changes of the independent variable, making such functions extremely difficult to integrate if a high degree of accuracy is required.

1 x0.001d x  1000 /1001  0.99900999... 0

Simpson’s rule with matlab (n = 401) = 0.99817473769905 Simpson’s rule with matlab (n = 801) = 0.99858815461115 Simpson’s rule with matlab (n = 1801) = 0.99881766133986

1 dx  0.0134924856495 0 1+(230x-30)2

Simpson’s rule with matlab (n = 401) = 0.01345973729507 Simpson’s rule with matlab (n = 801) = 0.01349241039654 Simpson’s rule with matlab (n = 1801) = 0.01349248564961

4 x2( x 1) 2 ( x  2) 2 ( x  3) 2 ( x  4) 2 d x  1024 / 693  14.776334776 0

082 mat001.doc 9:34 PM 30/04/18 1 1 4

1 2

1 0

8

6

4

2

0 0 0 . 5 1 1 . 5 2 2 . 5 3 3 . 5 4

z = (yq.^2).*((yq-1).^2).*((yq-2).^2).*((yq-3).^2).*((yq-4).^2);

Simpson’s rule with matlab (n = 51) = 14.78272370632085 Simpson’s rule with matlab (n = 801) = 14.77633487632491 Simpson’s rule with matlab (n = 1801) = 14.77633478023653 Simpson’s rule with matlab (n = 2801) = 14.77633477700115

082 mat001.doc 9:34 PM 30/04/18 2 MATLAB FILES

Test for evaluating the integral (int002.doc 5):

7 -1 zp = 1 m, a = 0.001 m and k = 10 m fn010.m function z = fn010(r,k) z = exp(j*k*r)/r; fndistance.m %086 fndistance.m %calculating the distance between aperture point Q and observation point P %4 sep 00 function r = fndistance(xp,yp,zp,xq,yq,zq) r = ((xp-xq).^2+(yp-yq).^2+zp.^2); mint010.m (mid-point rule) %mint010.m %086 %5 sep 00 %numerical integration g(x): see 082 int002.doc %see file fn010.m for function f(r) = exp(jkr)/r %mid-point rule global a %radius of aperture global ay %lower y bound for integration global by %upper y bound for integration global yq %aperture point Q y coordinate global h %y increment global n %number of partitions global fy %integand fy = exp(jkr)/r global gx %integral of exp(jkr) / r global gx1 global gx1real global gx1imag global k %wave number global zp %z coordinate of point P global f2 clear tic %setup a = 0.001; ay = -a; by = a; n = 1024; k = 1e7; xp = 0;

082 mat001.doc 9:34 PM 30/04/18 3 yp = 0; zp = 1; xq = 0; zq = 0; h = (by-ay)/n; yq = ay+h/2:h:by-h/2; r = fndistance(xp,yp,zp,xq,yq,zq); for i = 1:n gx(i) = fn010(r(i),k); end gx1 = h*sum(gx); gx1real = real(gx1); gx1imag = imag(gx1); fprintf('midpoint rule: k = %.4e \n',k) fprintf('midpoint rule: n = %.0f \n',n) fprintf('midpoint rule: h = %.4e \n',h) fprintf('midpoint rule: elapsed time = %.4g \n',toc) gx1real gx1imag

Results for evaluation of integral (5) 7 -1 zp = 1 m, a = 0.001 m and k = 10 m

n t gx real gx imag h 512 0.17 -5.537041605126E-04 -3.190417940619E-04 3.91E-06 1024 0.33 -5.537135956746E-04 -3.190404021620E-04 1.95E-06

MINT012.M (SIMPSON’S RULE)

%mint012.m %086 %6 sep 00 %numerical integration g(x): see 082 int002.doc %see file fn010.m for function f(r) = exp(jkr)/r %Simpson's Rule global a %radius of aperture global ay %lower y bound for integration global by %upper y bound for integration global yq %aperture point Q y coordinate global h %y increment global n %number of partitions global fy %integand fy = exp(jkr)/r global gx %integral of exp(jkr) / r global gx1 global gx1real global gx1imag global k %wave number global zp %z coordinate of point P global f2 clear tic %setup

082 mat001.doc 9:34 PM 30/04/18 4 a = 0.001; ay = -a; by = a; n = 41; %n must be odd ************** k = 1e7; xp = 0; yp = 0; zp = 1; xq = 0; zq = 0; h = (by-ay)/(n-1); c= 2*ones(n,1); c2=2*ones((n-1)/2,1); c(2:2:n)=c(2:2:n)+c2; c(1)=1; c(n)=1; yq = ay:h:by;

r = fndistance(xp,yp,zp,xq,yq,zq); for i=1:n gx(i) = fn010(r(i),k); end gx1 = gx*c; gx1 = gx1*h/3; gx1real = real(gx1); gx1imag = imag(gx1); fprintf('midpoint rule: k = %.4e \n',k) fprintf('midpoint rule: n = %.0f \n',n) fprintf('midpoint rule: h = %.4e \n',h) fprintf('midpoint rule: elapsed time = %.4g \n',toc) gx1real gx1imag

Results for evaluation of integral (5) 7 -1 zp = 1 m, a = 0.001 m and k = 10 m

n t gx real gx imag h 25 -5.531812107398E-04 -3.193332076714E-04 8.33E-05 51 -5.536894728111E-04 -3.190531525594E-04 4.00E-05 -9.19E-04 8.77E-04 101 0.05 -5.537150540180E-04 -3.190407348213E-04 2.00E-05 -4.62E-05 3.89E-05 201 0.11 -5.537166353765E-04 -3.190399878565E-04 1.00E-05 -2.86E-06 2.34E-06 401 0.11 -5.537167342453E-04 -3.190399415876E-04 5.00E-06 -1.79E-07 1.45E-07

082 mat001.doc 9:34 PM 30/04/18 5 %mint016.m %086 %6 sep 00 %numerical integration g(x): see 082 int002.doc %see file fn010.m for function f(r) = exp(jkr)/r %calculation of the wave filed along a line in the X-direction %Simpson's Rule global a %radius of aperture global ay %lower y bound for integration global by %upper y bound for integration global ax %lower x bound for integration global ay %upper x bound for integration global yq %aperture point Q y coordinate global hy %y increment in aperture global hx %x increment in aperture global hpx %x increment for observation points global nx %number of partitions nx-1 global ny %number of partitional ny-1 global npx %number of observation points along x axis global fy %integand fy = exp(jkr)/r global gx %integral of exp(jkr) / r global gx1 global U %intreal of the function over the complete surface global k %wave number global wl %wavelength global xp %coordinates of point P - observation point global yp global zp global xpmin global xpmax global xq %coordinates of point Q in aperture global yq global zq global cx %Simpson coefficients 1 4 2 ... 2 4 1 global cy global ix %counters 1, 2, 3 global iy global ipx clear tic %setup a = 0.00005; ax = -a; bx = a; %*********** nx = 29; %n must be odd ************** ny = 59; npx = 100; wl = 633e-9; k = (2*pi)/wl;

082 mat001.doc 9:34 PM 30/04/18 6 xp = 0; yp = 0; zp = 1; xq = 0; yq = 0; zq = 0; xpmin = 0; xpmax = 0.02;

%Simpson's Coefficients cx= 2*ones(nx,1); cx2=2*ones((nx-1)/2,1); cx(2:2:nx)=cx(2:2:nx)+cx2; cx(1)=1; cx(nx)=1; gx(1) = 0; gx(nx) = 0; cy= 2*ones(ny,1); cy2=2*ones((ny-1)/2,1); cy(2:2:ny)=cy(2:2:ny)+cy2; cy(1)=1; cy(ny)=1; hx = (bx-ax)/(nx-1); xq = ax:hx:bx; hpx = (xpmax-xpmin)/(npx-1); xp = xpmin:hpx:xpmax; for ipx = 1 : npx; for ix = 2:nx-1 by = (a^2 - xq(ix)^2)^0.5; ay = - by; hy = (by-ay)/(ny-1);

yq = ay:hy:by;

for iy =1:ny r = ((xp(ipx)-xq(ix))^2+(yp-yq(iy))^2+zp^2)^0.5; gx1(iy) = exp(j*k*r)/r; %fn010(r,k); end %end for iy gx2 = gx1*cy; gx(ix) = gx2*hy/3; end %end for ix

U(ipx) = gx*cx; U(ipx) = U(ipx)*hx/3; end %end for ipx

intensity = conj(U).* U; intensity = intensity./intensity(1); plot(xp,intensity,'bo') hold; rho = (k*a/zp).*xp; rho(1)=rho(1)+eps;

082 mat001.doc 9:34 PM 30/04/18 7 intensity2 = (2.*besselj(1,rho)./rho).^2; plot(xp,intensity2,'m+') fprintf('nx = %.0f \n',nx) fprintf('ny = %.0f \n',ny) fprintf('k = %.4e \n',k) fprintf('wl = %.4e \n',wl) fprintf('radius a = %.4e \n',a) fprintf('F << 1 = %.4e \n',a^2/(zp*wl)) fprintf('elapsed time = %.4g \n',toc)

082 mat001.doc 9:34 PM 30/04/18 8

Recommended publications