CSE 541 – Numerical Methods
description
Transcript of CSE 541 – Numerical Methods
CSE 541 – Numerical Methods
Roger Crawfis
Ohio State University
CSE 541 – Numerical Methods
Root Finding
April 20, 2023 OSU/CIS 541 3
Root Finding Topics
• Bi-section Method• Newton’s method• Uses of root finding for sqrt() and reciprocal sqrt()• Secant Method• Generalized Newton’s method for systems of non-
linear equations– The Jacobian matrix
• Fixed-point formulas, Basins of Attraction and Fractals.
April 20, 2023 OSU/CIS 541 4
Motivation
• Many problems can be re-written into a form such as:– f(x,y,z,…) = 0– f(x,y,z,…) = g(s,q,…)
April 20, 2023 OSU/CIS 541 5
Motivation
• A root, r, of function f occurs when f(r) = 0.
• For example:– f(x) = x2 – 2x – 3
has two roots at r = -1 and r = 3.• f(-1) = 1 + 2 – 3 = 0
• f(3) = 9 – 6 – 3 = 0
– We can also look at f in its factored form.f(x) = x2 – 2x – 3 = (x + 1)(x – 3)
April 20, 2023 OSU/CIS 541 6
Factored Form of Functions
• The factored form is not limited to polynomials.
• Consider:f(x)= x sin x – sin x.
A root exists at x = 1.f(x) = (x – 1) sin x
• Or, f(x) = sin x => x (x – 1) (x – 2) …
April 20, 2023 OSU/CIS 541 7
Examples
• Find x, such that– xp = c, xp – c = 0
• Calculate the sqrt(2)– x2 – 2 = 0
• Ballistics– Determine the horizontal distance at which the
projectile will intersect the terrain function.
2 2 0 2 2x x x
April 20, 2023 OSU/CIS 541 8
Root Finding Algorithms
• Closed or Bracketed techniques– Bi-section– Regula-Falsi
• Open techniques– Newton fixed-point iteration– Secant method
• Multidimensional non-linear problems– The Jacobian matrix
• Fixed-point iterations– Convergence and Fractal Basins of Attraction
April 20, 2023 OSU/CIS 541 9
Bisection Method
• Based on the fact that the function will change signs as it passes thru the root.
• f(a)*f(b) < 0
• Once we have a root bracketed, we simply evaluate the mid-point and halve the interval.
April 20, 2023 OSU/CIS 541 10
Bisection Method
• c=(a+b)/2
a bc
f(a)>0
f(b)<0
f(c)>0
April 20, 2023 OSU/CIS 541 11
Bisection Method
• Guaranteed to converge to a root if one exists within the bracket.
c ba
a = cf(a)>0
f(b)<0f(c)<0
April 20, 2023 OSU/CIS 541 12
Bisection Method
• Slowly converges to a root
bca
b = cf(b)<0
April 20, 2023 OSU/CIS 541 13
Bisection Method
• Simple algorithm:Given: a and b, such that f(a)*f(b)<0Given: error tolerance, err
c=(a+b)/2.0; // Find the midpointWhile( |f(c)| > err ) {
if( f(a)*f(c) < 0 ) // root in the left halfb = c;
else // root in the right halfa = c;
c=(a+b)/2.0; // Find the new midpoint}return c;
April 20, 2023 OSU/CIS 541 14
Relative Error
• We can develop an upper bound on the relative error quite easily.
,b a x c
a xa x
a bxc
April 20, 2023 OSU/CIS 541 15
Absolute Error
• What does this mean in binary mode?– err0 |b-a|
– erri+1 erri/2 = |b-a|/2i+1
• We gain an extra bit each iteration!!!
• To reach a desired absolute error tolerance:– erri+1 errtol
2
2
log
toln
tol
b aerr
b an
err
April 20, 2023 OSU/CIS 541 16
Absolute Error
• The bisection method converges linearly or first-order to the root.
• If we need an accuracy of 0.0001 and our initial interval (b-a)=1, then:
2-n < 0.0001 ⁼ 14 iterations
• Not bad, why do I need anything else?
April 20, 2023 OSU/CIS 541 17
A Note on Functions
• Functions can be simple, but I may need to evaluate it many many times.
• Or, a function can be extremely complicated. Consider:
• Interested in the configuration of air vents (position, orientation, direction of flow) that makes the temperature in the room at a particular position (teacher’s desk) equal to 72°.
• Is this a function?
April 20, 2023 OSU/CIS 541 18
A Note on Functions
• This function may require a complex three-dimensional heat-transfer coupled with a fluid-flow simulation to evaluate the function. hours of computational time on a supercomputer!!!
• May not necessarily even be computational.
• Techniques existed before the Babylonians.
April 20, 2023 OSU/CIS 541 19
Root Finding Algorithms
• Closed or Bracketed techniques– Bi-section– Regula-Falsi
• Open techniques– Newton fixed-point iteration– Secant method
• Multidimensional non-linear problems– The Jacobian matrix
• Fixed-point iterations– Convergence and Fractal Basins of Attraction
April 20, 2023 OSU/CIS 541 20
Regula Falsi
• In the book under computer problem 16 of section 3.3.
• Assume the function is linear within the bracket.
• Find the intersection of the line with the x-axis.
April 20, 2023 OSU/CIS 541 21
Regula Falsi
a bc
( ) ( )( ) ( )
( ) ( )( ) 0 ( )
0 ( )( ) ( )
( )
( ) ( )
f a f by x f b x b
a bf a f b
y c f b c ba b
a bf b c b
f a f b
f b a bc b
f a f b
f(c)<0
April 20, 2023 OSU/CIS 541 22
Regula Falsi
• Large benefit when the root is much closer to one side.
– Do I have to worry about division by zero?
a c b
April 20, 2023 OSU/CIS 541 23
Regula Falsi
• More generally, we can state this method as:
c=wa + (1-w)b
– For some weight, w, 0w 1.– If |f(a)| >> |f(b)|, then w < 0.5
• Closer to b.
April 20, 2023 OSU/CIS 541 24
Bracketing Methods
• Bracketing methods are robust• Convergence typically slower than open methods• Use to find approximate location of roots• “Polish” with open methods• Relies on identifying two points a,b initially such
that:• f(a) f(b) < 0
• Guaranteed to converge
April 20, 2023 OSU/CIS 541 25
Root Finding Algorithms
• Closed or Bracketed techniques– Bi-section– Regula-Falsi
• Open techniques– Newton fixed-point iteration– Secant method
• Multidimensional non-linear problems– The Jacobian matrix
• Fixed-point iterations– Convergence and Fractal Basins of Attraction
April 20, 2023 OSU/CIS 541 26
Newton’s Method
• Open solution, that requires only one current guess.
• Root does not need to be bracketed.
• Consider some point x0.
– If we approximate f(x) as a line about x0, then we can again solve for the root of the line.
0 0 0( ) ( )( ) ( )l x f x x x f x
April 20, 2023 OSU/CIS 541 27
Newton’s Method
• Solving, leads to the following iteration:
01 0
0
1
( ) 0
( )
( )
( )
( )i
i ii
l x
f xx x
f x
f xx x
f x
April 20, 2023 OSU/CIS 541 28
Newton’s Method
• This can also be seen from Taylor’s Series.
• Assume we have a guess, x0, close to the actual root. Expand f(x) about this point.
• If dx is small, then dxn quickly goes to zero.
2
( ) ( ) ( ) ( ) 02!
i
i i i i
x x x
xf x x f x xf x f x
1
( )
( )i
i ii
f xx x x
f x
April 20, 2023 OSU/CIS 541 29
Newton’s Method
• Graphically, follow the tangent vector down to the x-axis intersection.
xi xi+1
April 20, 2023 OSU/CIS 541 30
Newton’s Method
• Problems
diverges
x0
1
2
3
4
April 20, 2023 OSU/CIS 541 31
Newton’s Method
• Need the initial guess to be close, or, the function to behave nearly linear within the range.
April 20, 2023 OSU/CIS 541 32
Finding a square-root
• Ever wonder why they call this a square-root?
• Consider the roots of the equation:• f(x) = x2-a
• This of course works for any power:
0,p pa x a p R
April 20, 2023 OSU/CIS 541 33
Finding a square-root
• Example: 2 = 1.4142135623730950488016887242097
• Let x0 be one and apply Newton’s method.
2
1
0
1
2
( ) 2
2 1 2
2 2
1
1 2 31 1.5000000000
2 1 2
1 3 4 171.4166666667
2 2 3 12
ii i i
i i
f x x
xx x x
x x
x
x
x
April 20, 2023 OSU/CIS 541 34
Finding a square-root
• Example: 2 = 1.4142135623730950488016887242097
• Note the rapid convergence
• Note, this was done with the standard Microsoft calculator to maximum precision.
3
4
5
6
1 17 24 5771.414215686
2 12 17 408
1.4142135623746
1.4142135623730950488016896
1.4142135623730950488016887242097
x
x
x
x
April 20, 2023 OSU/CIS 541 35
Finding a square-root
• Can we come up with a better initial guess?
• Sure, just divide the exponent by 2.– Remember the bias offset– Use bit-masks to extract the exponent to an
integer, modify and set the initial guess.
• For 2, this will lead to x0=1 (round down).
April 20, 2023 OSU/CIS 541 36
Convergence Rate of Newton’s
• Now,
2
2
0 ( ) ( )
1( ) ( ) ( ) ( ), ,
21
( ) ( ) ( )2
n n n n
n n
n n n n n n n n n
n n n n n
e x x or x x e
f x f x e
f x e f x e f x e f for some x x
f x e f x e f
1 1
21
( ) ( )
( ) ( )
( ) ( )
( )
( )1
2 ( )
n nn n n n
n n
n n n
n
nn n
n
f x f xe x x x x e
f x f x
e f x f x
f x
fe e
f x
April 20, 2023 OSU/CIS 541 37
Convergence Rate of Newton’s
• Converges quadratically.
21
10 ,
10
kn
kn
if e then
e c
April 20, 2023 OSU/CIS 541 38
Newton’s Algorithm
• Requires the derivative function to be evaluated, hence more function evaluations per iteration.
• A robust solution would check to see if the iteration is stepping too far and limit the step.
• Most uses of Newton’s method assume the approximation is pretty close and apply one to three iterations blindly.
April 20, 2023 OSU/CIS 541 39
Division by Multiplication
• Newton’s method has many uses in computing basic numbers.
• For example, consider the equation:
• Newton’s method gives the iteration:
10a
x
21
2
1
1
2
kk k k k k
k
k k
ax
x x x x ax
x
x ax
April 20, 2023 OSU/CIS 541 40
Reciprocal Square Root
• Another useful operator is the reciprocal-square root.– Needed to normalize vectors– Can be used to calculate the square-root.
1a a
a
April 20, 2023 OSU/CIS 541 41
Reciprocal Square Root
• Newton’s iteration yields:
2
3
1( ) 0
2( )
Let f x ax
f xx
3
1
2
2 21
32
k kk k
k k
x xx x a
x ax
April 20, 2023 OSU/CIS 541 42
1/Sqrt(2)
• Let’s look at the convergence for the reciprocal square-root of 2.
0
21
22
3
4
5
6
7
1
0.5 1 3 2 1 0.5
0.5 0.5 3 2 0.5 0.625
0.693359375
0.706708468496799468994140625
x 0.707106444695907075511730676593228
x 0.707106781186307335925435931237738
x 0.7071067811865475244008442397
x
x
x
x
x
2481
If we could only start
here!!
April 20, 2023 OSU/CIS 541 43
1/Sqrt(x)
• What is a good choice for the initial seed point?– Optimal – the root, but it is unknown– Consider the normalized format of the number:
– What is the reciprocal?– What is the square-root?
12721 2 (1. )
s e m
April 20, 2023 OSU/CIS 541 44
1/Sqrt(x)
• Theoretically,
• Current GPU’s provide this operation in as little as 2 clock cycles!!! How?
• How many significant bits does this estimate have?
1 111272 22
127122
3 1271 12722
11. 2
1. 2
1. 2
e
e
e
x mx
m
m
New bit-pattern for the exponent
April 20, 2023 OSU/CIS 541 45
1/Sqrt(x)
• GPU’s such as nVidia’s FX cards provide a 23-bit accurate reciprocal square-root in two clock cycles, by only doing 2 iterations of Newton’s method.
• Need 24-bits of precision =>– Previous iteration had 12-bits of precision– Started with 6-bits of precision
April 20, 2023 OSU/CIS 541 46
1/Sqrt(x)
• Examine the mantissa term again (1.m).• Possible patterns are:
– 1.000…, 1.100…, 1.010…, 1.110…, …
• Pre-compute these and store the results in a table. Fast and easy table look-up.
• A 6-bit table look-up is only 64 words of on chip cache.
• Note, we only need to look-up on m, not 1.m.• This yields a reciprocal square-root for the first
seven bits, giving us about 6-bits of precision.
April 20, 2023 OSU/CIS 541 47
1/Sqrt(x)
• Slight problem:– The produces a result between 1 and 2.– Hence, it remains normalized, 1.m’.– For , we get a number between ½ and 1.– Need to shift the exponent.
1.m
1
x
April 20, 2023 OSU/CIS 541 48
Root Finding Algorithms
• Closed or Bracketed techniques– Bi-section– Regula-Falsi
• Open techniques– Newton fixed-point iteration– Secant method
• Multidimensional non-linear problems– The Jacobian matrix
• Fixed-point iterations– Convergence and Fractal Basins of Attraction
April 20, 2023 OSU/CIS 541 49
Secant Method
• What if we do not know the derivative of f(x)?
Tangent vector
xi xi-1
Secant line
April 20, 2023 OSU/CIS 541 50
Secant Method
• As we converge on the root, the secant line approaches the tangent.
• Hence, we can use the secant line as an estimate and look at where it intersects the x-axis (its root).
April 20, 2023 OSU/CIS 541 51
Secant Method
• This also works by looking at the definition of the derivative:
• Therefore, Newton’s method gives:
• Which is the Secant Method.
0
1
1
( ) ( )( )
( ) ( )( )
limh
k kk
k k
f x h f xf x
h
f x f xf x
x x
11
1
( )( ) ( )
k kk k k
k k
x xx x f x
f x f x
April 20, 2023 OSU/CIS 541 52
Convergence Rate of Secant
• Using Taylor’s Series, it can be shown (proof is in the book) that:
1 1
1 1
( )1
2 ( )
k k
kk k k k
k
e x x
fe e c e e
f
April 20, 2023 OSU/CIS 541 53
Convergence Rate of Secant
• This is a recursive definition of the error term. Expressed out, we can say that:
• Where =1.62.
• We call this super-linear convergence.
1k ke C e
April 20, 2023 OSU/CIS 541 54
Root Finding Algorithms
• Closed or Bracketed techniques– Bi-section– Regula-Falsi
• Open techniques– Newton fixed-point iteration– Secant method
• Multidimensional non-linear problems– The Jacobian matrix
• Fixed-point iterations– Convergence and Fractal Basins of Attraction
April 20, 2023 OSU/CIS 541 55
Higher-dimensional Problems
• Consider the class of functions f(x1,x2,x3,…,xn)=0,
where we have a mapping from n.
• We can apply Newton’s method separately for each variable, xi, holding the other variables fixed to the current guess.
April 20, 2023 OSU/CIS 541 56
Higher-dimensional Problems
• This leads to the iteration:
• Two choices, either I keep of complete set of old guesses and compute new ones, or I use the new ones as soon as they are updated.
• Might as well use the more accurate new guesses.• Not a unique solution, but an infinite set of solutions.
1 2
1 2
, , ,
, , ,i
ni i
x n
f x x xx x
f x x x
April 20, 2023 OSU/CIS 541 57
Higher-dimensional Problems
• Example:• x+y+z=3
– Solutions:• x=3, y=0, z=0
• x=0, y=3, z=0
• …
April 20, 2023 OSU/CIS 541 58
Systems of Non-linear Equations
• Consider the set of equations:
1 1 2
2 1 2
1 2
, , , 0
, , , 0
, , , 0
n
n
n n
f x x x
f x x x
f x x x
April 20, 2023 OSU/CIS 541 59
Systems of Non-linear Equations
• Example:
• Conservation of mass coupled with conservation of energy, coupled with solution to complex problem.
2 2 2
3
5
1x
x y z
x y z
e xy xz
Plane intersected with asphere, intersected with amore complex function.
April 20, 2023 OSU/CIS 541 60
Vector Notation
• We can rewrite this using vector notation:
1 2
1 2
( )
, , ,
, , ,
n
n
f f f
x x x
f x 0
f
x
April 20, 2023 OSU/CIS 541 61
Newton’s Method for Non-linear Systems
• Newton’s method for non-linear systems can be written as:
1( 1) ( ) ( ) ( )
( )
k k k k
kwhere is the Jacobian matrix
x x f x f x
f x
April 20, 2023 OSU/CIS 541 62
The Jacobian Matrix
• The Jacobian contains all the partial derivatives of the set of functions.
• Note, that these are all functions and need to be evaluated at a point to be useful.
1 1 1
1 2
2 2 2
1 2
1 2
n
n
n n n
n
f f f
x x x
f f f
x x x
f f f
x x x
J
April 20, 2023 OSU/CIS 541 63
The Jacobian Matrix
• Hence, we write
( ) ( ) ( )1 1 1
1 2
( ) ( ) ( )2 2 2( )
1 2
( ) ( ) ( )
1 2
( )
i i i
n
i i i
in
i i in n n
n
f f f
x x x
f f f
x x x
f f f
x x x
x x x
x x xJ x
x x x
April 20, 2023 OSU/CIS 541 64
Matrix Inverse
• We define the inverse of a matrix, the same as the reciprocal.
11a
a
1
1 0 0
0 1 0AA I
0 0 1
April 20, 2023 OSU/CIS 541 65
Newton’s Method
• If the Jacobian is non-singular, such that its inverse exists, then we can apply this to Newton’s method.
• We rarely want to compute the inverse, so instead we look at the problem.
1( 1) ( ) ( ) ( )
( ) ( )
i i i i
i i
x x f x f x
x h
April 20, 2023 OSU/CIS 541 66
Newton’s Method
• Now, we have a linear system and we solve for h.
• Repeat until h goes to zero.
We will look at solving linear systems later in the course.
( ) ( ) ( )
( 1) ( ) ( )
k k k
i i i
J x h f x
x x h
April 20, 2023 OSU/CIS 541 67
Initial Guess
• How do we get an initial guess for the root vector in higher-dimensions?
• In 2D, I need to find a region that contains the root.
• Steepest Decent is a more advanced topic not covered in this course. It is more stable and can be used to determine an approximate root.
April 20, 2023 OSU/CIS 541 68
Root Finding Algorithms
• Closed or Bracketed techniques– Bi-section– Regula-Falsi
• Open techniques– Newton fixed-point iteration– Secant method
• Multidimensional non-linear problems– The Jacobian matrix
• Fixed-point iterations– Convergence and Fractal Basins of Attraction
April 20, 2023 OSU/CIS 541 69
Fixed-Point Iteration
• Many problems also take on the specialized form: g(x)=x, where we seek, x, that satisfies this equation.
f(x)=x
g(x)
April 20, 2023 OSU/CIS 541 70
Fixed-Point Iteration
• Newton’s iteration and the Secant method are of course in this form.
• In the limit, f(xk)=0, hence xk+1=xk
April 20, 2023 OSU/CIS 541 71
Fixed-Point Iteration
• Only problem is that that assumes it converges.• The pretty fractal images you see basically encode
how many iterations it took to either converge (to some accuracy) or to diverge, using that point as the initial seed point of an iterative equation.
• The book also has an example where the roots converge to a finite set. By assigning different colors to each root, we can see to which point the initial seed point converged.
April 20, 2023 OSU/CIS 541 72
Fractals
• Images result when we deal with 2-dimensions.
• Such as complex numbers.
• Color indicates how quickly it converges or diverges.
April 20, 2023 OSU/CIS 541 73
Fixed-Point Iteration
• More on this when we look at iterative solutions for linear systems (matrices).