[100] 003 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com revised on 2012.12.08 cemmath The Simple is the Best Chapter 3 Integration 3-1 3-2 3-3 3-4 3-5 3-6 3-7 3-8 Single Integration Integrated Function and Plot Multiple Integration Area Properties For Closed Curves in 2D Surface Integral Line Integral Summary of Syntax References Integration of mathematical functions is primarily based on the Gauss quadrature, and utilizes a new Umbrella ‘int’. For users’ convenience however, well-known trapezoidal rule and Simpson’s rule are also provided by means of Spokes. In addition, the line integrals and the surface integrals are also discussed. Section 3-1 Single Integration โ Syntax of Umbrella ‘int’. Integration of a function with respect to a single independent variable can be written as ๐ ๐ผ = ∫ ๐(๐ฅ)๐๐ฅ ๐ The syntax of our new Umbrella ‘int’ for this situation is int .x[n=26,g=1](a,b) ( <<opt>>,f(x), g(x), h(x), … ) 1 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com or x = (a,b).span(n,g=1); int [x] ( <<opt>>,f(x), g(x), h(x), … ) and the relevant Spokes are .poly(nth) .quad(num) .togo(F) .return Newton-Cotes integration with polynomial of degree nth<=8 Gauss-Legendre quadrature with number of points <=6 takeout an integrated function as a matrix F return an integrated function as a matrix .plot plot integrated function for one-variable case The grid span defined in the Hub of ‘int’ implies that the given interval ๐ ≤ ๐ฅ ≤ ๐ is first divided into ๐ − 1 subintervals (equal spacing if ๐ = 1) and the integral is evaluated by means of local sums ๐ ๐ฅ๐ ๐ผ ≅ ∑∫ ๐(๐ฅ)๐๐ฅ ๐=2 ๐ฅ๐−1 โ Default Case. Integration over a given subinterval ๐ฅ๐−1 ≤ ๐ฅ ≤ ๐ฅ๐ is by default performed based on the fourth-degree Gauss-Legendre quadrature, i.e. the Spoke ‘quad(5)’ is used by default if not specified explicitly. (Example 3-1-1) Let us start with a simple example which has an exact value 1 ∫ 0 1 ๐ ๐๐ฅ = 2 1+๐ฅ 4 This can be confirmed by the following commands %> Umbrella 'int' #> int .x[5](0,1) ( 1/(1+x*x) ); #> pi/4; which result in ans = 0.7853982 ans = 0.7853982 2 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com It can be noted that the numerical value agrees with the exact one up to 7 digits, even though we use only 5 nodes for the grid span. Nevertheless, the total number of evaluating the given function is actually (5 − 1) × (4 + 1) = 20 for this case, as can be seen later. โ โ Comparison with Numerical Theories. The most simplest methods of integration among the existing numerical theories would be the lower and upper summations, ๐ผ๐ฟ , ๐ผ๐ , based on the zeroth-degree polynomial (i.e. constant over each subinterval) ๐ ∫ ๐(๐ฅ)๐๐ฅ ≅ ๐ผ๐ฟ = โ[๐(๐ฅ1 ) + ๐(๐ฅ2 ) + ๐(๐ฅ3 ) + โฏ + ๐(๐ฅ๐−1 )] ๐ ๐ ∫ ๐(๐ฅ)๐๐ฅ ≅ ๐ผ๐ = โ[๐(๐ฅ2 ) + ๐(๐ฅ3 ) + ๐(๐ฅ4 ) + โฏ + ๐(๐ฅ๐ )] ๐ where โ = (๐ − ๐)/(๐ − 1) and ๐ฅ๐ = ๐ + โ(๐ − 1), ๐ = 1,2,3, … , ๐. For monotonously increasing functions, it is well known that ๐ ๐ผ๐ฟ ≤ ∫ ๐(๐ฅ)๐๐ฅ ≤ ๐ผ๐ ๐ As an example, for a special case of ๐(๐ฅ) = ๐ 2๐ฅ , 0 ≤ ๐ฅ ≤ 1 and ๐ = 5, we have ๐ผ๐ฟ = (1/4)[๐ 0 + ๐ 0.5 + ๐ 1 + ๐ 1.5 ] = 2.4621730 ๐ผ๐ = (1/4)[๐ 0.5 + ๐ 1 + ๐ 1.5 +๐ 2 ] = 4.0594371 1 1 ∫ ๐ 2๐ฅ ๐๐ฅ = (๐ 2 − 1) = 3.1945280 2 0 This can be also confirmed from Figure 1 where the lower-step and upper-step functions are shown together with the given function. Corresponding Cemmath commands are #> .hold; plot .x (0,1) ( exp(2*x) ); 3 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com plot .x[5](0,1) ( exp(2*x) ).lstep; plot .x[5](0,1) ( exp(2*x) ).ustep; plot .x[5](0,1) ( exp(2*x) ).ystem; #> plot; Figure 1 Zeroth- degree polynomial integration (Example 3-1-2) For practical comparison, Cemmath provides a useful Spoke ‘poly’ which represents the degree of polynomial used for integration in each subinterval. ๐ผ๐ฟ = (1/4)[๐ 0 + ๐ 0.5 + ๐ 1 + ๐ 1.5 ] = 2.4621730 ๐ผ๐ = (1/4)[๐ 0.5 + ๐ 1 + ๐ 1.5 +๐ 2 ] = 4.0594371 #> int .x[5](0,1) ( exp(2*x) ).poly(0); #> int .x[5](1,0) ( exp(2*x) ).poly(0); ans = 2.4621730 ans = -4.0594371 In the above, it should be noted that Spoke ‘poly(0)’ always starts from the leftmost point, and thus the grid direction must be reversed to evaluate ๐ผ๐ corresponding to upper-step approximation of a given function. Of course from definition, the numerical results are exactly the same as the aforementioned values, where the minus sign is due to reversing grid direction. โ โ Trapezoidal Rule. The frequently used trapezoidal rule corresponds to the case of the first-degree polynomial approximation over each subinterval. (Example 3-1-3) For the aforementioned example, numerical integration 4 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com becomes (underlying numerical theories are omitted here) 1 1 ∫ ๐ 2๐ฅ ๐๐ฅ ≅ (0.25)[๐ 0 + 2๐ 0.5 + 2๐ 1 + 2๐ 1.5 + ๐ 2 ] 2 0 = 3.2608051(≠ 3.1945280) which can be confirmed by #> int .x[5](0,1) ( exp(2*x) ).poly(1); ans= 3.2608051 โ โ Simpson’s 1/3 Rule. The Simpson’s 1/3 rule is based on the seconddegree polynomial approximation over each subinterval. To apply the Simpson’s 1/3 rule, the range of integration ๐ ≤ ๐ฅ ≤ ๐ is first divided into 2๐ subregions. Then, the second-degree polynomial approximation is performed over ๐ฅ๐ ≤ ๐ฅ ≤ ๐ฅ๐+2 . This is equivalent to that the interval of ๐ ≤ ๐ฅ ≤ ๐ is divided into ๐ subintervals and each subinterval is again divided into 2 subintervals. Therefore, the corresponding syntax for the Simpson’s 1/3 rule is int .x[n+1,g=1](a,b) ( f(x) ) .poly(2) // 2n uniform h for g=1 Note that the Hub span is always associated with the total number of nodes not the number of subintervals. (Example 3-1-4) For the above example, numerical integration becomes 1 1 ∫ ๐ 2๐ฅ ๐๐ฅ ≅ (0.25)[(๐ 0 + 4๐ 0.5 + ๐ 1 ) + (๐ 1 + 4๐ 1.5 + ๐ 2 )] 2 0 = 3.1956051(≠ 3.1945280) which can be confirmed by #> int .x[3](0,1) ( exp(2*x) ).poly(2); ans= 3.1956051 In the above, it should be noticed that the Hub span has changed from the previous case, i.e. the grid size has changed from 5 to 3. As was discussed earlier, the grid size creates the number of subintervals for integration, i.e. ๐ − 5 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com 1, while Spoke ‘poly’ determines how many further sub-divisions are required. โ โ Newton-Cotes Integration. In general, numerical methods of integration discussed above are classified as the Newton-Cotes integration. For instance, Newton-Cotes integration with the second-degree polynomial is the same as the Simpson’s 1/3 rule. The Spoke ‘poly’ .poly(nth) performs the integration with specified degree of polynomials. Here, we employ polynomials at most up to eighth-degree. Higher degrees of polynomials are forced to become the eighth-degree internally in Cemmath. Let us discuss what the behavior is associated with the following syntax int .x[n=26,g=1](a,b) ( <<opt>>, f(x) ) .poly(nth) As was mentioned so far, the Hub span distributes ๐ nodes and thus ๐ − 1 equispaced subintervals are generated (if ๐ = 1). For each subinterval, ๐๐กโ degree of polynomial is employed. This requires that each subinterval must be again divided into ๐๐กโ subintervals (note that both ends of each subinterval are included at data points). Therefore, the original interval (๐, ๐) involves a total of (๐ − 1)๐๐กโ subregions, where ๐๐กโ ≤ 8. โ Gauss-Legendre quadrature. Gauss-Legendre quadrature, simply called Gauss quadrature, is based on polynomials with pre-selected nodes and coefficients. In view of accuracy, Gauss quadrature is the most powerful method of integration, although error evaluation is not as simple as in the Newton-Cotes methods. (Example 3-1-5) Consider the following case 1 ∫ ๐ ๐ฅ ๐๐ฅ = ๐ − −1 1 ≅ 2.35040238 … ๐ This can be solved by 6 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com #> int .x[2](-1,1) ( exp(x) ).quad(1); #> int .x[2](-1,1) ( exp(x) ).quad(2); #> int .x[2](-1,1) ( exp(x) ).quad(3); which results in ans = ans = ans = 2.0000000 2.3426961 2.3503369 These values can be confirmed by the numerical formulation of Gauss quadrature 1 ๐ผ0 = ∫ ๐ ๐ฅ ๐๐ฅ ≅ 2๐ 0 = 2 −1 1 ๐ผ1 = ∫ ๐ ๐ฅ ๐๐ฅ ≅ ๐ −1/√3 + ๐ 1/√3 = 2.3426961 −1 1 5 8 ๐ผ2 = ∫ ๐ ๐ฅ ๐๐ฅ ≅ (๐ −√0.6 + ๐ √0.6 ) + ๐ 0 = 2.3503369 9 9 −1 Our use of Spoke ‘quad’ shows consistency with numerical theories. Remember that the default option is ‘quad(5)’ if the method of integration is not specified explicitly. (Example 3-1-6) The following integral 2๐ ∫ 0 1 2๐ ๐๐ = ≅ 1.2566371 13 + 12 sin ๐ 5 can be compared with #> int .t(0,2*pi) ( 1/(13+12*sin(t)) ); ans = 1.2566371 In the above, the default grid size ๐ = 26 and ๐๐กโ = 4 are used. When default values are all used, it should be kept in mind that the total number of evaluation of a given function is (๐ − 1)(๐๐กโ + 1) = (25)(5) = 125 (relevant 7 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com numerical theories are omitted here). โ โ indefinite integral. When singularities occur at ends of an interval as in ๐ ∞ ∫ ๐(๐ฅ)๐๐ฅ , ∞ ∫ ๐(๐ฅ)๐๐ฅ , −∞ ๐ 1 ∫ 0 ∫ ๐(๐ฅ)๐๐ฅ −∞ 1 √1 − ๐ฅ 2 ๐๐ฅ = sin−1(1) the given interval is splitted into small subintervals. A built-in dot function is .integ(a,b, f(x)) Examples are ∞ 2 ∫ ๐ −๐ฅ sin(๐ฅ 2 ) ๐๐ฅ = 0 #> double f(x) = exp(-x^2)*sin(x^2); #> .integ(0,inf,f); ans = 0.28518528 1/2 √๐ (√2 − 1) 4 // built-in dot function #> sqrt(pi)/4*sqrt(sqrt(2)-1); ans = 0.28518528 1 ∫ 0 1 √1 − ๐ฅ 2 #> double f(x) = 1/sqrt(1-x^2); #> .integ(0,1,f); ans = 1.5707658 #> asin(1); ans = ๐๐ฅ = sin−1(1) // built-in dot function 1.5707963 A user-function equivalent to the above built-in function is //------------------// indefinite integral //------------------double integ(a,b, f(x)) { aa = a; bb = b; flip = 1; 8 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com if(a > b) { aa = b; bb = a; flip = -1; } (sum, h,r) = (0, 0.05, 1.2); if( aa < -0.5*inf && 0.5*inf < bb ) { // -inf<x<inf a = 0; for[300] { sum += int.x(a,a+h) ( f(x) ) + int.x(-a-h,-a) ( f(x) ); a += h; h *= r; } } else if( aa < -0.5*inf ) { // -inf<x<bb a = bb; for[300] { sum += int.x(a-h,a) ( f(x) ); a -= h; h *= r; } } else if( 0.5*inf < bb ) { // aa<x<inf a = aa; for[300] { sum += int.x(a,a+h) ( f(x) ); a += h; h *= r; } } else { // aa<x<bb a = b = 0.5*(aa+bb); h = 0.25*(bb-aa); for[30] { sumo = sum; sum += int.x(a,a+h) (f(x)) + int.x(b-h,b) (f(x)); a += h; b -= h; h *= 0.5; if( sum.-.sumo < 1.e-8) break; } } return sum*flip; } Section 3-2 Integrated Function and Plot โ Integrated Function. The integrated function is here allowed only for a single integration which can be written as ๐ฅ ๐น(๐ฅ) = ∫ ๐(๐ก)๐๐ก ๐ Note that ๐น(๐) = 0 by definition. The syntax for this integrated function is using Spoke ‘togo’ or ‘return’ int .x[n=26,g=1](a,b) ( <<opt>>, f(x), g(x), … ) .togo(F,G, …); int .x[n=26,g=1](a,b) ( <<opt>>, f(x), g(x), … ) .return; 9 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com where F is a matrix the size of which is exactly the same as the grid span. Meanwhile, Spoke ‘return’ does not require a variable, and returns the integration result as a matrix. However, for double and triple integration, use of Spokes ‘togo’ and ‘return’ is invalid. An example of using Spokes ‘togo’ and ‘return’ is as follows. #> int .x[5](0,1) ( x, x*x ) .togo (P,Q); #> int .x[5](0,1) ( x, x*x ) .return; P; Q; The results show the value of integration and 5 × 1 matrix P and Q, as well as 5 × 2 matrix. ans = [ 0.5 0.33333 ] P = [ [ [ [ 0 0.03125 0.125 0.28125 ] ] ] ] [ 0.5 ] [ [ [ 0 ] 0.0052083 ] 0.041667 ] [ [ 0.14062 ] 0.33333 ] [ [ 0 0.03125 0 ] 0.0052083 ] [ [ [ 0.125 0.28125 0.5 0.041667 ] 0.14062 ] 0.33333 ] Q = ans = โ Plot of Integrated Function. The integrated function can be also drawn by using Spoke ‘plot’ or ‘plot+’. Let us consider the integrals 10 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com ๐ฅ ๐ฅ ๐๐ (๐ฅ) = ∫ sin(๐ก ๐ )๐๐ก, ๐ถ๐ (๐ฅ) = ∫ cos(๐ก ๐ )๐๐ก 0 0 where the case of ๐ = 2 corresponds to the well-known Fresnel integrals. An example is #> x = (0,8*pi).span(1001);; #> int[x] ( sin(x^1.5), cos(x^1.5) ) .plot; In the above, the grid span for ๐ฅ–coordinate is generated by the Tuple discussed earlier. The results of integrals are shown in the left of Figure 2. An interesting result is the ๐๐ (๐ฅ) versus ๐ถ๐ (๐ฅ) plot which can be found by the following commands, #> x = (0,8*pi).span(1001);; #> int [x] ( sin(x^1.5), cos(x^1.5) ) .togo(S,C);; #> plot(S,C); or equivalently #> int [x] ( sin(x^1.5), cos(x^1.5) ) .return .plot; and is shown in the right of Figure 2. Figure 2 Plot of integrals Section 3-3 Multiple Integration 11 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com โ Double Integration. Double integration of a function with respect to two independent variables is written as ๐ ๐(๐ฅ) ๐ผ = ∫ [∫ ๐ ๐(๐ฅ, ๐ฆ)๐๐ฆ] ๐๐ฅ ๐(๐ฅ) The syntax for the double integration is int .x[n=11,g=1](a,b) .y[n=11,g=1]( c(x),d(x) ) (< <opt>>, f(x,y) ) and the Spokes are the same as before. (Example 3-3-1) An example for double integration is taken from ref. [1] 1 √๐ฅ−๐ฅ 2 ๐ผ=∫ ∫ 0 (๐ฅ − ๐ฅ 2 − ๐ฆ 2 )๐๐ฆ ๐๐ฅ = −√๐ฅ−๐ฅ 2 ๐ 32 which can be solved by #> int .x[11](0,1) .y[11]( -sqrt(x-x*x),sqrt(x-x*x) ) (x-x*x-y*y ); #> pi/32; ans = 0.0981746 ans = 0.0981748 Numerical result agrees with the exact value up to 5 digits. Since we employed the fourth-degree Gauss quadrature, the equivalent grid size is (5 ⋅ 10) × (5 ⋅ 10) = 50 × 50. However, the degree of agreement deteriorates when the typical trapezoidal rule is applied by Spoke ‘poly’ as follows #> int .x[51](0,1) .y[51]( -sqrt(x-x*x),sqrt(x-x*x) ) (x-x*x-y*y ).poly(1); ans = 0.0981316 Note that, even though an equivalent grid size of 50 ๏ด 50 is of similar order in grid resolution, the trapezoidal rule is inferior to the Gauss quadrature from the standpoint of accuracy. โ 12 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com โ Triple Integration. Triple integration of a function with respect to three independent variables ๐ ๐(๐ฅ) ๐ผ = ∫ [∫ ๐ ๐ฃ(๐ฅ,๐ฆ) ๐(๐ฅ, ๐ฆ, ๐ง)๐๐ง} ๐๐ฆ] ๐๐ฅ {∫ ๐(๐ฅ) ๐ข(๐ฅ,๐ฆ) can be treated by the following syntax int .x[n=6,g=1](a,b) .y[n=6,g=1](c(x),d(x)) .z[n=6,g=1] (u(x,y),v(x,y)) ( <<opt>>, f(x,y,z) ) Also, the same Spokes for ‘int’ are applicable to this case. (Example 3-3-2) An example for triple integration is also taken from ref. [1] 3 ๐ฅ ๐ง+๐ฅ ๐ผ=∫ ∫ ∫ 0 1 [๐ 2๐ฅ (2๐ฆ − ๐ง)]๐๐ง ๐๐ฆ ๐๐ฅ = ๐ง−๐ฅ 1 59๐ 6 + 8 8 which is solved by #> int .x(0,3) .z(1,x) .y(z-x,z+x) ( exp(2*x)*(2*y-z) ); #> 1/8+ 59*exp(6)/8; ans = 2975.4123518 ans = 2975.4123520 where the default size 6 × 6 × 6 is employed. Numerical result agrees with the exact value up to 9 digits. Nevertheless, the trapezoidal rule yields #> int .x[26](0,3) .z[26](1,x) .y[26](z-x,z+x) ( exp(2*x)*(2*y-z) ).poly(1); ans = 3011.1664032 where an equivalent grid size of 25 × 25 × 25 with a trapezoidal rule is equivalent to that of (5 ⋅ 5) × (5 ⋅ 5) × (5 ⋅ 5) = 25 × 25 × 25 with the fourth-degree Gauss quadrature. โ 13 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com Section 3-4 Area Properties For Closed Curves in 2D โ Closed Curves in 2D. In mathematics, it is well known that the area enclosed by a closed curve ๐ถ in the ๐ฅ๐ฆ-plane can be found from 1 (๐ฅ๐๐ฆ − ๐ฆ๐๐ฅ) ๐ถ2 ๐ด ≡ ∫ ๐๐ด = โฎ ๐ด Similarly, several well-known area properties such as the moment can be also determined by integration along the curve. Among those are ๐ด๐ฅ ≡ ∫ ๐ฅ๐๐ด ๐ด๐ฆ ≡ ∫ ๐ฆ๐๐ด ๐ด ๐ด๐ฅ๐ฅ ≡ ∫ ๐ฅ 2 ๐๐ด ๐ด ๐ด ๐ด๐ฅ๐ฆ ≡ ∫ ๐ฅ๐ฆ๐๐ด ๐ด๐ฆ๐ฆ ≡ ∫ ๐ฆ 2 ๐๐ด ๐ด ๐ด where the boundary of ๐ด must be a closed curve ๐ถ. These area properties for closed curves can be obtained by using Umbrella ‘plot’ and its Spokes listed below. .int1da(a) area enclosed by a closed curve .intxda(ax) area properties .intyda(ay) .intxxda(axx) .intxyda(axy) .intyyda(ayy) Here, we use the lower-case ‘a’ instead of the upper-case ‘A’, since ‘intdA’ shall be used for the case of vector area element. Note that the Spoke for evaluating the enclosed area is ‘int1da’ not ‘intda’. This is because that Spoke ‘intda’ shall be used in the surface integral. The meaning of each Spoke listed above is straightforward from its name. One thing to note is that the closed curve should be defined in a counterclockwise sense for correct sign (sometimes, it doesn’t matter whether the sign is plus or minus). (Example 3-4-1) Consider a closed curve enclosed by two curves ๐ฆ = 4 − ๐ฅ 2 and ๐ฆ = 0. We want to evaluate 14 [100] 003 Chapter 3 Integration, ∫ ๐๐ด = ๐ด Tutorial by www.msharpmath.com 32 4096 , ∫ ๐ฆ 2 ๐๐ด = 3 105 ๐ด The following Cemmath commands #> .hold; plot .line(-2,0, 2,0); plot .x(2,-2) ( 4-x*x ) .link .int1da(area) .intyyda(ayy); plot .line(0,-1, 0,4); #> plot; #> area / (32/3) ; #> ayy / ( 4096/105 ); displays a closed curve (shown in Figure 3) and returns two values ‘area’ and ‘ayy’. For convenience, numerical values are compared with the exact values in a ratio form. For example, area / (32/3) = 0.9996001 ayy / ( 4096/105 ) = 0.9990671 Figure 3 Area properties for a closed curve For this special case, numerical values show agreement with exact values to a degree of accuracy up to only 3 digits (even with default grid size of 51). It is apparent that Gauss quadrature would yield numerical values of much higher accuracy, of course. However under certain conditions, the integration becomes extremely complicated due to difficulty in expressing the closed curve. For 15 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com example, the closed curve may be rotated around the ๐ง-axis. โ (Example 3-4-2) To illustrate this point, the curve discussed above is rotated around the z -axis and the same procedure is repeated. #> .hold; plot .line(-2,0, 2,0); plot .x(2,-2) ( 4-x*x ) .link .zrot(pi/6) .int1da(area) .intyyda(ayy); plot .line(0,-1, 0,4); plot .line(0,0, 0,4) .zrot(pi/6); #> plot; #> area / (32/3); ayy / ( 4096/105 ); The above Cemmath commands yield area / (32/3) = 0.9996002 ayy / ( 4096/105 ) = 0.8039512 while the area is unchanged as expected. This situation is drawn in Figure 4. โ Figure 4 Area property for a rotated closed curve Section 3-5 Surface Integral โ Surface Integral (scalar case). When a surface is represented by means 16 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com of parameterization, the scalar area element may be written as ๐๐ด = |d๐ฎ × d๐ฏ| where d๐ฎ and d๐ฏ are infinitesimal tangent vectors (details are omitted here). Then, the surface integral is defined ∫ ๐๐ด ๐(๐ฅ, ๐ฆ, ๐ง) ๐ด where ๐(๐ฅ, ๐ฆ, ๐ง) is a scalar function with respect to three spatial coordinates. For surface integrals, we use very simple summation ∫ ๐๐ด ๐(๐ฅ, ๐ฆ, ๐ง) ≅ ∑ ๐๐ด๐ ๐๐ ๐ด i the accuracy of which cannot compete with well-devised formula for integration though. To evaluate the surface integral, we use Umbrella ‘plot’ to create a surface and then employ Spoke ‘intda’ .area (a) .intda (f(x,y,z), …) (fscalar,…) Without any loss of generality, those independent variables in the above expression are fixed to be ‘x,y,z’ since use of other variables is not of major interest. (Example 3-5-1) Let us consider again the surface enclosed by two curves ๐ฆ = 4 − ๐ฅ 2 and ๐ฆ = 0. For the surface shown in Figure 5, 17 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com Figure 5 A filled surface for surface integral we want to evaluate the following surface integrals ∫ ๐๐ด , ∫ ๐ฅ๐๐ด , ∫ ๐ฆ๐๐ด , ∫ ๐ฅ 2 ๐๐ด, ∫ ๐ฅ๐ฆ๐๐ด, ∫ ๐ฆ 2 ๐๐ด, ∫ ๐ฅ 2 (1 + 0.1๐ฅ๐ฆ)๐๐ด ๐ด ๐ด ๐ด ๐ด ๐ด ๐ด ๐ด Our attempt to treat this problem begins with creating a filled surface and then uses Spoke ‘intda’ #> plot .x[51](-2,2) ( 0, 4-x*x ) .fill[51] .area(a) .intda ( x, y, x*x, x*y, y*y, x*x*(1+0.1*(x*y)) ) ( ax, ay, axx, axy, ayy, rhoaxx ); #> a; ax; ay; axx; axy; ayy; rhoaxx; which results in a = 10.6624000 ax = ay = axx = axy = ayy = 0.0000000 17.0496039 8.5333320 0.0000000 38.9510297 rhoaxx = 8.5333320 In particular, a comment is placed on the value of ‘ayy’. The value obtained in terms of close-curve integration was 38.9731331, which is somehow different from the value obtained through surface integral. The reason for the occurrence 18 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com of this slight difference lies in the procedure of integration, as was mentioned earlier. โ (Example 3-5-2) The next example considers a surface enclosed by an annulus shown in Figure 6. The annular region is created first and then the desired surface integral is performed by #> plot .x[31](1,sqrt(5)) (0) .zrev[361](0,2*pi) .area(a) .intda( x*x+y )(b); #> a/(4*pi); b/(6*pi); Figure 6 An annular region for surface integral Even though we adopt 31 × 361 mesh for integration, comparison with exact values reveals that a/(4*pi) = 0.9999493 b/(6*pi) = 0.9997318 The degrees of agreement with exact values 4๐, 6๐ are just order of 3 digits. Of course, the degree of accuracy can be strikingly increased by employing Gauss quadrature, as long as mathematical handling can be afforded. โ โ Surface Integral (vector case). A surface element can be also expressed as a vector element, and thus the surface integral can be presented in several different ways, e.g. 19 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com ∫ ๐๐ดโ ⋅ ๐นโ (๐ฅ, ๐ฆ, ๐ง) ๐ด ∫ ๐๐ดโ × ๐นโ (๐ฅ, ๐ฆ, ๐ง) ๐ด ∫ ๐๐ดโ ๐(๐ฅ, ๐ฆ, ๐ง) ๐ด where the surface element ๐๐ดโ = ๐๐ฎ × ๐๐ฏ is normally defined from the righthand rule. In addition, since ๐๐ดโ = ๐ข๐๐ฆ๐๐ง + ๐ฃ๐๐ง๐๐ฅ + ๐ค๐๐ฅ๐๐ฆ, it can be noted that ∫ ๐๐ดโ ⋅ ๐นโ = ∫ ๐(๐ฅ, ๐ฆ, ๐ง)๐๐ฆ๐๐ง + ๐(๐ฅ, ๐ฆ, ๐ง)๐๐ง๐๐ฅ + ๐ (๐ฅ, ๐ฆ, ๐ง)๐๐ฅ๐๐ฆ ๐ด ๐ด where ๐นโ = (๐, ๐, ๐ ). This can be used to confirm the following equality 1 ๐ = ∫ ๐ฅ๐๐ฆ๐๐ง + ๐ฆ๐๐ง๐๐ฅ + ๐ง๐๐ฅ๐๐ฆ 3 ๐ด for a closed surface ๐ด, where ๐ designates the volume enclosed by the closed surface. Integration with infinitesimal vector elements can be performed by applying the Spokes listed below. .intdA * (Fx(x,y,z),Fy(x,y,z),Fz(x,y,z)) (fscalar) .intdA ^ (Fx(x,y,z),Fy(x,y,z),Fz(x,y,z)) (Fmatrix) .intdA (f(x,y,z)) (Fmatrix) Note that the spatial coordinates are always ‘x,y,z’ regardless of the Hub variables in generating surface. (Example 3-5-3) Consider a sphere of radius 1 and evaluate ∫ ๐๐ดโ ⋅ ๐นโ = ∫ ๐๐ดโ ⋅ (๐ฅ, 2๐ฆ, 3๐ง) = ∫ ๐๐ ∇ ⋅ (๐ฅ, 2๐ฆ, 3๐ง) = 6๐ ๐ด ๐ด ๐ 20 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com where the Gauss’s divergence theorem is applied to convert from surface to volume integral. This is solved by #> plot .sphere[101,101](1) .intdA*(x,2*y,3*z)(fs); #> fs/(8*pi); which results in ans = 0.99911617 Comparison with the exact value shows about 0.1% error. โ 1 (Example 3-5-4) For a sphere of radius 1, ๐ = ∫๐ด ๐ฅ๐๐ฆ๐๐ง + ๐ฆ๐๐ง๐๐ฅ +๐ง๐๐ฅ๐๐ฆ 3 can be easily confirmed by #> plot .sphere[101,101](1) .intdA*(x,y,z)(fs); #> fs/(4*pi) which results in ans = 0.9990956 โ (Example 3-5-5) When ๐น = (√๐ฅ 2 + ๐ฆ 2 , ๐ฅ 2 + ๐ฆ 2 , 0) , let us evaluate ∫๐ด ๐๐ดโ × ๐นโ for a disk of radius 1 in the xy –plane shown in Figure 7. The exact value is known to be (−๐/2,2๐/3, 0) Figure 7 A disk for surface integral 21 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com The following Cemmath commands #> plot .disk[101,101](1) .intdA^(sqrt(x*x+y*y),x*x+y*y,0)(F); #> F; [-pi/2, 2*pi/3, 0]; yield F = [ -1.5681 2.0919 0 ] ans = [ -1.5708 2.0944 0 ] โ (Example 3-5-6) Evaluate ∫๐ด ๐ง๐๐ดโ for a sphere of radius 1 located at the origin. The exact value is (0,0,4๐/3). We solve this problem by #> plot .sphere[101,101](1) .intdA (z)(F); #> F; [0,0, 4*pi/3]; The results are F = [ 2.61564e-018 ans = [ 3.32503e-018 0 4.18535 ] 0 4.18879 ] โ โ Surface Integral for Closed Surface. A finite volume in 3D can be defined by a certain closed surface the inside of which is defined as the inner region of the volume. For closed surfaces, one of the most interesting surface integrals would be the Archimedes principle combined with the Gauss’s divergence theorem, i.e. ∫ ๐๐ดโ (๐๐๐ง) = ∫ ๐๐∇(๐๐๐ง) = ๐๐๐โโ ∫ ๐๐ = ๐โโ (๐๐๐) ๐ด ๐ ๐ From this principle, it is possible to evaluate the volume enclosed by a closed surface. However, the use of Spoke ‘volume’ is something like DIY (do it yourself). In other words, Cemmath never guarantee whether the geometry of interest is a closed surface or not. (Example 3-5-7) Consider a torus generated by revolving a circle defined as 22 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com 1 2 1 2 (๐ฅ − ) + ๐ง 2 = ( ) 2 2 around the ๐ง–axis. Its surface area and volume are known to be ๐ด = ๐ 2 , ๐ = 1 2 ๐ . 4 Let us solve this problem by #> plot .torus[51,51](0.5,0.5) .area(a) .volume(v); #> a/(pi*pi); v/(0.25*pi*pi); ans = 0.9976997 ans = 0.9947454 where the torus is shown in Figure 8. As a matter of fact, the Spoke ‘volume’ has been originated from Spoke ‘.intdA (0,0,z)’ the reason of which is already discussed above. โ Figure 8 A torus for surface integral (Example 3-5-8) A frustum with square base can be drawn by Spoke ‘frustum’, e.g. #> plot .frustum[51,5,51](3,2,2) .area(a) .volume(v); #> a/56; v/(76/3); the result of which is shown in Figure 9. 23 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com Figure 9 A frustum with square base We can easily calculate the surface area and the volume of a frustum with square base ๐ด = 2[๐2 + ๐ 2 + (๐ + ๐)√2โ2 + (๐ − ๐)2 , ๐= โ 3 (๐2 + ๐๐ + ๐2 ) where the exact value is ๐ด = 56, ๐ = 76/3. The numerical results show that a/56 = 1.0000000 v/(76/3) = 1.0000105 Agreement is fairly good because all the surfaces are flat. โ Section 3-6 Line Integral โ Line Integral (vector case). The line integral along a curve ๐ถ is defined as ∫ ๐๐ฟโโ ⋅ ๐นโ = ∫ ๐(๐ฅ, ๐ฆ, ๐ง)๐๐ฅ + ๐(๐ฅ, ๐ฆ, ๐ง)๐๐ฆ + ๐ (๐ฅ, ๐ฆ, ๐ง)๐๐ง ๐ถ ๐ถ ∫ ๐๐ฟโโ × ๐นโ ๐ถ ∫ ๐๐ฟโโ ๐(๐ฅ, ๐ฆ, ๐ง) ๐ถ where ๐นโ = ๐ข๐(๐ฅ, ๐ฆ, ๐ง) + ๐ฃ๐(๐ฅ, ๐ฆ, ๐ง) + ๐ค๐ (๐ฅ, ๐ฆ, ๐ง) and ๐๐ฟโโ = ๐ข๐๐ฅ + ๐ฃ๐๐ฆ + ๐ค๐๐ง. 24 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com The line integrals with vector line element are treated by Spoke ‘intdL’. A limitation for the line integral is that the curve ๐ถ must be defined as 3D curve. The corresponding syntax for Spoke ‘intdL’ is .intdL * (Fx(x,y,z),Fy(x,y,z),Fz(x,y,z)) (fscalar) .intdL ^ (Fx(x,y,z),Fy(x,y,z),Fz(x,y,z)) (Fmatrix) .intdL (f(x,y,z),…) (Fmatrix,…) where the vector field is F=(Fx,Fy,Fz), and the results are saved on the scalar โโ (1) = ๐ฟโโ๐ − ๐ฟโโ๐ . fscalar or the matrix Fmatrix. Note that a special case is ∫ ๐๐ฟ ๐ถ (Example 3-6-1) Consider a curve ๐ถ defined as ๐ฅ2 32 + ๐ฆ2 22 = 1. It can be then shown that −๐ฆ๐๐ฅ + ๐ฅ๐๐ฆ = 2๐ ๐ฅ2 + ๐ฆ2 ๐ถ ∫ Even though the curve ๐ถ lies in the ๐ฅ๐ฆ–plane, we have to solve this problem in 3D space as shown in Figure 10. The corresponding Cemmath commands and the results are #> plot .@t[101](0,2*pi) ( 3*cos(t),2*sin(t),0 ) .intdL*( -y/(x*x+y*y), x/(x*x+y*y) )(fval); #> fval/(2*pi) ans = 1.0003291 โ Figure 10 An ellipse in the xy -plane for line integral 25 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com (๐ฅ, ๐ฆ, ๐ง) = (Example 3-6-2) Consider a helix ๐ถ defined by (cos ๐, sin ๐, 0.5๐) where ๐ ≤ ๐ ≤ 3๐. Let us confirm the following line integral ∫ ๐ฆ๐๐ฅ + ๐ฅ๐๐ฆ + ๐ง๐๐ง = ๐ 2 ๐ถ from the following commands #> plot .@t(pi,3*pi) ( cos(t),sin(t),0.5*t ) .intdL*(y,x,z) (fval); #> fval/(pi*pi) ans = 0.9999998 The corresponding curve is shown in Figure 11. โ Figure 11 A helix for line integral (Example 3-6-3) For a helix ๐ถ shown in Figure 11, let us confirm ∫๐ถ ๐๐ฟโโ × (๐ฆ, ๐ฅ, ๐ง) = ๐๐ฃ and ∫๐ถ ๐๐ฟโโ (1) = ๐๐ค by the following commands 26 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com #> plot .@t(pi,3*pi) ( cos(t),sin(t),0.5*t ) .intdL^(y,x,z) (Fcrs); #> plot .@t(pi,3*pi) ( cos(t),sin(t),0.5*t ) .intdL (1) (Fvec); #> Fcrs; Fvec; The results are as follows. Fcrs = Fvec = [ [ 7.7212e-008 0 3.1416 2.498e-016 2.0817e-017 ] 3.1416 ] Comparison with the exact solutions shows good agreements. โ โ Line Integral (scalar case). Another type of line integral along a curve ๐ถ with scalar line element can be defined as ∫ ๐๐ฟ ๐(๐ฅ, ๐ฆ, ๐ง) ≡ ∫ |๐๐ฟโโ | ๐(๐ฅ, ๐ฆ, ๐ง) ๐ถ ๐ถ where ๐(๐ฅ, ๐ฆ, ๐ง) is a scalar function. Such a line integral is treated by Spoke ‘intdl’. The syntax for the Spoke ‘intdl’ is .intdl( f(x,y,z,L), … )(fscalar, …) where L denotes the arc length measured from the starting point. For example, the arc length can be found by ‘.intdl(1)(arclen)’. Note that the lower-cased ‘dl’ denotes a scalar line element, whereas the upper-cased ‘dL’ denotes a vector line element. (Example 3-6-4) We can find the arc length of the helix considered in (example 3-6-2) and a line integral 1 5๐ 2 √5 ∫ ๐๐ฟ (๐ฟ) = [๐ฟ(3๐)]2 = , since ๐ฟ(๐ก) = (๐ก − ๐) 2 2 2 ๐ถ by the following commands #> plot .@t[101](pi,3*pi) ( cos(t),sin(t),0.5*t ) .intdl(1, L) (arclen, f); #> arclen/(sqrt(5)*pi); 27 [100] 003 Chapter 3 Integration, Tutorial by www.msharpmath.com #> f/(2.5*pi*pi); where the results are ans = ans = 0.9998684 1.0097342 Note that a fairly large number of segments are required to get 3 digits accuracy. โ Section 3-7 Summary of Syntax โ Single and Multiple Integration. The syntax of single and multiple integration is int .x[n=26,g=1](a,b) ( <<opt>>, f(x),g(x),h(x), … ) int .x[n=11,g=1](a,b) .y[n=11,g=1](c(x),d(x)) ( <<opt>>, f(x,y) ) int .x[n=6,g=1](a,b) .y[n=6,g=1](c(x),d(x)) .z[n=6,g=1] (u(x,y),v(x,y)) ( <<opt>>, f(x,y,z) ) โ Spokes. The Spokes for Umbrella ‘int’ are .poly(nth) Newton-Cotes integration with polynomial of nth<=8 .quad(num) .togo(F) .return .plot Gauss-Legendre quadrature with number of points num<=6 takeout an integrated function as a matrix F return an integrated function as a matrix plot an integrated function for one-variable case โ Spokes for ‘plot’. The line and surface integrals associated with geometries are found by using Umbrella ‘plot’ and its relevant Spokes as .int1da(a) .intxda(ax) area enclosed by a closed curve area properties .intyda(ay) .intxxda(axx) .intxyda(axy) .intyyda(ayy) 28 [100] 003 .area (a) .volume(v) Chapter 3 Integration, Tutorial by www.msharpmath.com // for closed surface only .intda ( f(x,y,z), … ) (fscalar, …) .intdl ( f(x,y,z,L), … ) (fscalar, …) where L is an arc length measured from the starting point .intdA * (Fx(x,y,z),Fy(x,y,z),Fz(x,y,z)) (fscalar) .intdA ^ (Fx(x,y,z),Fy(x,y,z),Fz(x,y,z)) (Fmatrix) .intdA (f(x,y,z)) (Fmatrix) .intdL * (Fx(x,y,z),Fy(x,y,z),Fz(x,y,z)) (fscalar) .intdL ^ (Fx(x,y,z),Fy(x,y,z),Fz(x,y,z)) (Fmatrix) .intdL (f(x,y,z),…) (Fmatrix,…) Section 3-8 References [1] R.E. Larson, R.P. Hostetler, B.H. Edwards, Calculus with Analytic Geometry, 6ed., Houghton Mifflin Co., (1998) [2] H.B. Wilson, L.H. Turcotte, Advanced Mathematics and Mechanics Applications Using MATLAB, 2ed., CRC Press (1997). 29