Numerical Analysis: Methods and Applications
Numerical Analysis: Methods and Applications
Chung-Ang University
Seoul, Korea
2
Contents
3 Root Finding 13
3.1 Bisection Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 Newton-Rapson Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4 Interpolation 27
4.1 Polynomial Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2 Spline Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.3 Least Squre-Error Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.4 Richardson Extrapolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5 Numerical Integrations 53
5.1 Definite Integral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.2 Infinite-Region Integral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.3 Improper Integral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7 Random Number 83
3
4
Chapter 1
Many problems can not be handled with only paper and pencil !!!
• Multi-Dimensional Problems
• Complex Initial or Boundary-Value Problems
• Simple Iteration calculation
Now a days, Nowadays, the computing power of PCs has increased dramatically:
5
6
Chapter 2
A number of problems in numerical analysis can be reduced to, or approximated by, a sys-
tem of linear equations. Our goal is the numerical solution of systems of linear eqs:
For a unique solution x, the matrix A should be invertible, which is verified by non-zero
determinant of A:
Det(A) , 0 (2.4)
x = A−1 b
7
As an example, let us consider the following linear equations:
3 x1 + 3 x2 − 3 x3 = 6
−3 x1 − 4 x2 − 4 x3 = −7 (2.5)
2 x1 + 1 x2 + 1 x3 = 7
3 3 −3 6
A = −3 −4 4 b = −7 (2.6)
2 1 1 7
Before entering into the problem, let us study some important properties of linear sysme
of equations. It is found that the solution of the system does not affected by the following
operations:
−3 x1 − 4 x2 − 4 x3 = −7
3 x1 + 3 x2 − 3 x3 = 6
2 x1 + 1 x2 + 1 x3 = 7
x1 + x2 − x3 = 2
−3 x1 − 4 x2 − 4 x3 = −7
2 x1 + 1 x2 + 1 x3 = 7
−3 x1 − 4 x2 − 4 x3 = −7
− x2 − 7 x3 = −1
2 x1 + 1 x2 + 1 x3 = 7
8
1) Make a matrix B by adding the identity matrix I whose size is same as A:
h i
B= A I
3 3 −3 1 0 0
= −3 −4 4 0 1 0
2 1 1 0 0 1
9
7) Operation on the 1st row and 2nd row to rebuild B as
3 A = [1 2 3; 4 5 6; 7 8 1];
4 Ainv = func_Minv(A);
5
6 C = Ainv*A;
7
8 Ainv
9 C
10
11 %%
12 function Ainv = func_Minv(A)
13 [M,N] = size(A);
14 B = [A, eye(size(A))];
15
16 for k = 1:M
17 B(k,k:2*N) = B(k,k:2*N)/B(k,k);
18 B([1:k-1,k+1:M],k:2*N) = ...
19 B([1:k-1,k+1:M],k:2*N) - B([1:k-1,k+1:M],k)*B(k,k:2*N);
20 end
21 Ainv = B(:,N+1:2*N);
22 end
10
1 Ainv =
2 1.3333 1.0000 0
3 -1.8333 -1.5000 0.5000
4 -0.8333 -0.5000 0.5000
5
6 C =
7 1 0 0
8 0 1 0
9 0 0 1
This can be verified by the calculation with Malab built-in function “inv”. Then you will get
the same inverse matrix. Now the given linear equation can be solved as
1
−1
x = A b = 3
2
This can be verified by the Matlab programming. Even we have already verified the our
inverse programm, we will use Malab built-in function “inv” for calculating the inverse ma-
trix from now. The following is the Matlab script for solving our linear system of equation:
3 A = [3 3 -3; -3 -4 4; 2 1 1];
4 b = [ 6; -7; 7];
5
6 x = inv(A)*b;
7 x
1 x =
2
3 1.0000
4 3.0000
5 2.0000
11
2.2 Backward-Substitution Process
We may have a question that we always have to use inverse matrix approach to solve the lin-
ear system of equations. Starting with this question, the method called Backward-Substitution
Process has been suggested. This method follows the same approach of finding inverse ma-
trix (but we will not find its inverse matrix).
3) The matrix is now in upper triangular form: there are no nonzero entries below the
diagonal. From the resulting set of equations in reverse order, the same x can be found:
2 x3 = 4
→ x3 = 2 → x2 = 3 → x1 = 1
−x2 + x3 = −1
3x1 + 3x2 − 3x3 = 6
1
x = 3
2
12
Chapter 3
Root Finding
We are now concerned with the problem of finding a zero for the function f (x).
• Bisection Method
• Newton-Rapson Method
• Secant Method, · · ·
a+b
1) p =
2
a = p, f (a)f (p) < 0
2)
b = p, otherwise
13
( )
( 1)
( 2)
( )
2 2 2
1) PN < ϵ,
2) PN − PN −1 < ϵ
P − PN −1
3) N <ϵ
PN
∆p
ϵ=
max(x) − min(x)
find the approximate zero of f (x) in x ∈ [−1, 2] with the bisection method. The stop condition
is ϵ <= 0.0005.
3 a = -1;
4 b = 2;
5 cond = 0.0005;
14
6
7 xx = a:0.01:b;
8 fxx = func_fx(xx);
9 plot(xx,fxx)
10
11 intv = b-a;
12 ii = 0;
13 cond1 = 1;
14 while cond1 > cond
15 ii = ii+1;
16 xcond(ii) = cond1;
17 pii = (a+b)/2;
18 fpii = func_fx(pii);
19
20 if func_fx(a)*fpii < 0
21 b = pii;
22 else
23 a = pii;
24 end
25
26 p(ii) = pii;
27 fp(ii) = fpii;
28
29 if ii>=2
30 cond1 = abs(pii-p(ii-1))/intv;
31 end
32 idx(ii) = ii;
33 end
34
38 %% Function
39 function fx = func_fx(x)
40
41 fx = 3*x.^3+5*x-5;
42
43 end
15
Figure 3.2: Example of root finding with the bisection method.
3 ans =
4
The bisection method, though conceptually clear, has significant drawbacks of slow convergence.
However, the method has the important property that it always converges to a solution, and
16
1)
( 2)
for that reason it is often used as an initial value for the more efficient methods (hybrid
methods). The following property is satisfied:
b−a
|Pn − p| < , n≥1
2n
17
– Let us consider a line with a slope f ′ (x1 ) passing through (x1 , f (x1 )).
Then x2 , the crossing point of this line and y = 0, is found to be much closer to
the solution than x1 .
f (x1 )
x2 = x1 −
f ′ (x1 )
Ex Find the zero of the function given in the previous example with the initial value xini =
−0.9.
3 a = -1;
4 b = 2;
5 cond = 0.001;
6
7 %%
8 xx = a:0.01:b;
9 fxx = func_fx(xx);
18
10 plot(xx,fxx), hold on
11 plot([-1 2],[0 0])
12 p(1) = input('Initial value = ');
13 f(1) = func_fx(p(1));
14 plot(p(1),0,'o')
15 pause
16
17 ii = 1;
18 idx(1) = 1;
19 intv = b-a;
20 dx = (b-a)/1000;
21 cond1 = 1;
22 xcond(1) = cond1;
23
29 cond1 = abs(p(ii)-p(ii-1))/intv;
30 xcond(ii) = cond1;
31 idx(ii) = ii;
32 pause
33 end
34 disp('[ii p f(p) xcond]')
35 [idx.' p.' f.' xcond.']
4 ans =
5
19
Figure 3.4: Root finding example with Newton-Rapson method.
Ex Find the zero of the function given in the previous example with initial value obtained
after applying the bisection method three times.
3 a = -1;
4 b = 2;
5 cond = 0.001;
6 Nb = 3;
7 dx = 0.001;
8
9 intv = b-a;
10 xx = a:0.01:b;
11 fxx = func_fx(xx);
12 plot(xx,fxx)
13
20
14 for ii=1:Nb
15 [fpii,pii,a,b] = func_bisection(a,b);
16 p(ii) = pii;
17 fp(ii) = fpii;
18 end
19
20 ii = Nb;
21 xcond(1:Nb) = 1;
22 idx(1:Nb) = 1:3;
23
24 cond1 = 1;
25 while cond1 > cond
26 ii = ii+1;
27 [fp(ii),p(ii)] = func_newton_rapson(pii,dx);
28 if ii>=2
29 cond1 = abs(p(ii)-p(ii-1))/intv;
30 end
31 xcond(ii) = cond1;
32 idx(ii) = ii;
33 end
34 disp('[ii p f(p) xcond]')
35 [idx.' p.' fp.' xcond.']
3 ans =
4
• Let us consider find the root in the domain of plane. The problem is to find (x, y)
21
satisfying the following:
df (x , y ) df (x , y ) "
# " #
0 0
dx dy 0 0 x − x0 = −f (x0 , y0 )
dg y − y −g(x0 , y0
dg 0
(x0 , y0 ) dy (x0 , y0 )
dx
−1
# df (x , y ) df (x , y ) "
" #
x − x0 dx 0 0 dy 0 0 −f (x0 , y0 )
=
y − y0 dg dg −g(x0 , y0
(x0 , y0 ) dy (x0 , y0 )
dx
−1
" # " # df (x , y ) df (x , y ) "
#
x x0 dx 0 0 dy 0 0 f (x0 , y0 )
= −
y y0 dg dg g(x0 , y0
(x0 , y0 ) dy (x0 , y0 )
dx
• Resulting expression
df −1
1 (x ) df1 (x ) ···
df1
(x )
dx 0 dx 0 dxN 0 f1 (x0 )
1 2
df
2 (x ) df2 (x ) df2
0 ··· (x0 ) f1 (x0 )
x = x0 − dx1 dx2 0 dxN
.. .. .. .. ..
. . . . .
dfN
dfN dfN
f N (x0 )
(x ) (x ) ··· (x )
dx1 0 dx2 0 dxN 0
22
x1 x10
x x
2 20
x = . , x0 = .
.. ..
xN xN 0
• Sometimes we have to find the roots of the given polynomial, and vice versa, the poly-
nomial coefficients of the given roots.
• For example, the following is the given polynomial:
1 >> P = [2 8 0 -4 6 8];
2 >> x0 = roots(P)
1 >> P1 = poly(x0)
3 %% f(x) = 2 x^3+5x^2-6x+8
23
-3.63
Figure 3.5: Root finding and polynomial coefficients.
4 P = [2 5 -6 8];
5 R = roots(P)
6
7 x = -5:0.01:2;
8 y = polyval(P,x);
9 plot(x,y), grid on
10 xlabel('x'), ylabel('f(x)')
11 text(-3,-40,'f(x) = 2x^3 + 5 x^2-6x+8’)
1 >>
2 R =
3
4 -3.6300 + 0.0000i
5 0.5650 + 0.8847i
6 0.5650 - 0.8847i
7
8 >> P1 = poly(R)
9 P1 =
10
24
11 1.0000 2.5000 -3.0000 4.0000
25
26
Chapter 4
Interpolation
• We consider the problem of finding a function that interpolates a given set of values:
n 0 1 ··· n ··· N
x x0 x1 · · · xn · · · xN
y y0 y1 · · · yn · · · yN
f (xn ) = yn for n = 0, 1, · · · N .
27
2
1
0
0 1 2
• Since there are three data points, the polynomial passing through these points with
the order 2 is unique. (Of course, there are many polynomials whose order is larger
than 2).
• 2nd-order polynomial passing through the data points is set as:
y = ax2 + bx + c
XP = y
28
2
x0 x0 1
a
y0
X= x2 x1 1 , p = b ,
y = y1
1
c y2
2
x2 x2 1
| {z }
Vandermonde matrix
p = X−1 y
Ex For the given data set, Find the polynomial passing through the data points given in the
following table:
n 0 1 N =2
x 1 1/2 3
y 3 −10 2
Solution
1 1 1 3
X = 1/4 1/2 1 , y = −10
9 3 1 2
−10.6
p = X−1 y = 41.9
−28.3
This is found to be same as the result obtained in the previous example !!!
Lagrange Interpolation
Joseph-Louis Lagrange (Jan. 25, 1736∼Apr. 10, 1813)
29
1 1
0 0
0 1 2
0 1
Linear Interpolation
• Lagrange suggested the following formula representing the function of curve passing
through the given data points (x0 , f (x0 )), (x1 , f (x1 )), · · · , and (xn , f (xN )):
30
Ex. Construct the polynomial interpolating the data by using Lagrange Polynomials.
n x y
0 1 3
1 1/2 −10
2 3 2
Solution
(x − 1/2) (x − 3) 1
L20 (x) = = − x − (x − 3)
(1 − 1/2) (1 − 3) 2
(x − 1)(x − 3) 4
L21 (x) = = (x − 1)(x − 3)
(1/2 − 1) (1/2 − 3) 5
(x − 1/2) (x − 1) 1 1
L22 (x) = = (x − 1) x −
(3 − 1/2) (3 − 1) 5 2
1
P2L (x) = −3 x − (x − 3)
2
− 8(x − 1)(x − 3)
2 1
+ (x − 1) x −
5 2
7 3
2
= −3 x − x +
2 2
2
− 8(x − 4x + 3)
2 3 1
+ x2 − x +
5 2 2
2
= −10.6 x + 41.9 x + −28.3
1 %% Lagrange Interpolation
2 clear all, close all, clc
3
4 x = [ 1 3 5 7];
5 y = [-5 1 -2 0];
6
31
7 %%
8 N = length(x);
9 Norder = N-1;
10
11 for ii = 1:N
12 idx = [1:ii-1 ii+1:N];
13 % Indices except i-th index
14 Denii = prod(x(ii)-x(idx));
15 % Prod of Den. Elements (!=i-th)
16 Numii = poly(x(idx));
17 % Poly Coef of Num. (order=Norder)
18 C(ii,:) = y(ii)*Numii/Denii;
19 % Coef Vector of ii-th Polynomial
20 end
21 CL = sum(C)
22 % Coef Vector of Resulting Lagrange Polynomial
23
1 CL =
2 0.2917 -3.7500 14.2083 -15.7500
3
4 P =
5 0.2917 -3.7500 14.2083 -15.7500
• For a given data set, the Lagrange interpolation can provides the appropriate polyno-
mial solution.
• If a new data point (xN +1 , yN +1 ) is added to the original one, the resulting polynomial
solution can also be found. However, each Lagrange Polynomial would have to be
updated, resulting in complicated work for a large N .
32
Figure 4.3: Example of Lagrange interpolation.
• Therefore there is a demand for alternative methods constructing the polynomials it-
eratively.
• With a proper PN (x) crossing the N + 1 data points, the following construction PN +1 (x)
seems to be reasonable:
PN +1 (x) = PN (x) + CN +1 (x)
CN +1 (x) = c (x − x0 )(x − x1 ) · · · (x − xN ), c: constant
• Note that
CN +1 (xn ) = 0, for n , N + 1
implying that PN +1 (x) will interpolate the data at x0 , x1 , · · · , xN .
• To get the value of the constant we calculate c such that
yN +1 = PN +1 (xN +1 )
= PN (xN +1 ) + c(xN +1 − x0 )(xN +1 − x1 ) · · · (xN +1 − xN )
yN +1 − PN (xN +1 )
c=
(xN +1 − x0 )(xN +1 − x1 ) · · · (xN +1 − xN )
yN +1 − PN (xN +1 )
→ cN +1 =
(xN +1 − x0 )(xN +1 − x1 ) · · · (xN +1 − xN )
33
• This construction is known as Newton’s Algorithm.
Ex
n x y
0 1 3
1 1/2 −10
2 3 2
Solution
Step 1: P0 (x) = 3
1
Step 2: P1 (x) = P0 (x) + c(x − 1) → −10 = 3 + c −
2
→ c = 26 → P1 (x) = 3 + 26(x − 1) = 26x − 23
1
Step 3: P2 (x) = P1 (x) + c(x − 1) x −
2
5
2 = 26 × 3 − 23 + c × 2 ×
2
53
→ c=− = −10.6
5
1
P2 (x) = P1 (x) − 10.6(x − 1) x −
2
2 3 1
= 26x − 23 − 10.6 x − x +
2 2
2
= −10.6 x + 41.9 x − 28.3
34
1 %% Newton's Forward Divide Difference
2 % for Interpolation
3 clear all, close all, clc
4
5 %%
6 x = [1 1/2 3]';
7 y = [3 -10 2]';
8
9 %%
10 N = length(x);
11
12 P = y(1);
13 xz = [];
14 for ii=2:N
15 xz = [xz x(ii-1)];
16 Den = prod((x(ii)-xz));
17 Num = y(ii)-polyval(P,x(ii));
18 c = Num/Den;
19 P = [0 P]+c*poly(xz);
20 end
21 P
1 P =
2
35
normally piecewise polynomial.
• Splines make use of a set of a number of subintervals obtained by cutting an interval.
Linear Splines
• A first spline (linear spline) is a function which is linear on each subinterval defined
by a partition:
• A function S is a linear spline on [a, b] if
x x0 x1 · · · xN
y y0 y1 · · · yN
• For a spline with this data, the linear polynomial on each subinterval is defined with
a slope ks as
Si (x) − yi = ks (x − xi )
y −y
Si (x) = yi + i+1 i (x − xi )
xi+1 − xi
Quadratic splines
• For a smooth interpolation, a quadratic spline can be considered, where the interpola-
tion function between successive data points are represented with 2nd-order polyno-
mials.
• The problem is to determine the related coefficients.
– Given (x0 , y0 ), (x1 , y1 ), · · · , (xN , yN ), fit quadratic splines through the data. The
36
Figure 4.4: Quadratic spline.
an , bn , and cn , n = 1, 2, · · · , N .
..
.
37
Figure 4.5: Example of quadratic spline.
2
f (xN −1 ) = aN xN −1 + bN xN −1 + cN
2
f (xN ) = aN xN + bN xN + cN
This condition gives 2N equations.
– Next the spline becomes smooth at the boundary if the first derivatives of two
quadratic splines are same at the boundary.
At x = x1 : 2 a1 x1 + b1 = 2 a2 x1 + b2
→ 2(a1 x1 − a2 x2 ) + b1 − b2 = 0
For all interior boundary points, N − 1 conditions are obtained:
2(an xn − an+1 xn+1 ) + bn − bn+1 = 0, n = 1, 2, · · · , N − 1
38
a) Velocity and acceleration at t = 16 seconds
b) Distance covered between t = 11 and t = 16 seconds
39
− Additional assumption
a1 = 0
• Matrix formulation
with
h i
A(1, 1 : 3) = x02 x0 1
h i
A(2, 1 : 3) = x12 x1 1 a1 0
h i
A(3, 4 : 6) = x 2
x 1
b1 227.04
h 1 1
i c 227.04
A(4, 4 : 6) = x22 x2 1 1
h
A(5, 7 : 9) = x2 x 1
i a2 362.78
2
h 2
b 362.78
A(6, 7 : 9) = x2 x3 1
i 2
3 c 517.35
h i 2
2
A(7, 10 : 12) = x x 1 a3
517.35
h 3 3
i
A(8, 10 : 12) = x42 x4 1 C = b3 B = 602.97
h i c3 602.97
A(9, 13 : 15) = x42 x4 1
h i a 901.67
A(10, 10 : 12) = x52 x5 1 4
h i b4 0
A(11, [1 4 2 5]) = 2x1 −2x1 1 −1
c 0
h i 4
A(12, [4 7 5 8]) = 2x2 −2x2 1 −1
a5
0
h i
A(13, [7 10 8 11]) = 2x3 −2x3 1 −1
b5
0
h i
c5 0
A(14, [10 12 11 14]) = 2x4 −2x4 1 −1
A(15, 1) = 1
• Coefficients
n an bn cn
1 0.0000 22.70 0.00
2 0.8888 4.93 88.88
3 −0.1356 35.66 −141.61
4 1.6048 −33.96 554.55
5 0.2089 28.86 −152.13
40
• The resulting velocity function becomes
+ 22.70 t, 0 ≤ t ≤ 10
+0.8888 t 2 + 4.93 t + 88.88,
10 ≤ t ≤ 15
v(t) = −0.1356 t 2 + 35.66 t − 141.61, 15 ≤ t ≤ 20
+1.6048 t 2 − 33.96 t + 554.55,
20 ≤ t ≤ 22.5
+0.2089 t 2 + 28.86 t − 152.13,
22.5 ≤ t ≤ 30
Acceleration at t = 16 sec
d
a(t) = − 0.1356 t 2 + 35.66 t − 141.61 , 15 ≤ t ≤ 20
dt
= −0.2712 t + 35.66
a(16) = 31.321 (m/s2 )
4 x = [0 10 15 20 22.5 30];
5 y = [0 227.04 362.78 517.35 602.97 901.67];
6
11 %%
12 A = zeros(Nm,Nm);
13 Ax = [(x.').^2 x.' ones(N1,1)];
14 for ii=1:N1
15 if ii==1
16 A(1,1:3) = Ax(1,:);
17 elseif ii==N1
18 ii2 = 3*(N1-2)+(1:3);
41
19 A(2*N,ii2) = Ax(N1,:);
20 else
21 ii1 = 2*(ii-1);
22 ii2 = 3*(ii-1)+(1:3);
23 A(ii1,ii2-3) = Ax(ii,:);
24 A(ii1+1,ii2) = Ax(ii,:);
25 end
26 end
27 dii = [1 3+1 2 3+2];
28 for ii=2:N
29 ii1 = 2*N+(ii-1);
30 ii2 = 3*(ii-2)+dii;
31 A(ii1,ii2) = [2*x(ii)*[1 -1] 1 -1];
32 end
33 A(3*N,1) = 1;
34
35 %%
36 B = zeros(Nm,1);
37 for ii=1:N
38 B(2*(ii-1)+1,1) = y(ii);
39 B(2*(ii-1)+2,1) = y(ii+1);
40 end
41 %%
42 A
43 B
44 C = inv(A)*B
42
– So, where should we stop?
– Universal consensus on optimal degree is a cubic one.
Cubic Splines
• The cubic spline for N + 1 data points, the spline S(x) is expressed as
C1 (x), x0 ≤ x ≤ x1
C2 (x), x1 ≤ x ≤ x2
S(x) = ..
.
CN (x), xN −1 ≤ x ≤ xN
Cn (x) = an x3 + bn x2 + cn x + dn
– First we require that the spline interpolate the given value at every data point:
Cn (xn−1 ) = yn−1
Cn (xn ) = yn
2 3
an + bn xn−1 + cn xn−1 + dn xn−1 = yn−1
→
an + bn xn + cn xn2 + dn xn3 = yn
– Notice that there are 2N of these conditions. Then to make S(x) as smooth as possible,
it is reasopnable to impose the following conditions for n = 1, 2, · · · , N − 1:
′ ′
Cn (xn ) = Cn+1 (xn )
Cn′′ (xn ) = C ′′ (xn )
n+1
2 2
bn + 2cn xn + 3dn xn = bn+1 + 2cn+1 xn + 3dn+1 xn
→
2cn + 6dn xn = 2cn+1 + 6dn+1 xn
43
Figure 4.6: Example of quadratic spline.
– Note that the equations above are all linear equations with respect to the unknowns
(coefficients). This feature makes splines easy to calculate since solving linear systems
is what computers do best.
Ex. For the following dataset, find the interpolated data for x = 0 : 0.05 : 2
Solution Even though we can developthe code for this example, it may be sufficient to use
Matlab built-in function. The following is the Matlab scrip:
1 %% Cubic Spline
2 clear all, close all, clc
3
44
Figure 4.7: Concept of least-square error method.
4 %%
5 x = [0 0.1 0.5 0.6 1 1.5 2];
6 y = [0 0.06 0.19 0.21 0.26 0.29 0.31];
7
8 xx = 0:0.05:2;
9 yy = spline(x,y,xx);
10 plot(x,y,'+',xx,yy)
45
4.3 Least Squre-Error Method
• Let us consider a moving car with a uniform velocity. The following table shows the
measured distance as a function of time.
n tn sn
1 1.2 1.1
2 2.3 2.1
3 3.0 3.1
4 3.8 4.0
5 4.7 4.9
6 5.9 5.9
• Let us try to find the coefficients of following line equation optimally fitting the above
measured data set:
sn → s(t) = c1 t + c0
• It is reasonable that the error at each point should be added independent of the sign
of error. Therefore the Mean Squred Error (MSE), given below, is very good canidate:
N
1 Xh i2
MSE = sn − s(tn )
N
n=1
• Next work is to find the best (c1 , c0 ) giving the Least MSE, which can be found as
∂MSE ∂MSE
= 0, =0
∂c1 ∂c0
• Here it is known that it is sufficient to consider the the total Squared Error (SE) instead
of the MSE to find (c1 , c0 ). the SE for this example can be found as
6 h
X i2 6 h
X i2
SE = sn − s(tn ) = sn − (c1 tn + c0 )
n=1 n=1
X6
= (sn2 − 2c1 tn sn − 2c0 sn + c12 tn2 + 2c1 c0 tn + c02 )
n=1
46
• Collecting this equation in terms of c1 and c0
6 6 6
X X X
SE = sn2 − 2c1 tn sn − 2c0 sn
n=1 n=1 n=1
6 6
X X
+ c12 tn2 + 2c1 c0 − 2c0 tn + c02
n=1 n=1
SE(c1 , c0 ) = 14.51 c1 − 29.50 c1 + 6.97 c1 c0 + c02 − 7.03 c0 + 15.00
2
• The method of least-squre error is solved systematically with the help of the Psudo-
inverse. The previous example can be written in a matrix form with the following X,
b, and y as
1.2 1 1.1
2.3 1 2.1
" #
3.0 1 b1 3.1
X = , b= , y =
3.8 1 b0 4.0
4.7 1 4.9
5.9 1 5.9
Xb → y
• In fact, Xb , y. Even we accept Xb = y to try to find b, but we can know b can not be
found because of non-invertible X (non-square matrix).
• Let us think of an approximate solution b which gives a least squared error. The squred
error is formed as
SE = Σ |y − Ab|2 : Scalar quantity
47
• This sacalr quantity SE can be expressed in a matrix form as
h i
SE = yT − (Xb)T [y − Xb]
= yT y − yT Xb − (Xb)T y + (Xb)T (Xb)
= yT y − yT X b − bT XT y + bT (XT X)b
• What is noteworthy is
– SE is a scalar.
– All elements of SE are also scalar.
– Especially yT y is a constant w.r.t. b.
• Let us take derivatives of SE with respect to b1 and b0 . In a matrix form, let us consider
the following:
∂ SE " #
48
Therefore
" #
∂ p
(Pb) = 1 = P = yT X
∂b p2
∂ T T ∂
b P = (Pb) = P = yT X
∂b ∂b
| {z }
from the
previous result
– bT Qb can be expressed as
h i q1 q2 b1
bT Qb = b1 b0
q 2 q 3 b 0
h i b1
= b1 q1 + b0 q2 b1 q2 + b0 q3
b0
= b1 q12 + b0 b1 q2 + b0 b1 q2 + b02 q3
49
• In summary
∂
h i
yT X b = XT y
∂b
∂
∂ SE
= −2 XT y + 2 XT Xb
h i
bT XT y = XT y →
∂b
∂b
∂ h i
bT (XT X)b = 2 (XT X)b
∂b
• Now XT X is a squre matrix (therefore invertible), the approximate solution b for least
SE can be found as
b = (XT X)−1 XT y
y = 1.0507 x − 0.1433
4 %%
5 x = [1.2 2.3 3.0 3.8 4.7 5.9].';
6 y = [1.1 2.1 3.1 4.0 4.9 5.9].';
7
8 N = length(x);
9 A = [x ones(N,1)];
10 pinvA = inv(transpose(A)*A)*transpose(A);
11 del_pinvA = pinvA-pinv(A); % Check by ``pinv(.)”
12 del_pinvA
13
14 %%
50
Figure 4.8: Example of least-square error method.
15 c = pinv(A)*y
16
17 xx = 1:0.01:6;
18 yy = c(1)*xx+c(2);
19 plot(x,y,'+',xx,yy)
1 del_pinvA =
2
3 1.0e-14 *
4
8 c =
9
10 1.0507
11 -0.1433
51
Orthogonal Least Squares
• There is a way to define SE not in the previous way but a new way called orthogonal
SE.
• The fig. shows well the related concept, and this method give more optimal from the
other viewpoint.
52
Chapter 5
Numerical Integrations
Z b
f (x) dx =???
a
Z b
f (x) dx = F(b) − F(a)
a
There may be a case that the closed form of the antiderivative does not exist or at least is
not known to humankind. Nevertheless, if you have to know such an integrand, you have
no way without relying on an approximation.
Often We will review the definition of the Riemann integral of a function. A partition of an
interval [a, b] is a finite, ordered collection of nodes xi :
53
1 1
( )
( )
Given such a partition, let Ln and Un be the lower and upper bounds on the n-th subinterval
[xn , xn+1 ]. Then the upper and lower sums are defined as
N
X −1
L(f ) = Ln (xn+1 − xn )
n=0
N
X −1
U (f ) = Un (xn+1 − xn )
n=0
Z b
1
f (x) dx = [L(f ) + U (f )]
a 2
54
Trapezoidal Rule
Z b
f (x) dx =???
a
Z b
f (a) + f (b)
f (x) dx ≃ (b − a)
a 2
With equal N -subsection having width ∆x, the composite trapezoidal rule yields a simplied
form:
Z b N −1
∆x X h i
f (x) dx ≃ f (xn ) + fn+1
a 2
n=0
∆x
h i
= f (x0 ) + f (xN ) + 2 f (x1 ) + f (x2 ) + · · · + f (xN −1 )
2
It is worthwhile to know that the resulting intrgral can be expressed as a linear expansion
with the function value at given points !!!
N
X
I≃ an f (xn )
n=0
Simpson’s Rule
If a given integrand is approximated with the following quadratic equation, the its integral
can be found as
f (x) ≃ f (x) ≃ c0 + c1 x + c2 x2
Zb
c c
f (x) dx = c0 (b − a) + 1 b2 − a2 + 2 b3 − a3
a 2 3
55
Figure 5.3: Integral with Gaussian quadrature.
where the coefficients c0 , c1 and c2 can be found from the following three data points as
" !#
a+b a+b
[a, f (a)] , ,f , [b, f (b)]
2 2
!
a+b
a2 f (b) + b2 f
(a) + ab [f (a) + f (b)] − 4ab f
2
c0 = 2
(b − a)
!
h i a+b
a f (a) + bf (b) + 3 af (b) + bf (a) − 4(a + b) f
2
c1 =
(b − a)2
" !#
a+b
2 f (a) + f (b) − 2f
2
c2 =
(b − a)2
This is also known to be expressed as a linear expansion similar as the Trapzoidal rule.
56
Gaussian Quadrature
Meaning of Quadrature in Dictionary (Oxford Dictionary):
Quarature in integral stands for means a approximate integral of a function as the linear
combination of the function at certain points:
Z b
f (x) dx ≃ a0 f (x0 ) + a1 f (x1 ) + · · · +aN f (xN )
a
Let us consider the case in Fig. 5.4 (a). Because the trapezoidal rule must pass through the
end points, there are cases where the formula results in a large error. Now, suppose that
the constraint of fixed base points was removed and we were free to evaluate the area under
a straight line joining any two points on the curve. By positioning these points wisely, we
could define a straight line that would balance the positive and negative errors as shown in
Fig. 5.4(b). Therefore, we can get an improved estimate of the integral.
Gaussian quadrature is the name for one class of techniques for implementing such a strat-
egy. The particular Gaussian quadrature formulas described in this section are called Gauss-
Legendre formulas, and will be described later in detail.
I = c0 f (a) + c1 f (b)
57
Figure 5.4: Algorithm of Gaussian quadrature.
b−a
c0 = c1 =
2
For the Gaussian quadrature, c0 , and c1 are determined to provide more accurate integration
results.
1) 2-Point Formula
• Just as was the case for the above derivation of the trapezoidal rule, the object of Gauss
quadrature is to determine the 4 unknowns (2 appropriate points and 2 coefficients) in
the following equation for much more accurate interal:
Z b
I= f (x) dx ≃ c0 f (x0 ) + c1 f (x1 ) · · · (8.3.2)
a
58
• Without the loss of generality (reasonable selection), we can choose
x1 = −x0
c1 = c0
This implies there are only two unknowns to be determined, and therefore two indepen-
dent conditions are sufficient for this problem.
• For this purpose, consider the following two meaningful integrands:
1) f (x) = 1
(5.1)
2) f (x) = x2
59
g(y)
y
b
a b y
f(x) a
-1 1 x -1 0 1 x
Figure 5.5: Transform for Gaussian quadrature.
• A simple change of variable can be used to translate other limits of integration into
this form.
Zb Z1
g(y) dy = f (x) dx → f (x) =???
a −1
b−a
→ f (x) = g(y)
2
Iexact = 1.640533
Solution
60
• The intgeral can be transformed as
Z 1
I= f (x) dx
−1
b−a
f (x) = g(y)
2
= 0.2 + 25y − 200y 2 + 675y 3 − 900y 4 + 400y 5 × 0.4
b−a a+b
y= x+ → y = 0.4x + 0.4
2 2
! !
−1 1
IGQ = f √ + f √
3 3
!
−1
f √ = 0.516741
3
→ IGQ = 1.822578
!
1
f √ = 1.305837
3
2) 3-Point Formula
• Similar way, the 3-point formula can be found with setting x0 = −x2 and a0 = a2 . In
addition, it is reasonable to let x1 = 0, resulting in a problem with only 3 unknowns as
h i
IGQ = c0 f (x0 ) + c1 f (x1 ) + c2 f (x2 ) = c1 f (0) + c2 f (x2 ) + f (−x2 )
61
Now applying the constraints:
f (x) = 1 → c1 + 2c2 = 2
f (x) = x2 → 2 c x2 = 2
2 2
3
2
f (x) = x4 → 2 c2 x24 =
5
r
2/5 3 3
→ x22 = = → x2 = = 0.7746
2/3 5 5
5 8
→ c2 = = 0.5556 → c1 = = 0.8889
9 9
Ex Use the 3-point formula from Table 22.1 to estimate the integral for
the same function as in previous Example.
Solution
3) 4-Point Formula
62
Figure 5.6: Coefficients for Gaussian quadrature.
r √
3 2 30
x1 = − ≃ 0.3400
7 35
√
r
3 2 30
x2 = 7 + 35 ≃ 0.8611
→
√
30
c1 = 1/2 + ≃ 0.6521
36
√
30
c2 = 1/2 − ≃ 0.3479
36
63
4) Higher-Point Formulas
• Values for c′ s and x′ s for up to 6-
point formula are summarized
in Table 22.1
64
Chapter 6
1 3 1 2
x(t) = t − t +K
3 2
where K is chosen such that x(a) = c.
65
Figure 6.1: Example of Euler method.
thus
Z t+∆t
x(t + ∆t) = x(t) + f (t, x(t)) dt
t
• If we can approximate the integral then we have a way of ‘stepping’ from t to t + ∆t, i.e.,
if we have a good approximate of x(t) we can approximate x(t + ∆t). Note that this means
that all the work we have put into approximating integrals can be used to approximate
the solution of ODEs.
• Using stepping schemes we can approximate x(tfinal ) given x(tinitial ) by taking a number of
steps. For simplicity we usually use the same step size, ∆t, for each step, though nothing
precludes us from varying the step size.
66
• If we apply the left-hand rectangle rule for approximating integrals to Eq. 10.2, we get
• This is called Euler’s Method (1st-order Taylor series method). Note this is essentially the
same as the forward difference approximation we made to the derivative, Eq. 7.1.
• Euler’s Method will underestimate x(t) because the curvature of the actual x(t) is positive,
and thus the function is always above its linearization. In Fig. 10.1, we see that eventually
the Euler’s Method approximation is very poor.
• For expecting better accuracy of integration f (t, x) from t to t + ∆t, the Trapezoid rule can
be applied as:
∆t h i
x(t + ∆t) ≃ x(t) + f (t, x(t)) + f (t + ∆t, x(t + ∆t))
2
However we cannot evaluate this exactly, since x(t +∆t) appears on both sides of the equa-
tion.
• Nevertheless it can be caculated with the help of appropriate approximation, and this is
the idea behind the Runge-Kutta Method, which we will study later.
• Applying the Kirchoff Voltage Law (KVL) to the given RC circuit with vc (0) = 0,
dvc (t)
v s (t) = i(t) R + vc (t)
vs (t) = Tc dt + vc (t)
→
dvc (t)
i(t) = C v (0) = 0
c
dt
67
+ vs(t)
vs(t) vc(t) 1
-
0 t
• Applying the Lapalce transform to find the exact unit-step (US) response of vc (t),
• For a pulse input as shown in Fig. XXX, the response of vc (t) therefore becomes
dvc (t) 1 1
+ vc (t) = vs (t)
dt Tc Tc
68
• By the Euler method with approximate difference formula of the derivative,
R = 50 Ω, C = 1 µF, V0 = 1, τ = 50 µs
calculate vc (t) with Euler’s method and plot the output waveform for −Tc ≤ t ≤ 5 Tc . Please
try the various time steps ∆t, and compare the results with the exact one.
• Applying the Kirchoff Voltage Law (KVL) to the given RLC circuit with vc (0) = 0 and
iL = 0,
di(t)
vs (t) = L dt + R i(t) + vc (t)
dv (t)
i(t) = C c
dt
dvc2 (t) dv (t)
v (t) = LC + RC c + vc (t)
s
dt dt
v (0) = 0,
i (0) = 0
c L
Rearranging
69
• Let us express the derivative with the Euler method:
R = 50 Ω, C = 1 µF, V0 = 1
and L satisfying ζ = 0.25, 1, 5, calculate the unit-step response of vc (t) with Euler’s method
and plot the output waveform for −Tc ≤ t ≤ 20 Tc . Please try the various time steps ∆t, and
compare the results with the exact one.
3 R = 50;
4 C = 1e-6;
5 stype = 1; % Source Type, 0: Unit Step, 1: Pulse
6 Tw = 100e-6; % Pulse Width
7 %%
8 Tc = R*C; % Time constant
9 Ts = Tc/100; % Sample Time
10 t = -Tc:Ts:5*Tc;
11 N = length(t);
12 if stype == 0 % Unit Step
13 Tw = max(t);
14 end
70
Figure 6.3: Result of the numerical solution of the RC circuit.
15 %% Euler Method
16 Vc = zeros(1,N); Vs = zeros(1,N);
17 ii1 = find((t>0)&(t<=Tw));
18 Vs(ii1) = 1;
19
7 R = 50;
8 C = 1e-6;
9 zeta = 5;
10
71
vs(t)
0 t
11 %%
12 L = C/((2*zeta/R)^2)
13 Tc = R*C/zeta;
14 Ts = Tc/20; % Sample Time
15 t = -Tc:Ts:20*Tc;
16 N = length(t);
17
18 %% Euler Method
19 a = 1+R*C/Ts+L*C/Ts^2; % Time constant
20 b = R*C/Ts+2*L*C/Ts^2;
21 c = -L*C/Ts^2;
22
23 Vc = zeros(1,N);
24 Vs = zeros(1,N);
25 ii1 = find(t>0);
26 Vs(ii1) = 1;
27
28 for ii=3:N
29 Vc(ii) = (b/a)*Vc(ii-1)+(c/a)*Vc(ii-2)+(1/a)*Vs(ii);
30 end
31
• We see that our more accurate integral approximations will be useless since they require
information we do not know, i.e., evaluations of f (t, x) for yet unknown x values. Thus
72
=5 =1 = 0.25
we fall back on Taylor’s Theorem (Theorem 1.1). We can also view this as using integral
approximations where all information comes from the left-hand endpoint.
• By Taylor’s theorem, if x has at least m + 1 continuous derivatives on the interval in ques-
tion, we can write
∆t 2 ′′ ∆t m (m)
x(t + ∆t) = x(t) + ∆t x′ (t) + x (t) + · · · + x (t)
2 m!
∆t m+1 (m+1)
+ x (τ)
(m + 1)!
where τ is between t and t + ∆t. Since τ is essentially unknown, our best calculable ap-
proximation to this
′ ∆t 2 ′′ ∆t m (m)
x(t + ∆t) ≃ x(t) + ∆t x (t) + x (t) + · · · + x (t)
2 m!
• This approximate solution to the ODE is called a Taylor’s series method of order m. We
also say that this approximation is a truncation of the Taylor’s series.
• The difference between the actual value of x(t + ∆t), and this approximation is called the
truncation error. The truncation error exists independently from any error in computing
the approximation, called round-off error.
Ex. Derive the Taylor’s series method for m = 3 for the following ODE.
dx(t)
x
dt = x + e
x(0) = 1
73
Solution ] The update equation can be found with simple calculus:
x(1) (t) = x + ex
x(2) (t) = x(1) + ex x(1) = x(1) (1 + ex )
x(3) (t) = x(2) + ex (x′ )2 + ex x(2) = ex (x′ )2 + x(2) (1 + ex )
Errors
– Truncation error is the error of truncating the Taylor’s series after the m-th term. You
can see that the truncation error is O(∆t m+1 ).
– Round-off error occurs because of the limited precision of computer arithmetic.
• While it is possible these two kinds of error could cancel each other, given our pessimistic
worldview, we usually assume they are additive.
• We also expect some tradeoff between these two errors as ∆t varies. As ∆t is made smaller,
the truncation error presumably decreases, while the roundoff error increases. This re-
sults in an optimal ∆t-value for a given method. See Fig. 10.2.
• Unfortunately, the optimal h-value will usually depend on the given ODE and the ap-
proximation method. For an ODE with unknown solution, it is generally impossible to
predict the optimal ∆t. We have already observed this kind of behavior, when analyzing
approximate derivatives (cf. Ex. 7.5.)
• Both of these kinds of error can accumulate. Since we step from x(t) to x(t + ∆t), an error
in the value of x(t) can cause a greater error in the value of x(t + ∆t). It turns out that for
some ODEs and some methods, this does not happen. This leads to the topic of stability.
2) Runge-Kutta Methods
Martin Kutta (1867-1944), engineer and mathematician, introduced the method in his PhD thesis, which was
developed further by Carl Runge (1856-1927), physicist and mathematician.
74
• Recall that the ODE problem is to find some x(t) such that
dx(t)
= f (t, x(t))
dt
x(a) = c
• We now examine the proper choices of the constants. Note that ω1 = 1, ω2 = 0 cor-
responds to Euler’s Method, and does not require computation of K2 . We will pick
another choice.
• Also notice that the definition of K2 should be related to Taylor’s theorem in two di-
mensions. Let’s look at it:
K2
= f (t + α∆t, x + βK1 )
∆t
= f t + α∆t, x + β∆t f (t, x)
(1) (1)
= f + α∆t ft (t, x) + β∆t f fx (t, x) + O(∆t 2 )
(1) (1)
= x(t) + (ω1 + ω2 )∆t x + ω2 ∆t αft + β f fx + O(∆t 3 )
′ 2
75
• The resulting expression of x(t + ∆t), choosing the following constants, becomes sim-
plified as
1
ω1 + ω2 = 1, αω2 = = βω2
2
1 2 (1)
(1)
x(t + ∆t) = x(t) + ∆t x + ∆t ft + f fx + O(∆t 3 )
′
2
1
= x(t) + ∆t x′ + ∆t 2 x′′ + O(∆t 3 )
2
i.e., our choice of the constants makes the approximate x(t + ∆t) good up to a O(∆t 3 )
term, because we end up with the Taylor’s series expansion up to that term.
• The usual choice of constants is α1 = α2 = 1, ω1 = ω2 = 1/2. This gives the 2nd-order
Runge-Kutta Method:
∆t ∆t
x(t + ∆t) ← x(t) + f (t, x) + f t + ∆t, x + ∆tf (t, x)
2 2
• This can be written (and evaluated) as
K1 ← ∆t f (t, x)
K2 ← ∆t f (t + ∆t, x + K1 )
1
x(t + ∆t) ← x(t) + (K1 + K2 )
2
Use ∆t = 0.1 to compute x(1.1) using both Taylor’s Series Methods and the 2nd-order Runge-Kutta
methods.
• The Runge-Kutta Method of order two has error term O(∆t 3 ). Sometimes this is not enough
and higher-order Runge-Kutta Methods are used.
76
• Among them, the 4-th-order Runge-Kutta method is known to be very accurate and effi-
cient. It is however too complex to derive its formula (out of scope of our study), and it
may be sufficient to use the final formula to our practical example.
• The following is the final formula of the 4-th order Runge-Kutta Method:
K1 ← ∆t f (t, x)
!
∆t 1
K2 ← ∆t f t + , x + K1
2 2
!
∆t 1
K3 ← ∆t f t + , x + K2
2 2
K4 ← ∆t f (t + ∆t, x + K3 )
• With the obtained K’s, x(t + ∆t) can be found from x(t) as
1
x(t + ∆t) = x(t) + [K + 2(K2 + K3 ) + K4 ]
6 1
1 1 1 1
= x(t) + K1 + K2 + K3 + K4
6 3 3 6
| {z }
Simpson’s Rule
The added terms in the final formula is same as that of the Simpson’s Rule in numerical
integrals.
• This method has order O(∆t 5 ).
• 2D difference approximation
∂2 V ∂2 V
∇2 V = + =0
∂x2 ∂y 2
∂V V (x + ∆x/2) − V (x − ∆x/2)
= V ′ (x) →
∂x ∆x
2
∂ V ′ ′
V (x + ∆x/2) − V (x − ∆x/2)
2
→
∂x ∆x
V (x + ∆x) − 2V (x) + V (x − ∆x)
=
∆x2
77
+
− +
If ∆x = ∆y = h,
∂2 V ∂2 V
∇2 V = + =0
∂x2 ∂y 2
[V (x + h, y) − 2V (x, y) + V (x − h, y)]
→
h2
[V (x, y + h) − 2V (x, y) + V (x, y − h)]
+ =0
h2
1h i
V (x, y) = V (x + h, y) + V (x − h, y) + V (x, y + h) + V (x, y − h)
4
5 + V2 + 0 + V5 − 4V1 = 0
→ −4V1 + V2 + 0 + V5 = −5
V5 + V7 + V2 + V10 − 4V6 = 0
→ V2 + V5 − 4V6 + V7 + V10 = 0
78
y
0V
b
1 2 3 4 2 V2
V0 5 6 7 8
0V V5 V6 V7
9 10 11 12 5 6 7
h
0 x V10 10
0 0V a h
A V = Vs → V = A−1 Vs
−4 1 0 0 1 0 0 0 0 0 0 0
..
.
A = 0 1 0 0 1 −4 1
1 0 0
..
.
0 0 0 0 0 0 0 1 0 0 1 −4
V1 −5
. .
.. ..
V = V6 , Vs = 0
. .
.. ..
V12 0
2) Method of Moments
79
Source points
z y
Field points
x
1 2 M
+Q
-Q 2 W
1
L
Figure 6.8: MOM solution example of differential equation.
of a capacitor is finite, and the real distrbution is different to the assumed. For this case,
some numerical approaches can give us a solution even though it is not a closed form.
• Here one numerical approach called moment method (or method of moments) is studied.
The followings are the steps for our purpose:
– Linear exapansion of the charge distribution on the upper plate with basis functions
and unknown coefficients:
80
R
X
qs = qsl δ(x − xsm , y − ysn )
l=1
l = (m − 1)N + j
qsl : point charge at the l-th index
– The charge distribution in the lower plate has the same but different sign.
– With an assumed potential difference V0 , the potential on the upper plate becomes
V + = V0 /2.
– Calculate the potential Vk at the reasonably chosen points on the upper plate:
k = (i − 1)N + j, l = (m − 1)N + n
R !
+ 1 X 1 1
Vk = al + − −
4πϵ0 Rkl Rkl
l=1
q
R+kl = (xi − xm )2 + (yj − yn )2
q
R−kl = (xi − xm )2 + (yj − yn )2 + d 2
Q = F−1 V+
X Qsum
Qsum = Q → C=
V0
81
82
Chapter 7
Random Number
• Let us consider a trial of throwing one dice. There are definitely 6 events (Different faces).
• If a variable x is the number of dots on the top surface, the following table shows the
related event, variable x, and probability of each event:
Event S1 S2 S3 S4 S5 S6
Variable x 1 2 3 4 5 6
PX (x) 1/6 1/6 1/6 1/6 1/6 1/6
• The event takes place randomly, therefore, the variable x is called Random Variable
(RV). However, for the time being, we call it Random Number (RN).
• Now let us consider the expectation (mean, average) and variance of x for one-time
throwing.
83
PX(x)
1/6
0 1 6 x
6
X
X= xn PX (xn )
n=1
2 2
σX2 ≜ (X − X)2 = X 2 − 2X X + X = X 2 − 2X X + X
2
= X2 − X
1 h i
σX2 = (1 − 3.5)2 + (2 − 3.5)2 + · · · + (6 − 3.5)2 = 2.9167
6
1 h i
σX2 = 12 + 22 + · · · + 62 − 3.52 = 2.9167
6
6 6
2
X X
2 2
σX = (xn − x) PX (xn ) = xn2 PX (xn ) − X
n=1 n=1
84
fX(x) Area = Probability
for interval x
Total Area = 1
0 x
x x+ x
Figure 7.2: Histogram to PDF conversion.
Continuously Distributed RN
• Let us consider one continupusly
distributed random number X.
• The probability of event from the
lowest to x is called the Cumulative
Distribution Function (CDF) which
is denoted by FX (x):
FX (x) = P (X ≤ x)
Therefore the probability that X lies in the semi-closed interval (a, b] can be written as
• If we let P∆x (x) as the probability of event from x to x + ∆x with a very small interval ∆x:
85
(1/6) (x-3)
q
1/6
0 1 6 x 0 1 x
1
1
1/6
0 1 6 x 0 1 x
(a) (b)
• Let us return to our 1st example of throwing a dice, we know the CDF beomes as
and the PDF can be expressed with the help of Dirac Delta Function as
6
X 1
fX (x) = δ(x − n)
6
n=1
86
• Let us consider a uniform distributed RN on x ∈ [0, 1] (Fig. XXX), whose PDF can be
written as
q, 0 ≤ x ≤ 1
fX (x) =
0, elsewhere
The unknown q can be found from the fact that the total probability should be 1:
Z1 Z1
fX (x) dx = q dx = 1 → q = 1
0 0
87
– Inputs
Number of data: N
Seed (Initial Condition): Z0
• Let us assume that we do not know the value of π. Then is there a way estimating its value
with the help of random number ???
– Let us consider a unit circle and the square perfectly touch this circle as shown in Fig.
XXX.
– We can write the areas of the unit circle and square
Auc = π, Asq = 4
– At first let us generate N -times trials of two uniform random numbers in [−1, 1], and
placing its pair into the xy-plane.
– Next calculate their distances from the origin, and counting the points Nin whose dis-
tance meets the following condition:
q
xi2 + yi2 ≤ 1
– We can estimate π with different N , and the result will be close to the announced π
with increasing N .
88
– Let us check this with Matlab simulation:
3 NN = 2.^([Link]);
4 th = ([Link])*pi/180;
5 for ii=1:length(NN)
6 N = NN(ii);
7 xn = 2*rand(1,N)-1;
8 yn = 2*rand(1,N)-1;
9 rn = sqrt(xn.^2+yn.^2); % Distance from the origin
10 ii1 = find(rn<=1);
11 Nin = length(ii1); % No. of points inside unit circle
12 PI(ii) = Nin/N*4; % Estimation of
13 if ii==1
14 subplot(2,1,1)
15 plot(xn,yn,’b+’), hold on, plot(cos(th),sin(th),'r'),
16 axis([-1.5 1.5 -1.5 1.5]), axis equal
17 pause(1)
18 end
19 end
20 subplot(2,1,2)
21 plot(NN,PI)
• Inputs
Number of data: N
Seed (Initial Condition): Z0
89
Figure 7.4: Calculation of π-value with random number.
– Histogram
– Normalization for all probability being 1.
• In statistics, histograms are widely used. For an N -times trials, we can count the number
of events Mn in the n-th small cell (interval) of the variable x. Repeating this for all evenly
spaced cells, the histogram having the following property is constructed:
h = {M1 , M2, · · · , MN }
N
X
Mn = N
n=1
90
• The approximate probability in the specific cell can be expressed as
Mn
Pn = ≃ fX (x)∆x
N
Therefore the PDF can be calculated as
h
fX (x) ≃
N ∆x
Linear Transformation of RN
Y = aX + b
2 2
σY2 = Y − Y = Y 2 − Y
h 2 i
= a2 X 2 + 2ab X + b2 − a2 X + 2ab X + b2
2
= a2 X 2 − X = a2 σX2
Sum of RN
Y = X1 + X2
where X1 and X2 are Identically and Independently Distributed (IID) RN. With
Y = X 1 + X 2 = 2X
2
σY2 = (X1 + X2 )2 − X1 + X2
2 2
= X12 + X22 + 2 X1 X2 − X1 − X2 − 2 X1 X2
h 2i
= 2 X12 − X1 = 2 σX2
91
• The variance of Y becomes not 4 times but 2 times of that of X, which is interesting. It
is noteworthy that the sum of two IID RNs (X1 + X2 ) is definitely different to the RN of 2
times of single RN (2X1 ).
• Now for the following RN:
N
X
Y= Xn , X1 , X2 , · · · , XN : IID RNs
n=1
Y =NX
√
σY2 = N σX2 → σY = σX
Y =NX
√
σY2 = N σX2 → σY = σX
• This result shows the effect of averaging if the determinstic signal is contaminated by
random signal.√N -times averaging makes the signal voltage N times, but the standard
deviation only N times. In other words, signal power becomes N 2 times, but the noise
power N times, resulting in an N -times increase in Signal to Noise Ratio (SNR):
SNRN = N × SNR1
Y = X1 + X2
y 2 3 4 5 6 7 8 9 10 11 12
fY (y) 1/36 1/6 1/36
Z = Y1 + Y2 → W = Z1 + Z2 → U = W1 + W2 · · ·
92
• New random variable X is given as the sum of IID random variables Xn having zero mean:
N
X
X = X1 + X2 + · · · + Xn + · · · = Xn
n=1
• As mentioned before, the thermal noise signal is known to follow the zero-mean Gaussian
distribution. Then what is the PDF of Gaussian random variable X with variance σ 2 .
– Let us start with an assumtion that its formula follows exponetial function of x2 as:
2
fX (x) = a e−bx
– There are two unknowns a and b, and there should be two independent conditions to
determinthese unknowns:
Z∞
i) fX (x) dx = 1
−∞
Z∞
ii) x2 fX (x) dx = σ 2
−∞
1 2 /(2σ 2 )
→ fX (x) = √ e−x
2π σ 2
• Let us generate the zero-mean Gaussian random number having a variance 1, that is
N (0, 1) in a two ways in Matlab program:
93
It is noteworthy to compare the obtained PDF with the exact formula.
Since the variance of uniform RV on [0, 1] is 1/12, the RV x1 is found to have x1 = 0 and
σ12 = 1.
• Now if a new random variable x is given as follows (the sum of IID uniform random
numbers x1 , x2 , · · · xN ):
N
1 X
x= √ xn
N n=1
then this RV will have zero mean and unit variance. In addition, this will follows the
Gaussian RV if we inicrease N .
• Let us check this with Matab programming.
Effect of Averaging
94
noise power with being N times only, resulting in an N -times increase in Signal to Noise
Ratio (SNR):
SNRN = N × SNR1
Random Variable
• In most electronic circuits and equipments, a complex gaussian RNs are needed to model
the signal, especially in determine the envelope of signal. We call this the IQ-processing,
and the related block diagram is shown in Fig. XXX.
• Conceptually this is same as the dart throwing experiment (2-D Gaussian RN).
• In this case, our concern is the distribution of the magnitude and square of the complex
Gaussian RN. Especially how will be the PDF of the magnitude around the origin. Defi-
nitely most of the points of dart exist around the origin, and become space away from the
origin. And we may think that the density becomes high around the origin.
• Let us examine this phenomena by simulation.
– Let us generate two zero-mean unit variance Gaussian RNs, and place one RN along
x-axis and the other along the y-axis, that is, on the xy-plane.
– Then calculate their distance R, and count the points within the concentric rings (their
centers are the origin) having the same width ∆r.
This is the first number produced by the Matlab RN generator with its default settings
(Start up matlab and set format long, type rand), and it’s the number you get.
95
• If all Matlab users, all around the world, all on different computers, keep getting this
same number, is it really random? No, it isn’t.
• Computers are (in principle) deterministic machines and should not exhibit random be-
havior.
• Other distributed RN can be generated from the UDRN with an appropriate transform.
• The state of the art for generating UDRN has advanced considerably in the last decade
and now begins to resemble a mature field.
• Issues are new mathematical algorithms and HW-based implementation.
• Caution: Many out-of-date and inferior methods remain in general use.
• Lehmer invented the Linear Congruential Generator (LCG), which is the basis for many
of the RN generators in use today. Lehmer’s generators involve three integer parameters,
a, c, and m, and an initial value, x0 , called the seed.
xk+1 = mod(axk + c, m)
With a = 13, c = 0, m = 31, x0 = 1
x = {1, 13, 14, 27, 10, 6, · · · }
c = 0 → Multiplicative LCG (MLCG).
96
• What’s the next value? Well, it looks pretty unpredictable, but you’ve been initiated. So
you can compute
mod(13 × 6, 31) = 16
• Many weakeness are observed, and there are increasing needs for advanced algorithems.
A Combined Generator may be a candidate !!!
1 function ux = func_rand_phill(N,Z0,Nb)
2
7 m = 2^Nb;
8 a = 2^(round(Nb/2))+3;
9 Z(1) = Z0;
10 for ii=2:N
11 Z(ii) = mod(a*Z(ii-1),m);
12 end
13 ux = Z/m;
97
General case
x x
f(x)
0
t
f(x)
0 x
98
x
Trial (n)
Simulaon
Exact
fX(x)
x
Figure 7.6: Example of histogram to pdf conversion.
3 N = 10000;
4 x = rand(1,N);
5 mx = mean(x)
6 sx2 = var(x,1)
7
8 dx = 0.01;
9 xd = -1:dx:2;
10 y = hist(x,xd);
11 fx = y/(N*dx);
12
13 fu = zeros(1,length(xd));
14 ii1 = find((xd>=0)&(xd<=1));
15 fu(ii1) = 1;
16
99
x
n
fX(x)
x
Figure 7.7: Gaussian random number.
• How can a RN x be generated for a specified PDF fX (x) from UDRN ???
fX (x) → x = {x1 , x2 , · · · }
• It can be feasible from a UDRNs U (0, 1) with a range (0,1) by using transformation method.
• Uniform distributed u has the following PDF:
u = U (0, 1) → fU (u) = 1, 0<u<1
100
• Relation between u and x by the transformation rule
du
fX (x) = fU (u)
dx
du
fX (x) =
dx
• Since fX (x) is always positive,
u = FX (x)
a RN x for a specified PDF can be found from the inverse CDF with an argument u.
x = FX−1 (u)
– Since FY (y) is just an area of fY (y) to the left of y, y for a u can be found from the data
of FY (y).
fW (w) = e−w
FW (w) = 1 − e−w
FW−1 = w = − ln(1 − F)
w = − ln(1 − u)
• Normal distribution RN can also be found from the Rayleigh one and UDRN. For two
independent normal distributed RNs, their combined magnitude and phase are known
to follow Rayleigth and uniform distributions.
• Box-Muller method
v1 = 2u1 − 1, v2 = 2u2 − 1
q
R = v12 + v22 : Square
102
1/6
0 1 6 x
1/6
6 1 0
Convolution
6/36
1/36
0 2 7 12 x
1+1
1+2 2+1
1+6 2+5 3+4 4+3 5+2 6+1
– Normal distributed RN
√
ξ = 2 erf−1 (2u − 1)
– Rayleigh and exponential distribution RNs can be generated from the normal
distribution.
• Case by case, depending on the available computing resource, one of the aboves can
be used !!!
103
Generalization
Central-Limit
Noise Voltage across a Resister Theorem Gaussian PDF
Single electron
in resistor at T = 300 oK Vn
t=0
+ - + -
+ -
Vn1(t0 )
t=t1 Vn1(t)
+ - 0 t
Vn1(t1 )
t=t2 Vn2(t)
- + 0 t
Vn1(t2 )
Vn1(t)
0 t
0 t
0 t
0 t
104
Vn1(t)
⋮
Vn8(t)
Vn(t)
Figure 7.10: Sum of uniform random numbers to generate Gaussian random noise.
Chaotic Signal
1 µ
xn+1 = −µ x − +
2 2
µh i
= 1 − |2xn − 1|
2
105
106
Appendix A
• While other programming languages usually work with numbers one at a time, MATLAB
operates on whole matrices and arrays.
• It include basic operations, such as creating variables, array indexing, arithmetic, and
data types.
• The well-known language C is mainly used for control of HW and embedded system
because of powerful capability of memory access. On the other hand, the Matlab has
an advantage in scientific and engineering calculations and data visualization.
107
After launching the Matlab, you can see the following main windows:
• Command window
• Editor
Compared to other lamnguage such as C, it is rather easy to learn the Matlab. From now,
we will study how to use the Matlab with some practical examples.
3 >> a = 4;
4 >> b = 2;
or in Editor window:
3 a = 4;
4 b = 2;
Now if you type the followings in the command widow, you can the following results:
1 >> c1 = a+b
2 >> c1 =
3 6
4
5 >> c2 = a-b
6 c2 =
7 2
108
2) test.m or test.p
test.p can be only run (source code is not seen)
1) Vector
1 >> a = [1 2 3]
2 a =
3 1 2 3
4
5 >> b = [6 7 10]
6 b =
7 6 7 10
2) Matrix
1 1 1 1 2 3
a = 1 2 3 , b = 6 7 9 (A.2)
5 4 2 2 4 5
In the Editor,
3 a=[1 1 1; 1 2 3; 5 4 2];
4 b=[1 2 3; 6 7 9; 2 4 5];
5 a
6 b
109
1 a =
2 1 1 1
3 1 2 3
4 5 4 2
5
6 b =
7 1 2 3
8 6 7 9
9 2 4 5
10
• Matrix multiplication
• Element by element multiplication
3 a = [1 1 1;
4 1 2 3;
5 5 4 2];
6 b = [1 2 3;
7 6 7 9;
8 2 4 5];
9
10 c = a*b;
11 d = a.*b;
12
13 c
14 d
While the size of two matrices satisfy the muliplication rule in ∗ multiplication, same size of
two matrices can be multiplied element by element (.∗). You can see the different results.
1 c =
2 9 13 17
110
3 19 28 36
4 33 46 61
5
6 d =
7 1 2 3
8 6 14 27
9 10 16 10
3 a = 3;
4 b1 = 2;
5 b2 = -2;
6
7 c1 = a/b1;
8 c2 = a/b2;
9 [c1 c2]
10
11 d1 = floor(a/b1);
12 d2 = ceil(a/b1);
13 [d1 d2]
14
15 d3 = rem(a,b1);
16 d4 = mod(a,b1);
17 [d3 d4]
18
19 d5 = rem(a,b2);
20 d6 = mod(a,b2);
21 [d5 d6]
1 ans =
2 1.5000 -1.5000
3
4 ans =
5 1 2
111
6
7 ans =
8 1 1
9
10 ans =
11 1 -1
3 N = 5;
4 a = zeros(1,N);
5 b = ones(1,N);
6 c = eye(3)
7
8 a
9 b
10 c
1 a =
2 0 0 0 0 0
3
4 b =
5 1 1 1 1 1
6
7 c =
8 a =
9 1 0 0
10 0 1 0
11 0 0 1
112
1 clear all, close all, clc
2
3 a = [1 1 0; 2 2 2; 3 4 5];
4 d = det(a);
5 b = inv(a);
6
7 d
8 b
1 d =
2 -2.0000
3
4 b =
5 -1.0000 2.5000 -1.0000
6 2.0000 -2.5000 1.0000
7 -1.0000 0.5000 0
3 x = 0;
4 for n=1:10
5 xn = n^2+3*n;
6 x = x+xn;
7 end
8 x
9
10 n = 1:10;
11 yn = n.^2+3*n;
113
12 y = sum(yn);
13 y
1 x =
2 550
3
4 y =
5 550
10
Y
x= n (A.4)
n=1
3 x = 1;
4 for n=1:10
5 x = x*n;
6 end
7 x
8
9 n = 1:10;
10 y = prod(n);
11 y
1 x =
2 3628800
3
4 y =
5 3628800
114
Signal plot
8 y=(abs(x 2)-1)
y
4
0
-4 -3 -2 -1 0 1 2 3 4
x
3 x=-4:0.1:4;
4 y=(abs(x)-1).^2;
5
6 plot(x,y)
7 title(‘Signal plot’)
8 xlabel(‘x’)
9 ylabel(‘y’)
10
11 text(0,8,’y=(abs(x)-1)^2’)
3 f = 1e3;
4 Ts = 0.05e-3;
5 t = 0:Ts:4e-3;
115
Figure A.3: Stem plot with axis setting
6 x = sin(2*pi*f*t);
7
8 figure(‘name’,’Sampled signal’)
9 stem(t,x)
10 title(‘Stem plot’), grid on
11 axis([0 max(t) -1.5 1.5])
3 thdeg=[Link];
4 th=thdeg*pi/180;
5 x=exp(j*th);
6
7 plot(real(x),imag(x))
8 axis([-1.5 1.5 -1.5 1.5])
9 axis equal
116
1
-1
-1 0 1
2-D Plots
117
where colorbar command shows the level of color displayed.
Next let us study how to use meshgrid and use it for pcolor plot.
1 xx =
2
3 0 0.5000 1.0000
4 0 0.5000 1.0000
5 0 0.5000 1.0000
6 0 0.5000 1.0000
7
9 yy =
10
118
11 1 1 1
12 2 2 2
13 3 3 3
14 4 4 4
All 12 points of (x, y) pair are formed. By using the meshgrid command, it will be easy to
calculate the related function and pcolor plot. Then the operation with a double loops can
be efficiently performed with matrix operation.
3 dx = 0.2;
4 dy = 0.2;
5
6 x = -3:dx:3;
7 Nx = length(x);
8 y = -3:dy:3;
9 Ny = length(y);
10
11 [xx,yy] = meshgrid(x,y);
12 z = exp(-xx.^2-yy.^2);
13
14 pcolor(x,y,z), colorbar
Then the same pcolor can be obtained. Furthermore, pcolor plot can be smoothed with
shading interp command.
3 dx = 0.2;
4 dy = 0.2;
5
6 x = -3:dx:3;
7 Nx = length(x);
8 y = -3:dy:3;
9 Ny = length(y);
10
11 [xx,yy] = meshgrid(x,y);
119
12 z = exp(-xx.^2-yy.^2);
13
14 pcolor(x,y,z), colorbar
15 shading interp
3 dx = 0.2; dy = 0.2;
4
5 x = -3:dx:3; Nx = length(x);
6 y = -3:dy:3; Ny = length(y);
7
8 [xx,yy] = meshgrid(x,y);
9 z = exp(-xx.^2-yy.^2);
10
11 zmin = floor(min(min(z))*10)/10;
12 zmax = ceil(max(max(z))*10)/10;
13 dz = (zmax-zmin)/10;
14
15 zlevels = zmin:dz:zmax;
16 [C,h] = contour(x,y,z,zlevels)
17 clabel(C,h)
120
18 shading interp, colorbar
In addition, quiver (meaning of arrow) plot is very useful for visualizing the vector quantity,
such as electric field vector and direction of wind blowing as well as the gradient of scalar
functions. One example is as follows:
3 dx = 0.2; dy = 0.2;
4 x = -3:dx:3; Nx = length(x);
5 y = -3:dy:3; Ny = length(y);
6
7 [xx,yy] = meshgrid(x,y);
8 z = xx.*yy.*exp(-xx.^2-yy.^2);
9
10 [zx,zy] = gradient(z,dx,dy);
11
12 contour(x,y,z)
13 shading interp
14 colorbar
15 hold on
16
17 quiver(x,y,zx,zy)
121
Figure A.8: 2D plot of quiver
If you need more information about these plot functions, you can use help command in
command window as
Other detailed programming skills will be learned through many examples of Numerical
Analysis.
122