Docsity

Numerical Methods, Numerical Methods, Numerical Methods, Exercises of Computer Science

Toggle sidebar
bg1
Locating roots for nonlinear equations
KEY WORDS. nonlinear equations, iterative method, bisection method,
Newton’s method, secant method.
GOAL.
•Introduce nonlinear equation.
•Learn about iterative methods for solving nonlinear equations
1 A Practical Problem: Floating Spherical Ball.
According to Archimedes, if a solid that is lighter than a fluid is placed in the
fluid, the solid will be immersed to such a depth that the weight of the solid is
equal to the weight of the displaced fluid. For example, a spherical ball of unit
radius will float in water at a depth H (which is the distance from the bottom
of the ball to the water line) determined by the density of the ball ρb, assuming
that the density of the water ρw= 1. The volume of the submerged segment of
the sphere is
V=ZH
0
πr2(h)dh =ZH
0
π(1 −(1 −h)2)dh = (H2−H3
3) (1)
To find the depth at which the ball floats, we must solve the equation that states
that the water density times the volume of the submerged segment is equal to
the ball density ρbtimes the volume of the entire sphere, i.e.,
ρw×V=ρb×4π
3,(2)
which simplifies to
H3−3H2+ 4ρb= 0 (3)
The depth Hof physical interest lies between 0 and 2 since the ball is of unit
radius and His measured upwards from the bottom of the ball. The question
here is how to find this depth H.
Exercise 1.1. Assume that ρb= 1/3, by using the Intermedia Value Theorem,
show that there exists a root H0, s.t. H3
0−3H2
0+ 4ρb= 0.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Numerical Methods, Numerical Methods, Numerical Methods and more Exercises Computer Science in PDF only on Docsity!

Locating roots for nonlinear equations

KEY WORDS. nonlinear equations, iterative method, bisection method, Newton’s method, secant method.

GOAL.

  • Introduce nonlinear equation.
  • Learn about iterative methods for solving nonlinear equations

1 A Practical Problem: Floating Spherical Ball.

According to Archimedes, if a solid that is lighter than a fluid is placed in the fluid, the solid will be immersed to such a depth that the weight of the solid is equal to the weight of the displaced fluid. For example, a spherical ball of unit radius will float in water at a depth H (which is the distance from the bottom of the ball to the water line) determined by the density of the ball ρb, assuming that the density of the water ρw = 1. The volume of the submerged segment of the sphere is

V =

∫ H

0

πr^2 (h)dh =

∫ H

0

π(1 − (1 − h)^2 )dh = (H^2 −

H^3

To find the depth at which the ball floats, we must solve the equation that states that the water density times the volume of the submerged segment is equal to the ball density ρb times the volume of the entire sphere, i.e.,

ρw × V = ρb ×

4 π 3

which simplifies to H^3 − 3 H^2 + 4ρb = 0 (3) The depth H of physical interest lies between 0 and 2 since the ball is of unit radius and H is measured upwards from the bottom of the ball. The question here is how to find this depth H.

Exercise 1.1. Assume that ρb = 1/3, by using the Intermedia Value Theorem, show that there exists a root H 0 , s.t. H^30 − 3 H 02 + 4ρb = 0.

2 Math Problem: Finding Roots

Definition 2.1. Let f be a real or complex valued function of a real or complex variable. A number r, real or complex, for which

f (r) = 0 (4)

is called a root of that equation or a zero of the function f.

For example, the function f (x) = 2x − 4 has the zero r = 2. The function g(x) = x^2 − 3 x + 2 has two roots r = 1, 2. For our floating sphere problem, the function f is given by f (H) = H^3 − 3 H^2 + 4ρb. Thus, our floating sphere problem is an example of the general root finding problem. Another typical root finding problem is the problem of finding the points of intersection of two curves. Let y = f (x) and y = g(x) be two continuous real- valued functions. Geometrically, each function is a curve in the (x, y) plane. We wish to find a point of intersection of these two curves. In other words, we need to find an r such that f (r) = g(r), (5)

or f (r) − g(r) = 0. (6)

Therefore, a point of intersection of the curves y = f (x) and y = g(x) is a zero of the function h(x) = f (x) − g(x).

A first and natural question to ask about locating roots for nonlinear equa- tions is

  • Does there ever exist a root?
  • If yes, how many roots does the equation have?

The following Intermediate-Value Theorem only partially answers the ques- tions.

Theorem 2.2. (Intermediate-Value Theorem) Let f (x) be a continuous func- tion on a close interval [a, b]. If

f (a)f (b) < 0 , (7)

then there exists an r ∈ (a, b), s.t. f (r) = 0.

Remark 2.3. In general, the existence and the number of roots for nonlinear equations is an open question. The Intermedia-Value Theorem is a sufficient condition for the existence of a root, but not necessary condition.

Exercise 2.4. Could you think of an example, when the assumption of Intermedia- Value Theorem is not satisfied, but the existence of root is still valid?

Then the function y = f (x) must intersect the x-axis. In other words, there must be a point r ∈ [a, b] such that f (r) = 0. Which point of [a, b] is the root? A good guess would be the mid-point c 0 = (a + b)/2. With this guess, we compute the value of f at the mid-point c 0. There are two possible cases:

  • f (c 0 ) = 0: This is fortuitous - we have found the zero, namely, c 0.
  • f (c 0 ) 6 = 0: In this case, we must have either f (a)f (c 0 ) < 0 or f (c 0 )f (b) <

In the second case, we conclude that there must be a zero in either [a, c 0 ] or [c 0 , b]. In either case, we are left with a half-sized bracketing interval. We repeat the same procedure to come up with an even smaller interval that brackets a zero of f , and denote by c 1 the mid-point of this new subinterval. Repeated use of this procedure will produce a sequence of approximations

c 0 , c 1 , c 2 , · · · (11)

which converges to a zero of the function f; that is,

lim n→∞ cn = r (12)

The following is a segment of MATLAB code that implements the bisection method in which the halving process can continue until the current interval is shorter than a designated positive tolerance delta.

function root=BisectionM(f, a, b, delta, nmax) fa = f(a); fb = f(b); if fafb > 0 disp(’Initial interval does not bracket a root’) return end itcount=0; while (abs(b-a) > delta & itcount<nmax) itcount=itcount+1; c=(a+b)/2; fc=f(c); if fafc<= % There is a root in the interval [a,c] b=c; fb=fc; % Otherwise, there is a root in the interval [c,b] else a=c; fa=fc; end

end root=(a+b)/2; disp(sprintf(’itcount = %i’,itcount))

EXAMPLE 1. Find the largest root of

f (x) ≡ x^6 − x − 1 = 0

accurate to within delta = 0.001 using no more than 15 iterations. Please check that this root lies in the interval [1, 2]. Then we may choose a = 1 and b = 2, and obtain the following.

f=inline(’x^6-x-1’)

f =

Inline function: f(x) = x^6-x-

root = BisectionM(f,1.,2.,0.001,15) itcount = 10

root =

It should be noted that the time required by a root-finder like BisectionM() is proportional to the number of function evaluations. The arithmetic that takes place outside of the function evaluations is typically insignificant.

Exercise 3.3. Use BisectionM() or your own code to solve the floating sphere problem (3) in which the sphere’s density is 1/3.

3.1.2 The convergence of the bisection method

Consider the bracket [an, bn] at the iterative step n. Since [an, bn] was obtained from [an− 1 , bn− 1 ] by the halving procedure, then with cn = (an + bn)/2 we have

|cn − r| ≤

|bn − an| =

|bn− 1 − an− 1 | = · · · =

2 n+^

(b − a), (13)

where (b−a) is the length of initial interval and r is one of the roots bracketed in [an, bn]. In summary, if the bisection method is applied to a continuous function f on an interval [a, b], where f (a)f (b) < 0, then, after n steps, an approximate root will have been computed with error at most (^2) n^1 +1 (b − a).

Exercise 3.4. Please draw a picture to illustrate Newton’s method. Be sure to carefully label your picture.

3.2.2 Algebraic derivation of Newton’s method

Newtons method can also be derived in the following way using the Taylor series of a function of one variable. Let xn be the current approximation to a zero r of the function f (x). The error, denoted by h, is given by

h = r − xn, (20)

from which it follows that r = xn + h. (21)

Since r is a zero of f (x), we have

f (xn + h) = 0.

Using Taylor series at xn, we arrive at the following,

f (xn) + hf ′(xn) +

h^2 2!

f ′′(xn) + O(h^3 ) = 0. (22)

If equation (22) is viewed as an equation in terms of the variable h, by solving for h from this equation we would obtain a true root of the function f. Un- fortunately, this is impossible in practice. Therefore, we take only the first two terms of the series, which yields a linear equation to solve for h. This linear equation is f (xn) + hf ′(xn) = 0, (23)

with solution

h = − f (xn) f ′(xn)

Since we truncated the series in h, this is generally different from the true solution. However, the corrected solution

xn+1 = xn + h = xn −

f (xn) f ′(xn)

provides a better approximation to the solution of f (x) = 0. This is the New- ton’s method.

3.2.3 Finding the

2 by Newton’s method

Recall that the problem of finding the square root of 2 can be expressed as a solution of x^2 − 2 = 0, (26)

which is equivalent of locating zeros of f (x) = x^2 − 2. Since f ′(x) = 2x, the Newton’s method is given by

xn+1 = xn −

f (xn) f ′(xn)

= xn −

x^2 n − 2 2 xn

(xn +

xn

which is equation (37). The following is a segment of MATLAB code that implements the Newton’s method in which the iterative process can continue until the two successive approximations are close enough (tolerance delta).

function root=NewtonM(f, fderiv, x0, delta, nmax) error =1; itcount=0; while abs(error) > delta & itcount<nmax fx=f(x0); dfx=fderiv(x0); if dfx == 0; disp(’The derivative is zero. Stop.’) return end x1=x0-fx/dfx; error = x1-x0; x0=x1; itcount=itcount+1; end if itcount >=nmax disp(’Maximum number of iterations exceeded’) root=x1; else disp(sprintf(’itcount = %i’,itcount)) root=x1; end

Using the initial guess x0 as the starting point, we carry out a maximum of nmax iterations of Newton’s method. Procedures must be supplied for the external functions f and f ′. The parameter delta is used to control the convergence and is related to the accuracy desired or to the machine precision available.

EXAMPLE 2. Solve the problem in Example 1 using Newton’s method with x 0 = 1.

f=inline(’x^6-x-1’)

f =

Combining equation (28) and (30) gives,

en+1 = −

e^2 n f ′′(ξn) f ′(xn)

Assume that f ′(r) 6 = 0, that is, r is a simple zero of f. Then, there exists a neighborhood of r in which f ′^ is nonzero. In other words, there exists a δ 0 > 0 such that |f ′(x)| ≥ c 0 > 0 , whenever |x − r| ≤ δ 0. (32)

Also assume that the second order derivative is bounded in this δ 0 neighborhood. Thus there is a constant M such that

f ′′(η) f ′(x)

| ≤ M, (33)

for any η, x ∈ (r − δ 0 , r + δ 0 ). Thus from this bound and equation (31), we have

|en+1| ≤ M |en|^2 , (34)

which is what we would expect if Newtons method converges quadratically. This result can be sued to give a mathematical proof of the convergence of Newtons method. Specically, we have the following theorem.

Theorem 3.6. If f , f ′^ and f ′′^ are continuous in a neighborhood of a simple root r of f , then there is a δ > 0 such that, if the initial guess x 0 satises |r − x 0 | ≤ δ, then all subsequent approximations xn satisfy the same inequality, and converge to r quadratically.

In short, Newtons method always converges when the initial guess is ‘suffi- ciently’ close to the root. The popularity of Newtons method is essentially based upon the fact that it converges quadratically. This is sometimes described by saying that the number of accurate digits in the answer doubles with each iteration. Quadratic conver- gence is more properly described as an asymptotic property, however, so that, in general, we cannot expect a doubling in the number of accurate digits on early iterations.

3.2.5 Choice of initial guess x 0.

When using Newtons method, it can be difficult to decide on a suitable initial guess. If x 0 is not close enough to the root, Newtons method will either diverge or converge to another root. It is sometimes helpful to have some insight into the shape of the graph of the function to guide the selection of an initial guess. Often the bisection method is used initially to obtain a suitable starting point, and Newtons method is then used to improve the precision.

4 The Secant Method

In comparison to Newton’s method, the secant method uses the intersection of a secant line with the x-axis to update the approximation xn. The correspond- ing iterative scheme is derived as follows. Assume that xn− 1 and xn are two approximations to the exact root r. We draw a secant line using the points (xn− 1 , f (xn− 1 )) and (xn, f (xn)) whose equation is given by

y − f (xn) = m(x − xn), (35)

where

m = f (xn) − f (xn− 1 ) xn − xn− 1

is the slope of the straight line. The point of intersection of the line (35) with the x-axis, denoted by xn+1, is given by

−f (xn) = m(xn+1 − xn),

or

xn+1 = xn −

f (xn) m

Substituting for m from (36)

xn+1 = xn −

f (xn)(xn − xn− 1 ) f (xn) − f (xn− 1 )

Note that the secant method needs two initial points to start the iteration, while Newton’s method needs only one. In addition, it can be proved that New- ton’s method converges more rapidly than the secant method. But the secant method does not involve the derivative of the function. In some applications when the derivative may not be available or is expensive to evaluate, the secant method is a viable alternative to Newton’s method.

Exercise 4.1. Using the secant method, solve the floating sphere problem (3) in which the sphere’s density is 1/3. Be sure to clearly indicate your choice of initial guess(es). Compare your results with those obtained using Newton’s method and the bisection method. In addition to your results, please provide your code, in Matlab, clearly commented.

5 Reading Assignments/References:

  1. About bisection method: follow the link http://en.wikipedia.org/wiki/ Bisection_method.
  2. About Newton’s method: follow the link http://en.wikipedia.org/ wiki/Newton’s_method.
  3. About secant method: follow the link http://en.wikipedia.org/wiki/ Secant_Method.