RootFinding Algorithms PowerPoint PPT Presentation

presentation player overlay
1 / 18
About This Presentation
Transcript and Presenter's Notes

Title: RootFinding Algorithms


1
Root-Finding Algorithms
2
Root-Finding Problem
  • A common engineering problem is trying to find
    the value of x for which f(x) 0. This is a
    root-finding problem.
  • Example
  • f(x) x3 27
  • To find f(x) 0
  • x3 27 0
  • x3 27
  • x 3
  • Often it is difficult or impossible to solve a
    root-finding problem analytically (i.e.
    rearranging the equation to produce x
    constant).
  • Example
  • f(x) 3x5 5x4 3x3 - 6x2 x - 3
  • In these cases it is useful to determine the root
    using a numerical method (iterative
    approximations).

3
Fixed point iteration
  • We have previously showed that we can find the
    fixed point x g(x) using the fixed point
    iteration algorithm where pn g(pn-1).
  • Given a function f(x) where we are trying to
    determine the root of f(x), we can let g(x)
    f(x) x, determine the fixed point of g(x), and
    this would be the root of f(x).
  • Note that this algorithm only works if g(x) ?
    K lt 1, for some constant K. In many cases this
    criteria will not be met and therefore we require
    an alternative algorithm to determine the root.

4
Exhaustive Search Method
  • Suppose we are given a function f(x) and that a
    root exists in the interval a, b. We can find
    the value of x by iteratively trying the values
    x0 a, x1 a ? , x2 a 2?, , xn a n?,
    and stopping when f(xn) is sufficiently close
    to 0. If the step value ? is sufficiently small
    we can obtain an accurate result but this could
    take an extremely long time. For example, if the
    interval is 0,10 and the step size is ?
    0.001, it will take on average 10,000 guesses.
  • In addition to the inefficiency of this approach,
    if f(x) is a steep function, this approach may
    not produce an accurate result.
  • Example
  • Suppose we are given the function f(x)
    (1-e-(x-1.0001)/1e-2). This function has a root
    at 1.0001. Suppose we are given an interval
    0.9,1.1 and use a step size of ? 0.001. The
    exhaustive search method will test f(1.000)
    -0.01 and f(1.001) 0.086, neither of which are
    that close to f(x) 0.

5
Bisection Method
  • The exhaustive method is not an efficient
    algorithm. We can imagine a more sophisticated
    approach if we think of how we would approach a
    high-low game.
  • Suppose someone was thinking of an integer number
    between 0 and 100 and you were to guess the
    number. Your only feedback was whether your guess
    was too high or too low.
  • The exhaustive search would simply guess the
    numbers in sequence (i.e. 1, 2, 3, 4, 5, ).
  • The better approach is to guess 50. If the number
    is too high then you know the number is between 1
    and 50. If the number is too low then you know
    the number is between 50 and 100. Of course, if
    the number is 50, you guessed correctly and you
    are done.

6
Bisection Method
  • The bisection method is much more efficient than
    the exhaustive search method for finding roots.
    This method organizes the guesses. The size of
    the interval that the roots is located in, is
    reduced by a factor of two with each guess.
  • We start with an initial interval a,b where
    f(a) and f(b) have opposite signs (i.e. one is
    positively valued and one is negatively valued).
    This implies that f(x) must cross the x-axis at
    some point in the interval a,b (see graph).

b
a
7
Bisection Method
  • The bisection method uses the midpoint c (a
    b)/2 as its initial guess. Three cases arise.
  • Case 1 f(c) 0
  • We are done because x c is the root.
  • Case 2 f(a) and f(c) have opposite signs
  • The root must lie in the interval a,c
  • Case 3 f(b) and f(c) have opposite signs
  • The root must lie in the interval c,b
  • If case 2 and 3 occurs, we are left with the same
    problem but the interval we are dealing with is
    half the size of the original. We repeat this
    process iteratively until we have reached the
    root, or are within an acceptable tolerance.

8
Bisection Method
Iteration 1
b0
c0
a0
b1
Iteration 2
a1
c1
b2
c2
Iteration 3
a2
9
Bisection Method
  • Since we know the root lies in the interval
    a,b, and we are guessing the root to be the
    midpoint, the difference between the true root r
    and the guess c must be less than half the
    interval size. That is,
  • error r c0 ? (b0 a0)/2 max(error)
  • In the second iteration, the size of the interval
    is halved, so the maximum error is also halved.
    That is,
  • error r c1 ? max(error) (b1 a1)/2
    (b0 a0)/22
  • Iteratively, using this argument
  • error r cn ? max(error) (bn an)/2
    (b0 a0)/2n1

10
Bisection Method
  • Despite a large number of iterations, we may
    never find the exact root (e.g. f(x) x - ?). To
    avoid running an infinite number of iterations we
    can terminate when any of these three conditions
    are satisfied
  • Put a limit on the number of iterations we run.
  • Put a tolerance limit on how close f(x) must be
    to zero that is, terminate the method when
    f(x) lt ?, where ? is the tolerance.
  • Terminate when the interval is sufficiently
    small that is, terminate the method when
    max(error) (bn an)/2 lt ?, where ? specifies
    what we consider sufficiently small. For example,
    to be accurate to 8 decimal places set ?
    0.5?10-8.
  • For the bisection method, condition 3 is often
    the best to use.

11
False Position Method
  • The false position method works in a similar
    fashion to the bisection method. We start with an
    initial interval a,b where f(a) and f(b) have
    opposite signs, which is the same as the
    bisection however, instead of choose our initial
    guess c as the midpoint of the interval, we join
    the point a,f(a) and b,f(b) with a straight
    line and choose c as the point where that
    straight line crosses the x-axis.

12
False Position Method
  • It can be shown that
  • The same three cases arise as in the bisection
    method.
  • Case 1 f(c) 0
  • We are done because x c is the root.
  • Case 2 f(a) and f(c) have opposite signs
  • The root must lie in the interval a,c
  • Case 3 f(b) and f(c) have opposite signs
  • The root must lie in the interval c,b
  • We repeat this process iteratively until we have
    reached the root, or are within an acceptable
    tolerance.

13
False Position Method
  • Although the interval where the root is a,b
    becomes iteratively smaller with the false
    position method, unlike the bisection method, the
    size of the interval does not necessarily
    converge to zero.
  • In the example shown below the smallest interval
    that the false position method will converge to
    is a0,r, where r is the root. Therefore, only
    the first two termination conditions (refer to
    slide 10) used in the bisection method are used
    for the false position method.

a0
c0
b0
c1
14
False Position Method
  • The false position method converges to the root
    faster than the bisection method.
  • In this course, you need only concern yourself in
    being able to implement the bisection method.

15
Square Root Example
  • Note there are some problems that can be
    rearranged into root-finding problems.
  • Example
  • Write a program that accepts a user input for a
    number and compute the square root using the
    bisection method to an accuracy of 4 decimal
    places (i.e. rearrage the square root into a root
    finding problem). The program should continue
    accepting user inputs until a negative number is
    entered.

16
Square Root Example
Rearranged into a root finding problem. That is,
finding the square root of y is equivalent to
finding the root of the equation f(x)
  • For values of y greater than 1.0 set low to 0 and
    high to x
  • For values of y less than 1.0 set low to 0 and
    high to 1.0
  • The square root of y is guaranteed to fall
    between these two values

17
Square Root Example
  • include ltiostreamgt
  • using namespace std
  • const double TOL 0.5e-4
  • double f(double x, double y)
  • int main()
  • double x, low, mid, high
  • while (true)
  • cout ltlt "Enter a number (negative number
    to quit) "
  • cin gtgt y
  • if (y lt 0.0)
  • break

18
  • low 0.0
  • if (y gt 1.0)
  • high y
  • else
  • high 1.0
  • mid (low high)/2.0
  • while (high - low gt 2.0TOL)
  • if (f(mid,y) 0.0)
  • break
  • if (f(mid,y)f(low,y) lt 0.0)
  • high mid
  • else
  • low mid
  • mid (low high)/2.0
Write a Comment
User Comments (0)
About PowerShow.com