Week
Week
ENGINEERING
MEC3456
Taylor series
3
MEC3456/MAE3456
END
5
Errors in numerical computation
MEC3456/MAE3456
Errors
§ Can also get “overflow” or “underflow” errors – i.e. numbers that are
too big/small to be represented in a computer
– These are usually due to programming error, and not usually
fundamental to numerical methods
Round-off error
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
31 30 23 22 0
9
MEC3456/MAE3456
0 0 0 0 0 0 1 0 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
31 30 23 22 0
0 1 0 0 0 1 0 0 0 1 0 1 1 1 1 0 1 1 0 1 0 0 1 0 1 0 0 0 1 0 0 0
31 30 23 22 0
10
MEC3456/MAE3456
Round-off error
11
MEC3456/MAE3456
Round-off error
12
MEC3456/MAE3456
Round-off error
§ It is possible that these errors could all be of the same sign, in which
case after N operations the error estimate is:
Total error ≈ N εm
Round-off error
14
MEC3456/MAE3456
Round-off error
- b ± b2 - 4ac
§ For example, finding the roots of a quadratic: x=
2a
§ This will produce big errors if b2 >> 4ac,
– e.g. b2 = 1010, 4ac = 1
– Writing in scientific notation, with same exponent
– b2 = 1.0e10, 4ac = 0.0000000001e10 => b2 - 4ac=1.0e10
15
MEC3456/MAE3456
Truncation error
Truncation error
y (x)
Stability
18
MEC3456/MAE3456
§ Overflow and/or underflow errors result from the fact that there is a
maximum and minimum exponent that can be represented
– (approx 10-40 – 10+40 for single precision, 32 bit)
– e.g. if real_max is largest real number that can be represented, what is
100*real_max?
Summary
§ Next recording
– MATLAB overview
20
Matrix equations and their solution
Brief recap of direct methods
MEC3456/MAE3456
22
MEC3456/MAE3456
23
MEC3456/MAE3456
§ Very expensive
– For n unknowns (n x n matrix), requires approx. 2n3/3 operations.
– For n =10 million (typical problem), requires approx 7 x 1020 operations
– On a fast PC (30 GigaFlops), this is 2 x 1010 sec. or ≈ 700 years
– Even on the worlds fastest computer, over 220 hours.
Tridiagonal form Ax = d
é b1 c1 0 ù é x1 ù é d1 ù
êa b c ú ê x ú êd ú
ê 2 2 2 úê 2 ú ê 2 ú
§ Often, systems arise which have the form ê a3 b3 • úê • ú = ê • ú
ê úê ú ê ú
ê • • cn -1 ú ê • ú ê•ú
§ This is called a 'tridiagonal' system êë 0 an bn úû êë xn úû êëd n úû
§ In this case, the efficient 'Thomas algorithm’ can be used (O(n))
– This is Gaussian elimination, but only have to eliminate the a’s
– The back substuitution only involves on b and one c
– There is the added benefit that you only have to store the numbers on
the tri-diagonal
26
MEC3456/MAE3456
END
27
Iterative methods – the Jacobi method
MEC3456/MAE3456
§ Introduce the “iteration” number – m and label the initial guess for each xi as
xi0 (and the next estimates xi1, xi2, xi3, ... xim , etc.)
a11
1 " 0$
x12 = b − a
# 2 21 1 x 0
− a x
23 3
0
− a x
24 4 %
a22
1 "
x31 = #b3 − a31 x1 − a32 x2 − a34 x4 $%
0 0 0
a33
1 " 0$
x14 = b − a x
# 4 41 1
0
− a x
42 2
0
− a x
43 3 %
a44
31
MEC3456/MAE3456
§ Introduce the “iteration” number – m and label the initial guess for each xi as
xi0. (and the next estimates xi1, xi2, xi3, ... xim , etc.)
32
MEC3456/MAE3456
§ Introduce the “iteration” number – m and label the initial guess for each xi as
xi0. (and the next estimates xi1, xi2, xi3, ... xim , etc.)
33
MEC3456/MAE3456
1# &
i−1 n
(
xi
m+1) m m
= %bi − ∑ aij x j − ∑ aij x j (
aii %$ j=1 j=i+1 ('
§ For some problems, we known values at the boundaries. Use these and
xi0 = 0 for interior points.
§ For time dependent problems we could use the value at the previous timestep
1# &
i−1 n
( m+1) m m
§ Then the Jacobi iteration xi = %bi − ∑ aij x j − ∑ aij x j ( can be written in
aii %$ ('
matrix form as j=1 j=i+1
Jacobi method
36
MEC3456/MAE3456
END
37
Iterative methods – the Gauss-Seidel method
MEC3456/MAE3456
§ With Jacobi, we always use the “old” values (xim) on the RHS.
§ With Gauss-Seidel, use the updated values as soon as they are
computed (i.e. we use xim+1 values where possible).
1 " m$
x1m+1 = b − a x m
− a x m
− a x
a11 # 1 12 2 13 3 14 4 %
1 " m$
x2m+1 = b − a x m+1
− a x m
− a x
a22 # 2 21 1 23 3 24 4 %
1 " m$
x3m+1 = b − a x m+1
− a x m+1
− a x
a33 # 3 31 1 32 2 34 4 %
1 " m+1 $
x4m+1 = b − a x m+1
− a x m+1
− a x
a44 # 4 41 1 42 2 43 3 %
39
MEC3456/MAE3456
1 # i−1 n &
xi( ) = %bi − ∑ aij x m+1
m+1 m
j
− ∑ a x
ij j
(
aii %$ j=1 j=i+1 ('
40
MEC3456/MAE3456
xin+1 − xin
ε a,i = n+1
< εs
xi
42
MEC3456/MAE3456
END
43
Iterative methods
Successive Over Relaxation (SOR)
MEC3456/MAE3456
1 # i−1 n &
§ Start with Gauss-Seidel xim+1 = %bi − ∑ aij x m+1 − a x m
∑ ij j ( i i( + x m
− x m
j
aii %$ j=1 j=i+1 '
)+ # i−1 n & -
1 m+
xim+1 = xim m+1 m
+ * %bi − ∑ aij x j − ∑ aij x j ( − xi .
+, aii %$ j=1 j=i+1 (' +/
45
MEC3456/MAE3456
1# &
i−1 n
§ This is equivalent to calculating x!im+1 m+1 m
= %bi − ∑ aij x j − ∑ aij x j ( (i.e. GS)
aii %$ j=1 j=i+1 ('
– SOR converges quickly for an optimal ωopt where 1 < ωopt < 2
– The optimal relaxation factor can be related to the spectral radius (λmax)
of the Gauss-Seidel iteration matrix.
ω = 2
opt
1+ 1− λ max
47
MEC3456/MAE3456
49
MEC3456/MAE3456
– They can be faster than determining the inverse of a very large matrix.
END
51
Root finding
MEC3456/MAE3456
§ Example: Artillery
§ How to define the mathematical
problem?
– Position of target = rT
– Position shell = rS(t,θ)
t is time, θ is inclination of barrel
§ Problem is, when is rT = rS(t,θ)
– i.e.
d(t,θ ) = (rT − rS (t,θ )) ⋅ (rT − rS (t,θ )) = 0
53
MEC3456/MAE3456
§ Any guess within this region will be classified as a root because |f(x)| < e
54
MEC3456/MAE3456
Root finding
56
MEC3456/MAE3456
END
57
Root finding – Bracketed methods
MEC3456/MAE3456
§ Thus we require
– a lower bound xl
– an upper bound xu
59
MEC3456/MAE3456
xr = f ( xl , xu )
2. If |f (xr)| < ε,
– we have found the root.
Else
– “choose” a new interval
– i.e. replace either xl or xu by xr
xl xr xu
3. Repeat steps 1 and 2
– Until |f (xr)| < ε
60
MEC3456/MAE3456
xl xr xu
61
MEC3456/MAE3456
xl xr xu
§ If f (xl) and f (xr) have different signs:
f (xl ) × f (xr ) < 0
– The root must lie between xl and xr
– We set the “new” xu = xr 62
MEC3456/MAE3456
§ Only thing left to do is determine the next guess - Two basic choices
§ Bisection § False position
– New guess is the midpoint – x-crossing of chord between bounds
xl + xu f (xu ) xl − f (xl ) xu
xr = xr =
2 f (xu ) − f (xl )
(xu , f(xu))
xr
(xl , f(xl))
xl xr xu
63
MEC3456/MAE3456
END
64
Root finding – Open methods
MEC3456/MAE3456
as f (xi ) xi xi+1
xi+1 = xi −
f "(xi )
69
MEC3456/MAE3456
xi+1 =x −
( x − x ) f (x )
i i−1 i
i xi-1 xi xi+1
( f (x ) − f (x ))
i i−1
70
MEC3456/MAE3456
71
MEC3456/MAE3456
72
MEC3456/MAE3456
Open method root finding : KEY IDEA
x3 x1
x0 x2
74
MEC3456/MAE3456
x5 x0 x2 x4 x1 x3
75
MEC3456/MAE3456
x0
76
MEC3456/MAE3456
END
77
Root finding: systems of equations
MEC3456/MAE3456
§ Example let
– a=2
– b=0
– c=-3
– d=0
– e=0
– r=1.5
! f1 (x1 , x2 ,......, xn ) = 0
# f (x , x ,......, x ) = 0
§ In terms of the # 2 1 2 n
#
general system " . .
# . .
#
#$ fn (x1 , x2 ,......, xn ) = 0
§ To simplify write
f ( x, y ) = 0
– f1(x,y) = f(x,y)
– f2(x,y) = g(x,y) g ( x, y ) = 0
81
MEC3456/MAE3456
Generalised Newton-Rapshon
§ Drawbacks…
– Have to write function files for all derivatives as well as functions.
– For 10 functions this is 110 function files required!
§ Modifications…
– Use the equivalent of the secant method to avoid derivatives.
– There are various Public Domain routines available to do this
§ see Numerical Recipes.
85
MEC3456/MAE3456
Summary
END
87