Anda di halaman 1dari 33

Numerical Methods for Civil Engineers

Lecture 5

Roots of Equations
- Graphical Methods - Fixed-Point Iteration - Bisection Method - Newtons Method - Secant Method - Roots of Polynomials

f(x) = 0

By Mongkol JIRAVACHARADET School of Civil Engineering Suranaree University of Technology

Bracketing
Coarse level search for roots over large interval 1) Subdividing large interval into smaller subinterval 2) Examine sign at the end of each subinterval f (x) + + + + + +

+++++++++

x1

----------

++++

x2

NUMCOM05-02

Bracketing Algorithm
Given: f (x), xmin, xmax, n % Size of bracket interval dx = (xmax - xmin)/n xa = xmin % Initialize left side of test bracket i=0 % Initialize counter while i < n i=i+1 xb = xa + dx if f (xa) and f (xb) have different sign save [ xa , xb ] for further root finding end xa = xb end
NUMCOM05-03

Detecting Sign Change f (xa) & f (xb)


1st Solution: f (xa) f (xb) < 0 ?

>> format long e >> fa = 1e-120; fb = -2e-300; >> fa * fb ans = 0

< realmin

2nd Solution: Use sign function


>> fa = 1e-120; fb = -2e-300; >> sign(fa) ~= sign(fb) ans = 1 Homework:

return true value = 1

Write MATLAB bracketing algorithm of the function f (x) = x - x1/3 - 2 = 0 for interval 0.1
NUMCOM05-04

Graphical Methods
Plot the function and observe where it crosses x axis

667.38 f ( x) = (1 e0.147 x ) 40 x
x 4 8 12 16 20 f(x) 34.115 17.653 6.067 -2.269 -8.401

f(x)
40

20

0.000
0 -10 4 8 12

16 20 x

Observe: x 15

f (15) = -0.4133
NUMCOM05-05

Interpolation
Assume as linear function for short interval

6.607 12 16 2.269

Interpolation: x = 12 +

6.607 (16 12) = 14.977 6.607 (2.269)

f (14.977) = -0.3691

f (15) = -0. 4133, f (14) = 1.582 2nd Interpolation: x = 14 +

1.582 (15 14) = 14.793 1.582 (0.4133)

f (14.793) = -0.013
NUMCOM05-06

Fixed-Point Iteration
1) Change f (x) = 0 to x = g(x)
x2 2 x + 1 = 0

x2 + 1 x= 2

2) Predict new xi+1 as a function of old xi

xi+1 = g (xi)
3) Iterative until satisfy
xi +1 xi a = 100% xi +1
NUMCOM05-07

Example: Finding root of f (x) = x - e-x = 0 i


0 1 2 3 4 5 6 7 8 9 10

xi +1 = e xi

xi
0 1 0.3679 0.6922 0.5005 0.6062 0.5454 0.5796 0.5601 0.5711 0.5649

a (%)
100 171.8 46.9 38.3 17.4 11.2 5.90 3.48 1.93 1.11
Approximated Root

True value = 0.5671


0.8

0.6

0.4

0.2

0 0

4 5 6 7 8 9 Number of Iteration

10 11 12

>> 0 >> exp(-ans)


NUMCOM05-08

Two-Curve Graphical Method


1 0.8 0.6 f(x) = exp(-x) - x 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 0.2 0.4 x 0.6 0.8 1

f ( x) = e x f ( x) = e x x
0.8

Root

0.6

0.4

f ( x) = x

0.2

0 0

0.2

0.4 x

0.6

0.8

>> x = 0 : 0.1 : 1; >> plot(x, exp(-x)-x)

>> x = 0 : 0.1 : 1; >> plot(x, x, x, exp(-x))

NUMCOM05-09

Convergence
1

0.8

y2 = g(x)
0.6922 0.6062

y1 = x

0.6

y
0.4 0.2

0.5005 0.3679

0 0 0.2 0.4 0.6 0.8 1

x
NUMCOM05-10

Divergence
1

0.8

y2 = g(x)
0.6

y1 = x

y
0.4 0.2

0 0 0.2 0.4

x0
0.6 0.8 1

x
NUMCOM05-11

Bisection Method
Repeatedly halve interval while bracketing root f (x) +

xb xa xm
1) Choose interval [ xa , xb ] which has sign-change 2) Compute midpoint of interval xm = ( xa + xb ) / 2 3) Select subinterval which has sign-change and repeat 2)

NUMCOM05-12

Example: Use bisection method find the root of equation

667.38 f ( x) = (1 e0.147 x ) 40 x
Estimate root at midpoint:

True value of the root: 14.7802 f(x)


40

12 + 16 xr = = 14 2

t =

14.7802 14 100% = 5.279% 14.7802

20

Initial interval [ 12 , 16 ] 16 4 8 12 20 x

Select interval with sign-change:

f (14) = 1.582

+ [ 14 , 16 ]

0 -10

Next estimation: xr = (14+16)/2 = 15

15 14 a = 100% = 6.667% 14 NUMCOM05-13

Termination Criteria and Error Estimates Stop computation when a < s , Ex. s = 0.5% Iteration
1 2 3 4 5 6

xa
12 14 14 14.5 14.75 14.75

xb
16 16 15 15 15 14.875

xr
14 15 14.5 14.75 14.875 14.8125

a(%)
6.667 3.448 1.695 0.840 0.422

t(%)
5.279 1.487 1.896 0.204 0.641 0.219

< s = 0.5%

NUMCOM05-14

Bisection Algorithm
initialize: a = . . . , b = . . . for k = 1, 2, . . . xm = a + (b-a)/2 if sign(f (xm)) = sign(f (a)) a = xm else b = xm end if converged, stop end less susceptible to roundoff error than (a+b)/2

NUMCOM05-15

Example: Apply Bisection to x - x1/3 - 2 = 0


bisect.m function xm = bisect(a, b, n) if nargin<3, n=15; end % Default number of iterations fa = a - a^(1/3) - 2; % Initial value of f(a) and f(b) fb = b - b^(1/3) - 2; fprintf( k a xmid b f(xmid)\n); for k = 1:n xm = a + 0.5*(b-a); % Computing midpoint fm = xm - xm^(1/3) - 2; % Function value at midpoint fprintf(%3d %12.8f %12.8f %12.8f %12.3e\n, k, a, xm, b, fm); if sign(fm) == sign(fa) % Root lies in [xm, b] replace a a = xm; fa = fm; else % Root lies in [xm, a] replace b b = xm; fb = fm; end end
NUMCOM05-16

Newtons Method
First-order Taylor series:

f ( xi +1 ) f ( xi ) + f ( xi )( xi +1 xi )

f ( xi ) Set f ( xi +1 ) = 0 xi +1 = xi f ( xi )
f(x) slope = f(x) f(xi) Root xi+1 xi x

f ( xi ) xi xi +1 = f ( xi )

NUMCOM05-17

Approximation Sequence of Newtons Method

f(x) f(x1)

x2 x3 f(x2) x1 x

NUMCOM05-18

Failure of Newtons Method


Case 1: Inflection point in vicinity of root

f(x)

x2

x1

x3 x

NUMCOM05-19

Failure of Newtons Method


Case 2: Oscillate around local maximum or minimum

f(x)

NUMCOM05-20

Failure of Newtons Method


Case 3: Jump away for several roots

f(x)

NUMCOM05-21

Failure of Newtons Method


Case 4: Disaster from zero slope

f(x)

NUMCOM05-22

Example: Apply Newtons Method to x - x1/3 - 2 = 0


newton.m function x = newton(x0, n) if nargin<2, n=5; end % Default number of iterations x = x0; % Initial guess fprintf( k f(x) dfdx x(k+1)\n); for k = 1:n f = x - x.^(1/3) - 2; % Function value dfdx = 1 - (1/3)*x.^(-2/3); % Derivative value x = x - f/dfdx; fprintf(%3d %12.3e %12.3e %18.15f\n, k-1, f, dfdx, x); end

NUMCOM05-23

Secant Method
for function whose derivatives are difficult to evaluate Derivative approximated by backward finite difference

f ( xi 1 ) f ( xi ) f ( xi ) xi 1 xi
f(xi)

f ( xi ) xi +1 = xi f ( xi )

f ( xi ) ( xi 1 xi ) xi +1 = xi f ( xi 1 ) f ( xi )

f(xi-1) xi-1 xi x
NUMCOM05-24

Example: Use secant method to estimate root of e-x - x = 0 Initial estimate x-1 = 0 and x0 = 1.0 True root = 0.56714329. . . First iteration: x-1 = 0 x0 = 1

f (x-1) = 1.00000 f (x0) = -0.63212

0.63212(0 1) x1 = 1 = 0.61270 1 (0.63212)

t = 8.0%

NUMCOM05-25

Second iteration: x0 = 1 x1 = 0.61270


x2 = 0.61270

f (x0) = -0.63212 f (x1) = -0.07081

0.07081(1 0.61270) = 0.56384 0.63212 (0.07081)

t = 0.58%

Third iteration: x1 = 0.61270 x2 = 0.56384

f (x1) = -0.07081 f (x2) = 0.00518

0.00518(0.61270 0.56384) x3 = 0.56384 = 0.56717 0.07081 (0.00518) t = 0.0048%


NUMCOM05-26

Roots of Polynomials
f n ( x) = a0 + a1 x + a2 x 2 +
where n = order of polynomial as = constant coefficients Roots of polynomials: (1) nth-order equation has n real or complex roots (2) If n is odd, at least one root is real. (3) Complex roots exist with conjugate pairs ( + i and i)
NUMCOM05-27

+ an x n

Ordinary Differential Equation (ODE)

d2y dy a2 2 + a1 + a0 y = F (t ) dt dt
General Solution:

d2y dy a2 2 + a1 + a0 y = 0 dt dt

Set y = e rt a2 r 2 e rt + a1re rt + a0 e rt = 0 Canceling exponents a2 r 2 + a1r + a0 = 0


Characteristic equation

NUMCOM05-28

Characteristic Equation

a2 r + a1r + a0 = 0
2

Roots of equation by quadratic formula

r1 a1 a12 4a2 a0 = r2 2 a2
(1) If a12 - 4a2a0 > 0, roots are real y Overdamped case

EIGENVALUES

y = c1e + c2 e
r1t

r2t

t
NUMCOM05-29

(2) If a12 - 4a2a0 = 0, single root is real y Critically damped case

y = ( c1 + c2t ) e

rt

t (3) If a12 - 4a2a0 < 0, two roots are complex conjugate y Underdamped case

r1 r2

= i

y = c1et cos t + c2 et sin t


NUMCOM05-30

f1 ( x) = x 2 3x + 2 r1 3 32 4(2) 2 = = r2 1 2 f 2 ( x) = x 2 10 x + 25 r1 10 102 4(25) 5 = = r2 5 2 f3 ( x) = x 2 17 x + 72.5 17 17 4(72.5) 8.5 + 0.5i = = r2 8.5 0.5i 2 r1


2

Distinct real roots

Repeated real roots

Complex roots

NUMCOM05-31

Roots of Polynomials
3 2.5 2 1.5 1 0.5 0 -0.5 -1 0 2 4 6 8 10

y = f(x)

distinct real roots

repeated real roots x

complex roots

NUMCOM05-32

MATLABs roots Function


Compute eigenvalues of companion matrix 4th-order polynomial: c5 4 + c4 3 + c3 2 + c2 + c1 = 0 Companion Matrix

c2 / c1 1 A= 0 0

c3 / c1 0 1 0

c4 / c1 0 0 1

c5 / c1 0 0 0

Eigenvalue problem

A =
root([1 -3 2]) root([1 -10 25]) root([1 -17 72.5])
NUMCOM05-33

f1 ( x) = x 2 3 x + 2 f 2 ( x) = x 2 10 x + 25 f3 ( x) = x 2 17 x + 72.5

Anda mungkin juga menyukai