[100] 003 Tutorial int - msharpmath, The Simple is the Best

advertisement
[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
Download