Chapter 19 Chapter 19 Chapter 19 Chapter 19 Numerics
Numerics in General in General
Bo-Yeong Kang, 2009
Kyungpook National University
1
What is What is
numerical analysis ? y
The study of algorithms to sol e the problems that
solve the problems that
there is no solution formula
3
19.1 Introduction
The steps from problems p p to answer
Modeling Modeling
Choosing a numeric methods programming
programming
Doing the computation
Interpreting the results
19.1 Introduction
Real number
Fl ti i t f f Floating point form of
numbers numbers
0.6247*10
3, 0.735*10
-13, -0.2000*
10-1Number of significant digits is kept fixed g g p Decimal point “floating”
5
19.1 Introduction
Floating point form of Floating point form of numbers
numbers
a = m*10
n, 0.1≤|m| < 1
On computers, m & n is limited p ,
ā = m*10
n, m=0.d
1d
2…d
k, d
1>0
mantissa
exponent mantissa
19.1 Introduction
Floating point form of n mbe s
numbers
ā = m*10
n, m=0.d
1d
2…d
k, d
1>0
mantissa
exponent exponent
sign mantissati
IBM 3000 & 4000 series Single precision floating point number 1 sign, 7 exponent, 24 mantissa, 32 bits
7
19.1 Introduction
Floating point form of oat g po t o o numbers
The number outside that range occurs
Underflow when the number is smaller (zero) Overflow when it is larger (halt)
exponent
sign mantissa
19.1 Introduction
Roundoff(
끝처리)
mantissa
Roundoff(
끝처리)
0.d
1d
2…d
kd
k+1d
k +2d
k+3… Roundoff rule
Discard (k+1)th and all subsequent decimals
Discard (k+1)th and all subsequent decimals
If the number discarded is less than half a unit in kth place, leave kth decimal unchanged(rounding down)
If it is greater than half a unit, round add one to the kth decimal If it is greater than half a unit, round add one to the kth decimal (rounding up)
If it is exactly half a unit, round off to the nearest even decimal
Rounding up/down happens about equallyg p/ pp q y ex)3.45-3.4, 3.55-3.6
9
19.1 Introduction
Error in Rounding Error in Rounding
ā= fl(a),
floating point approximation of a by rounding
|m-m| ≤ ½ * 10
-k (ex. |0.45-0.4| ≤ ½ * 10-1)|m| ≥ 0.1 , 1/|m| ≤ 10
|(a- ā)/a| ≈|(m-m)/m| ≤ ½ * 10
1-kError bound in rounding
19.1 Introduction
Errors of Numeric Results
Finding the results – Approximation
Rounding errors
Experimental errors from measurements
T ncating e o s f om p emat el b eaking off Truncating errors from prematurely breaking off
Formulas for Errors Formulas for Errors
є = a – ã
a = ã + є , True value = Approximation + error a ã + є , True value Approximation + error ã = 10.5, a=10.2, є = -0.3
11
19.1 Introduction
Errors of Numeric Results
є = |a – ã|(absolute error) or ã – a | |( )
є
r= є/a = (a-ã)/a = Error/True value (relative error ) This look useless because a is unknown
/ã є
r= є/ã
This still looks problematic because є is unknown
One can obtain in practice is an error bound for ã,
|є| ≤ , hence |a-ã| ≤
| | | |
|є
r|≤
r, hence |(a-ã)/a| ≤
r19.1 Introduction
E P ti
Error Propagation
How errors at the beginning and in later steps
propagate into the computation and affect accuracy (a) Subtraction
x = x+ є y=ỹ+є |є | ≤ |є | ≤
x = x+ є
1, y=ỹ+є
2, |є
1| ≤
1 ,|є
2| ≤
2є = |x-y – (x-ỹ)|
= |x-x – (y-ỹ)|
True value subtraction
Approximation subtraction
= |x x (y ỹ)|
= | є
1– є
2| ≤ |є
1|+|є
2| ≤
1+
213
19.1 Introduction
Error Propagation Error Propagation
(b) Relative error of multiplication
x = x+ є
1, y=ỹ+ є
2, |є
r1| ≤
r1 ,|є
r2| ≤
r2|( ỹ)/ | є
r= |(xy – xỹ)/xy|
= |(xy – (x- є
1)(y-є
2))/xy|
|( )/ |
= |(є
1y+є
2y-є
1є
2)/xy|
≈ |(є
1y+є
2x)/xy|
| / | | / | | | | |
≤ | є
1/x |+ |є
2/y| = |є
r1|+|є
r2| ≤
r1+
r219.1 Introduction
Error Propagation Error Propagation
Th 1 E P i
Th 1 E P i
Th.1 Error Propagation Th.1 Error Propagation
(a)In addition and subtraction, an error bound
f th lt i i b th f th
for the results is given by the sum of the error bounds for the terms
(b)I lti li ti d di i i b d
(b)In multiplication and division, an error bound for the relative error of the results is given by
h f h b d f h l i f
the sum of the bounds for the relative error of the given numbers
15
19 2 Solution of 19.2 Solution of
Equation by Iteration q o y o
19.2 Solution of Equation by Iteration
F(x) = 0
17
19.2 Solution of Equation by Iteration
F(x) = 0 F(x) 0
Zeros of Bessel function ?
19
19.2 Solution of Equation by Iteration
Algebraic equation Algebraic equation
Transcendental equation
19.2 Solution of Equation by Iteration
F(x) = 0
No formula for exact solution Approximation
Iteration method
21
19.2 Solution of Equation by Iteration
Iteration method Iteration method
Initial guess x 0 step by step x 1 x 2 step by step x 1 x 2 … Fi d i t it ti
Fixed point iteration Newton’s method
Secant method
19.2 Solution of Equation by Iteration
Fixed-point iteration p
Transform f(x) 0 to x g(x) Transform f(x)=0 to x=g(x)
Choose x 0 x 1 =g(x 0 ) x 2 2 =g(x g( 1 1 ) )
… X n+1 n+1 =g(x g n n )
23
19.2 Solution of Equation by Iteration
Ex1. Fixed-point iteration p
Transform to x=g(x)
Converge
Diverge
19.2 Solution of Equation by Iteration
Ex1. Fixed-point iteration p
Divide by x
25
Indexing Indexing
Extract the index terms Extract the index terms Weights
Data structure
Header file Ex. stdlib.h
27
Data type Data type Int integer Int, integer
Long, long integer
Float, floating-point
Double, double precision floating point
Indexing Indexing
Extract the index terms Extract the index terms Weights
Data structure
29
Indexing Indexing
Extract the index terms Extract the index terms Weights
Data structure
x 0 =1 0 x 0 =1.0
31
3 0 x 0 =3.0
(+,-)1.#INF, infinite number NaN not a number =
NaN, not a number =
(+,-)1.#IND (indeterminate)
19.2 Solution of Equation by Iteration
Ex1. Fixed-point iteration p
lower part, the slope of g1(x) is less than the slope of y=x
of y x
upper part, the slope of g1(x) is steeper than the slope of y=x
|g1’(x)| < 1 is sufficient for convergence
1.0 3.0 33
C f Fi d P i t
19.1 Introduction
Convergence of Fixed-Point Iteration
Iteration
Th 1 C Th 1 C
Th.1 Convergence Th.1 Convergence
Let x=s be a solution of x=g(x) and suppose
that g has a continuous derivative in some
interval J containing s. Then if |g’(x)| ≤ K < 1 in
J, the iteration process defined by (3) converges
for any x
0in J, and the limit of the sequence {x
n}
is s.
C f Fi d P i t
19.2 Solution of Equation by Iteration
Convergence of Fixed-Point Iteration
Iteration
|g’(x)|<1
S S
J 35
Proof
19.2 Solution of Equation by Iteration
Proof
|g’(x)|<1
S S
19.2 Solution of Equation by Iteration
Ex1. Fixed-point iteration p
Transform to x=g(x)
| ’( )| |2 /3|
|g1’(x)| = |2x/3|<1
-3/2 < x < 3/2 의 어떤 x0에서라도 수렴
37
19.2 Solution of Equation by Iteration
Ex1. Fixed-point iteration p
Divide by x
|g1’(x)| = |1/x2|<1
1 1 의 어떤 에서라도 수렴
-∞< x <-1, 1<x<∞ 의 어떤 x0에서라도 수렴
19.2 Solution of Equation by Iteration
Iteration method Iteration method
Initial guess x 0 step by step x 1 x 2 step by step x 1 x 2 … Fi d i t it ti
Fixed point iteration Newton’s method
Secant method
39
19.2 Solution of Equation by Iteration
Newton’s Method
F(x)=0
Commonly used
Simplicity and great speed
Simplicity and great speed
19.2 Solution of Equation by Iteration
Newton’s Method
Idea Tangent
41
19.2 Solution of Equation by Iteration
Newton’s Method
Fail, Try another x0
Success
Fail, Try another x, y 00
43
19.2 Solution of Equation by Iteration
Ex3. Square Root q
45
19.2 Solution of Equation by Iteration
Ex4. Transcendental Equation q
47
49
19.2 Solution of Equation by Iteration
Ex5. Algebraic Equation g q
51
19.2 Solution of Equation by Iteration
Order of an Iteration method Speed of Convergence
53
19.2 Solution of Equation by Iteration
Subtract g(s)=s
xn+1 – s = -g’(s) єn + 1/2 g’’(s) єn2 + … є = g’(s) є + 1/2 g’’(s) є 2 +
-єn+1 = -g (s) єn + 1/2 g (s) єn2 + … єn+1 = g’(s) єn - 1/2 g’’(s) єn2 + …
(a) єn+1 ≈ g’(s) єn in the case of first order
(b) є +1 ≈ -1/2 g’’(s) є 2 in the case of second order (b) єn+1 ≈ 1/2 g (s) єn in the case of second order Ex. єn=10-k, єn+1 = c*(10-k)2=c*10-2k
C f N t ’
19.2 Solution of Equation by Iteration
Convergence of Newton’s Method
Method
єn+1
= g’(s)
єn- 1/2 g’’(s)
єn2+ …
єn+1g (s)
єn1/2 g (s)
єn+ …
єn+1 ≈ -1/2 g’’(s) єn2 , second order if f’ and f’’ are not zero at a solution s
55
Taylor Series
미분가능한 어떤 함수를 다항식의 형태 미분가능한 어떤 함수를 다항식의 형태 로 근사화하는 방법
어떤 한 점에서 그것의 미분값으로 계 산되는 항들의 무한 합으로 표현
Taylor Series Taylor Series
f(x) = c
0+c
1(x-a)+c
2(x-a)
2+c
3(x-a)
3+…
(
단, |x-a|<R
때 수렴)
( | | )
x=a, f(a) = c0
f’(x) = c1+2c2(x-a)+3c3(x-a)2+…
x=a, f’(a) = c1
f’’(x) = 2c2+3*2c3(x-a)+…
x=a, f’’(a) = 2c2, f’’(a)/2 = c2
Cn =
f’’’(x) = 3*2c3+…
x=a, f’’’(a) = c3, f’’’(a)/(3*2) = c3
…
57
Taylor Series Taylor Series
The Taylor series of a real or complex function ƒ(x) y p ƒ( ) that is infinitely differentiable in a neighborhood of a real or complex number a, is the power series
where n! denotes the factorial of n and ƒ (n)(a) denotes the nth
derivative of ƒ evaluated at the point a; the zeroth derivative of ƒ is derivative of ƒ evaluated at the point a; the zeroth derivative of ƒ is defined to be ƒ itself and (x − a)0 and 0! are both defined to be 1.
In the particular case where a 0 the series is also
In the particular case where a = 0, the series is also
called a Maclaurin series.
처음 몇 항까지를 선택함으로써 x = a 주변에서의 f(x)의 근사식으로 사용
The exponential function y = ex (continuous red line) and the corresponding Taylor polynomial of degree four (dashed green line) around the origin
line) around the origin.
59
The exponential function (in blue) The exponential function (in blue), and the sum of the first n+1 terms of its Taylor series at 0 (in red).
19.2 Solution of Equation by Iteration
Iteration method Iteration method
Initial guess x 0 step by step x 1 x 2 step by step x 1 x 2 … Fi d i t it ti
Fixed point iteration Newton’s method
Secant method
61
19.2 Solution of Equation by Iteration
Secant Method
f’(x)
A far more difficult expression
Computationally expensive
Computationally expensive
19.2 Solution of Equation by Iteration
Secant Method
초기치 2개 필요 초기치 2개 필요
63
19.2 Solution of Equation by Iteration
Ex8. Secant Method
19.2 Solution of Equation by Iteration
Convergence of Secant Method Evaluation of derivatives
Avoided
Convergence
Almost quadratic like Newton Almost quadratic like Newton
|є | ≈ const| є |
1 62|є
n+1| ≈ const| є
n|
1.6265
19.2 Solution of Equation by Iteration
Iteration method Iteration method
Initial guess x 0 step by step x 1 x 2 step by step x 1 x 2 … Fi d i t it ti
Fixed point iteration Newton’s method
Secant method
19.3 Interpolation
67
19.3 Interpolation
approximate values approximate values
of a function f(x) for an x
between different x-values x 0 ,x 1 ,…,x n
19.3 Interpolation
69
19.3 Interpolation
Lagrange Interpolation
19.3 Interpolation
y-f 1 =(f 1 -f 0 /x 1 -x 0 )(x-x 1 )
71
19.3 Interpolation
Lagrange Interpolation g g p Idea
Idea
(x 0 ,f 0 ), (x 1 ,f 1 )
x=x 0 P 1 (x)=f 0
x=x 0 , P 1 (x)=f 0
x=x 1 , P 1 (x)=f 1
19.3 Interpolation
(x 0 ,f 0 ), (x 1 , f 1 ) x=x 0 , P 1 (x)=f 0 x=x 1 P 1 (x)=f 1 x=x 1 , P 1 (x)=f 1
P (x ) =f L +f L =f (L =1 L =0) P 1 (x 0 ) =f 0 L 0 +f 1 L 1 =f 0 (L 0 =1, L 1 =0) P 1 (x 1 ) =f 0 L 0 +f 1 L 1 = f 1 (L 0 =0, L 1 =1) L 0 = (x-x 1 )/(x 0 -x 1 )
L 0 = (x x 1 )/(x 0 x 1 ) L 1 = (x-x 0 )/(x 1 -x 0 )
73
19.3 Interpolation
y-f
1=(f
1-f
0/x
1-x
0)(x-x
1) y=(f
1-f
0/x
1-x
0)(x-x
1)+f
1={(f
1-f
0)(x-x
1)+f
1(x
1-x
0)}/(x
1-x
0)
={(-f
0)(x-x
1)+f
1(x-x
1)+f
1(x
1-x
0)}/(x
1-x
0)
={(-f
0)(x-x
1)+f
1(x-x
0)}/(x
1-x
0)
=(-f
0)(x-x
1) /(x
1-x
0)+f
1(x-x
0) /(x
1-x
0)
= f
0(x-x
1) /(x
0-x
1)+f
1(x-x
0) /(x
1-x
0)
= f
0L
0+f
1L
119.3 Interpolation
Ex1. Linear Lagrange Interpolation
P
1(x) =f
0L
0+f
1L
1 ,L = (x x )/(x x ) L = (x x )/(x x ) L
0= (x-x
1)/(x
0-x
1), L
1= (x-x
0)/(x
1-x
0)
75
19.3 Interpolation
L
0= (x-x
1)(x-x
2)/(x
0-x
1)(x
0-x
2)
L
0(x x
1)(x x
2)/(x
0x
1)(x
0x
2)
L
1= (x-x
0)(x-x
2)/(x
1-x
0)(x
0-x
2)
L
2= (x-x
0)(x-x
1)/(x
2-x
0)(x
2-x
1)
19.3 Interpolation
Quadratic Interpolation
Q p
three points three points
(x 0 ,f 0 ), (x 1 ,f 1 ), (x 2 ,f 2 ) x=x 0 , P 2 (x)=f 0
x=x 1 P 2 (x)=f 1 x=x 1 , P 2 (x)=f 1 x=x 2 , P 2 (x)=f 2
77
19.3 Interpolation
P 2 (x) =f 0 L 0 +f 1 L 1 +f 2 L 2 (x 0 ,f 0 ), (x 1 ,f 1 ), (x 2 ,f 2 )
x=x (L =1 L =0 L =0) x=x 0 , (L 0 =1, L 1 =0, L 2 =0) x=x 1 , (L 0 =0, L 1 =1, L 2 =0) x=x 2 , (L 0 =0, L 1 =0, L 2 =1)
L 0 = (x-x 1 )(x-x 2 )/(x 0 -x 1 )(x 0 -x 2 )
L ( )( )/( )( )
L 1 = (x-x 0 )(x-x 2 )/(x 1 -x 0 )(x 0 -x 2 )
L 2 2 = (x-x ( 0 0 )(x-x )( 1 1 )/(x )/( 2 2 -x 0 0 )(x )( 2 2 -x 1 1 ) )
19.3 Interpolation
Ex2. Quadratic Lagrange Interpolation
79
19.3 Interpolation
L ( )( )/( )( )
L
0= (x-x
1)(x-x
2)/(x
0-x
1)(x
0-x
2)
L
1= (x-x
0)(x-x
2)/(x
1-x
0)(x
0-x
2)
L
2= (x-x
0)(x-x
1)/(x
2-x
0)(x
2-x
1)
19.3 Interpolation
L = (x x )(x x )/(x x )(x x ) L
0= (x-x
1)(x-x
2)/(x
0-x
1)(x
0-x
2) L
1= (x-x
0)(x-x
2)/(x
1-x
0)(x
0-x
2) Code
작성Code
작성81
19.3 Interpolation
General Lagrange
I t l ti P l i l
Interpolation Polynomial
Error Estimate
83
19.3 Interpolation
Ex3. Error Estimate of Linear Interpolation
9 0 ≤ t ≤ 9 5 9.0 ≤ t ≤ 9.5
19.3 Interpolation
Ex3. Error Estimate of Linear Interpolation
85
19.3 Interpolation
Ex3. Error Estimate of Linear Interpolation
Interpolation Interpolation
Linear Lagrange Interpolation
Quadratic Lagrange Interpolation Quadratic Lagrange Interpolation
General Lagrange Interpolation Error Estimate
87
19 5 Numeric Integration 19.5 Numeric Integration
& Differentiation
N i l ti f Numeric evaluation of
integrals integrals
89
Rectangular Rule
middle point of 1-st subinterval
Trapezoidal Rule
91
19.5 Numeric Integration
Ex1. Trapezoidal Rule
F(0)+f(1)
93
19.3 Interpolation
0.746824
19.3 Interpolation
95
19.3 Interpolation
0.746824
Error Bounds and
Estimate for Trapezoidal Rule
97
Error Bounds and
Estimate for Trapezoidal Rule
Error Bounds and
Estimate for Trapezoidal Rule
99
Error Bounds and
Estimate for Trapezoidal Rule
f
19.5 Numeric Integration
Ex2. Error Estimation for Trapezoidal Rule
101
Simpson’s Rule Simpson s Rule
of Integration
Piecewise constant approximation pp Rectangular rule
Piecewise linear approximation Trapezoidal rule
Trapezoidal rule
Piecewise quadratic approximation Simpson’s rule p
103
Divide the interval
i t b
into even number
of equal subintervals
of equal subintervals
19.5 Numeric Integration
105
-h -2h h -h 2h h
19.5 Numeric Integration
107
Error of Simpson’s Rule Error Bound
19.5 Numeric Integration
Error of Simpson s Rule, Error Bound, Degree of Precision
Because Simpson rule uses second order, if n=2, error of Simpson rule has f(3) derivatives ((5) in pp.800 & (3*) in pp.819).
B hi th bl i T l l i l
By approaching the problem using Taylor polynomial, we can derive the error involves the fourth derivative of f(4) .
Simpson’s rule gives exact results when applied to any polynomial of degree three or less
polynomial of degree three or less.
109
Error of Simpson’s Rule Error Bound
19.5 Numeric Integration
Error of Simpson s Rule, Error Bound,
Degree of Precision
Error of Simpson’s Rule Error Bound
19.5 Numeric Integration
Error of Simpson s Rule, Error Bound, Degree of Precision
111
Error of Simpson’s Rule Error Bound
19.5 Numeric Integration
Error of Simpson s Rule, Error Bound, Degree of Precision
• Newton–Cotes formulas are a group of formulas for numerical integration based on
evaluating the integrand at equally-spaced points
Closed Newton-cotes formula, [a,b]
Open Newton-cotes formula, (a,b)
Numeric Differentiation
113
Computation of values of derivative of f(x)
from a given value of f(x)
from a given value of f(x)
Numeric differentiation should be avoided
whenever possible whenever possible
115
큰 값의 작은 차를 큰 값의 작은 차를
작은 양으로 나눈 것의 극한
약간의 부정확한
f(x)
값이라도 약간의 부정확한f(x)
값이라도f’(x)
에 큰 영향을 줄 수 있음19.5 Numeric Differentiation
f(x ) f(x1) f(x0)
x0 x1/2x1 h
117
19.5 Numeric Differentiation
f(x) = x
4f’(1.5) = ?
f(x ) f(x1) f(x0)
1.5 x1/2 2.0
x1 1.0
x0
118 x1/2
h=1
x1 x0
f’’(x ) ?
19.5 Numeric Differentiation
f’’(x
1) = ?
119
19.3 Interpolation
f(x) x
4f(x) = x
4f’’(2) = ?
Lagrange polynomial Lagrange polynomial
approximation, differentiation
P
2(x) =f
0L
0+f
1L
1+f
2L
2-h -2h
L
0= (x-x
1)(x-x
2)/(x
0-x
1)(x
0-x
2) L
1= (x-x
0)(x-x
2)/(x
1-x
0)(x
0-x
2)
L ( )( )/( )( )
L
2= (x-x
0)(x-x
1)/(x
2-x
0)(x
2-x
1)
121
Lagrange polynomial Lagrange polynomial
approximation, differentiation
x=x0 x=x1 x=x2
Numeric Integration g
Rectangular Rule Trapezoidal Rule Trapezoidal Rule
Simpson Rule Error Estimate
Differentiation Differentiation
123