Numerical Method
Numerical Method
Trapezoidal Rule and Simpson’s Rule are the numerical methods to approximate the definite integral
especially when the integral is difficult to be solved by using ordinary integral method.
1 Trapezoidal Rule
For the definition of the Trapezoidal Rule, let
Z b
h
f (x)dx ≈ Tn = [f (x0 ) + 2f (x1 ) + . . . + 2f (xn−1 ) + f (xn )]
a 2
b−a
where h = and n is the subintervals of equal length [a, b] .
n
Z 2
1
Example 1. Use the Trapezoidal Rule with n = 5 to approximate the integral dx.
1 x
Solution
2−1
Let n = 5, a = 1 and b = 2 and h = = 0.2. From the definition,
5
Z 2
1 0.2
dx ≈ T5 = [f (1) + 2f (1.2) + 2f (1.4) + 2f (1.6) + 2f (1.8) + f (2)] ;
1 x 2
1 1 1 1 1 1
= 0.1 1 +2 +2 +2 +2 +1 ;
1 1.2 1.4 1.6 1.8 2
2 2 2 2
= 0.1 1 + + + + + 0.5 ;
1.2 1.4 1.6 1.8
≈ 0.6956.
The use of Python Programming is useful as assistance to compute the integral numerically. The
following is the syntax for the problem in example 1.
2 Simpson’s Rule
For the definition of Simpson’s Rule 1 , let
Z b
f (x)dx ≈ Sn
a
h
= [f (x0 ) + 4f (x1 ) + 2f (x2 ) + 4f (x3 ) + . . . + 2f (xn−2 ) + 4f (xn−1 ) + f (xn )]
3
b−a
where h = and n is the even subintervals.
n
1
Thomas Simpson is an English mathematician in the 18th century.
Page 1 of 8
Partial Derivatives MAT538
Z 2
1
Example 2. Use the Simpson’s Rule with n = 10 to approximate the integral dx.
1 x
Solution
2−1
Let n = 10, a = 1 and b = 2, we have h = = 0.1, from the definition,
10
Z 2
1
dx ≈ Sn
1 x
0.1
= [f (1) + 4f (1.1) + 2f (1.2) + 4f (1.3) + . . . + 2f (1.8) + 4f (1.9) + f (2)] ;
3
0.1 4 2 4 2 4 2 4 2 4
= 1+ + + + + + + + + + 0.5 ;
3 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
≈ 0.6932.
algebraically
Z 2
1 2
dx = ln(x) = ln(2) − ln(1) ≈ 0.6932.
1 x 1
Z 5
cos(2x)
Example 3. Evaluate dx with 8 subintervals by using the Trapezoidal Rule and Simpson’s
1 x
Rule. Give the answer correct to four decimal places. (8 marks : Jan2018)
Solution
5−1 cos(2x)
h= = 0.5 and f (x) = .
8 x
cos(2x)
x cos(2x) f (x) = Coef (Coef)∗f (x) Coef (Coef)∗f (x)
x
1.0 -0.4161 -0.4161 1 -0.4161 1 -0.4161
1.5 -0.9900 -0.6600 2 -1.3200 4 -2.6400
2.0 -0.6536 -0.3268 2 -0.6536 2 -0.6536
2.5 0.2837 0.1135 2 0.2270 4 0.4540
3.0 0.9602 0.3201 2 0.6402 2 0.6402
3.5 0.7540 0.2154 2 0.4308 4 0.8616
4.0 -0.1455 -0.0364 2 -0.0728 2 -0.0728
4.5 -0.9111 -0.2025 2 -0.4050 4 -0.8100
5.0 -0.8391 -0.1678 1 -0.1678 1 -0.1678
-1.7373 -2.8045
Page 2 of 8
Partial Derivatives MAT538
Practice
Use the Trapezoidal Rule and Simpson’s Rule to approximate the given integral with the specific value
of n. (Round down the answer to four decimal places)
Z 2 Z 4
1. xdx, n = 4. 2. (x + 1)dx, n = 6.
1 0
Z 2p Z 4
3. x3 − 1dx, n = 10. 4. ln(x)dx, n = 6.
1 1
4 4
√
Z Z
3
5. x sin(x)dx, n = 8. 6. y cos(y)dy, n = 8.
0 0
1 4
x2
Z Z
7. dx, n = 10. 8. ln(1 + ex )dx, n = 8.
0 1 + x4 0
k1 = f (1, 1) = 2[1][1] = 2;
1 1
k2 = f 1 + [0.1], 1 + [0.1][2] = f (1.05, ) = 2[1.05][1.1] = 2.3100;
2 2
1 1
k3 = f 1 + [0.1], 1 + [0.1][2.31] = f (1.05, 1.1155) = 2[1.05][1.1155] = 2.3426;
2 2
k4 = f (1 + 0.1, 1 + [0.1][2.3426]) = f (1.1, 1.2343) = 2[1.1][1.2343] = 2.7155.
and therefore
0.1
y(1.1) = yn + (k1 + 2k2 + 2k3 + k4 ) ;
6
0.1
= 1+ [2 + 2[2.31] + 2[2.3426] + 2.7155] ;
6
0.1
= 1+ [14.0207];
6
≈ 1.2337.
Page 3 of 8
Partial Derivatives MAT538
Solution
Let f (x, y) = sin(x + y), x10 = 1.0, y10 = 2.3375 and the stepsize h = 0.1,
and therefore
0.1
y11 = y10 + (k1 + 2k2 + 2k3 + k4 ) ;
6
0.1
= 2.3375 + [−0.1947 + 2[−0.2340] + 2[−0.2320] + (−0.2693)] ;
6
0.1
= 2.3375 + [−1.3960];
6
≈ 2.3142.
Practice
dy sin(x) − xy 2
JUN16 If = and y(0) = 5, use the Fourth-Order Runge-Kutta method with a step size
dx 3
h = 0.3 to approximate y when x = 0.3. Give the answer correct to 5 decimal places. (ans:
4.66485)
DEC16 In a chemical reaction, the amount y grams of a substance after t hours is modeled
dy
= e−2t − 2y, y(0) = 0.
dt
Use the Fourth-Order Runge-Kutta method with the step size h = 2 to estimate y(2). (ans:
-3.4802)
JUL17 Consider the Differential Equation y 0 = sin(x + y) with initial condition y(0) = 2 and step size
h = 0.1. If x10 = 1 and y10 = 2.3375, estimate the value of y11 = y(1.1) by using the Fourth-
Order Runge-Kutta method. Give the answer correct to four decimal places. (ans: 2.3142)
Page 4 of 8
Partial Derivatives MAT538
4 Fourier Analysis
In this section, we will study the Fourier analysis which is widely used in the study of wave. We will
limit our discussion to Fourier series and the related topics.
(i) A fucntion y = f (x) is even when for each x in the domain of f such that f (−x) = f (x).
(ii) A fucntion y = f (x) is odd when for each x in the domain of f such that f (−x) = −f (x).
Example 6. Determine
√ whether the given function is even, odd or neither.
(a) f (x) = 2 + x2 (b) g(x) = x1/3 − sin(x) (c) k(x) = e2x
Solution p p
(a) f (−x) = 2 + (−x)2 = 2 + x2 = f (x).
f (x) is an even function.
h i
(b) g(−x) = (−x)1/3 − sin(−x) = −x1/3 + sin(x) = − x1/3 − sin(x) = −g(x).
Solution
2(−x) 2x
f (−x) = 2 − =2+
π π
f (x) is neither an even nor an odd function.
which
Z T
1
a0 = f (x)dx; (2)
T −T
Z T
1 nπx
an = f (x) cos dx; n = 0, 1, 2, ... (3)
T −T T
Z T
1 nπx
bn = f (x) sin dx; n = 1, 2, ... (4)
T −T T
Page 5 of 8
Partial Derivatives MAT538
Solution
1 π x2 π
Z
π
a0 = xdx = = .
π 0 2π 0 2
Z π
1
an = x cos(nx)dx;
π 0
Z π
1 x sin(nx) π sin(nx)
= − dx ;
π n 0 0 n
cos(nπ) 1
= 2
− 2 ;
n π n π
1
= [cos(nπ) − 1] , n = 1, 2, ...
n2Zπ
1 π
bn = x sin(nx)dx;
π 0
Z π
1 x cos(nx) π cos(nx)
= − + dx ;
π n 0 0 n
cos(nπ)
= − , n = 1, 2, ...
n
∞
π X 1 cos(nπ)
Therefore f (x) = + [cos(nπ) − 1] cos(nx) − sin(nx) ;
4 n2 π n
n=1
π 2 1
= + − cos(x) + sin(x) + − sin(2x) + ...
4 π 2
π 2 1
= − cos(x) + sin(x) − sin(2x) + ...
4 π 2
Example 9. Let f (x) be a periodic function with period 2π. Find the Fourier series expansion for
f (x) until the first three non-zero terms. (13 marks : Jul2017)
0, −π < x < 0,
f (x) =
π − x, 0 < x < π.
Solution
Z π Z 0 Z π
1 1
a0 = f (x)dx = f (x)dx +f (x)dx ;
π −π π −π 0
Z 0 Z π
1
= (0)dx + (π − x)dx ;
π −π 0
1 π
Z
= 0+ (π − x)dx;
π 0
π
x2
1
= πx − ;
π 2 0
π
= .
2
Page 6 of 8
Partial Derivatives MAT538
Z π Z 0 Z π
1 1
an = f (x) cos(nx)dx = f (x) cos(nx)dx + f (x) cos(nx)dx ;
π −π π −π 0
Z 0 Z π
1
= (0) cos(nx)dx + (π − x) cos(nx)dx ;
π −π 0
1 π
Z
= 0+ (π − x) cos(nx)dx;
π 0
1 (π − x) sin(nx) cos(nx) π
= − ; n = 1, 2, ...
π n n2 0
1 cos(πn) 1
= − + 2 ;
π n2 n
1 − cos(nπ)
= 2
.
Z π Zn 0π Z π
1 1
bn = f (x) sin(nx)dx = f (x) sin(nx)dx + f (x) sin(nx)dx ;
π −π π −π 0
Z 0 Z π
1
= (0) sin(nx)dx + (π − x) sin(nx)dx ;
π −π 0
1 π
Z
= 0+ (π − x) sin(nx)dx;
π 0
(π − x) cos(nx) sin(nx) π
1
= − − ; n = 1, 2, ...
π n n2 0
1 sin(πn) π
= − + ;
π n2 n
1 sin(πn)
= − .
n πn2
∞
a0 X
Therefore f (x) = + [an cos(nx) + bn sin(nx)] ;
2
n=1
∞
π X 1 − cos(nπ) 1 sin(πn)
= + cos(nx) + − sin(nx);
4 n2 π n πn2
n=1
π 2 1
= + cos(x) + 0 + · · · + sin(x) + sin(2x) + · · · ;
4 π 2
π 2
= + cos(x) + sin(x) + · · ·
4 π
A Fourier series is just not only represents the function on the interval (−T, T ) but also gives the
periodic extension of f outside of this interval.
Example 10. Sketch the periodic extension of f (x) = x in example 8 for −π < x < 2π.
Solution
y
x
−π π 2π
Page 7 of 8
Partial Derivatives MAT538
In-Class Tutorial
3. Find the Fourier series for the function f (x) = x + π, −π < x < π.
∞
X − cos(nπ)
ans:f (x) = π + 2 sin(nx)
n
n=1
2x
JAN 2018 Consider the function f (x) = 2 − for −π < x < π and f (x + 2π) = f (x).
π
(i) Sketch the graph of f (x) if −3π < x < 3π.
(ii) State whether the function f (x) is even, odd or neither even nor odd.
(iii) Find the Fourier series of f (x) if a0 = 4.
∞
X 4
ans:(ii) neither odd nor even, (iii)f (x) = 2 + cos(nπ) sin(nx).
nπ
n=1
t2
DEC 2016 A function f (t) is defined by f (t) = for 0 < t < 2π and f (t + 2π) = f (t).
2
(i) Sketch the graph of f (t) if −4π < t < 4π.
8π 2
(ii) Given a0 = , obtain the Fourier series for f (t).
3
∞
4π 2 X 2
2π
ans:f (t) = + cos(nt) − sin(nt) .
3 n2 n
n=1
Page 8 of 8