TWO METHODS FOR FINDING ROOTS OF EQUATIONS Closed (Bracketing) Methods Open Methods
Motivation: i In engineering applications, it is often necessary to determine the rootofan of equation when a formula does not exist to yield an explicit solution. Example: Determine the formula for x, given a quadratic equation, 2 ax bx c + + = 0 Here there is a formula that can be derived for the explicit value of x, x = ± 2 b b 4ac 2a
Often, however, the unknown cannot be evaluated explicitly. We say that variable is implicit in the equation and that the equation is transcendental. Example: cos x x= 0 also stated as f ( x) = cos x x Note that we cannot rearrange this equation to obtain an explicit expression for x.
How can we determine the value of x that will satisfy the transcendental equation? Numerical procedures can be used to determine the roots of a transcendental equation. Two categories of numerical methods for determining the roots : Bracketing methods. Open methods.
Bracketing Methods f(x) x x l x r Strategy 1 Choose a range of x x < x < x that contains a root 1. Choose a range of x, x l < x < x r, that contains a root, f(x) = 0. Note that f(x l ) * f(x r ) < 0.
2. Divide the range into two ranges. f(x) x l x m x r x 3. Test one of the ranges for the existence of a root, f x f x ( ) ( )? <?0 l m
f(x) x l x m x r x 4. If f(x l ) * f(x m ) > 0 (as it is in the figure above), the root lies in the range, x m < x < x r, and x l becomes x m ; otherwise, the root lies in the range, x m < x < x r, and x r becomes x m. 5. Repeat procedures 2 through 4 until x r x <ε x = l ( x + x ) l r 2
Example Problem Find the root of f ( ) sin Solution: f x = x x In order to get a rough estimate of the location of the root, plot x versus f(x). >> x = linspace(0,1); >> fx = sin(sqrt(x)) x; >> plot(x,fx) >> grid on >> From the plot, we can see that the root lies in the range, 0.5 < x < 1.0
Bisection method Divide the selected range into two equal sized ranges, x m = (0.5 + 1.0)/ 2 = 0.75 Test the left range for a root, ( ) ( ) ( ) l ( ) f ( ) i ( ) m f x = f 0.5 = sin 0.5 0.5 = 0.1496 f x = f 0.75 = sin 0.75 0.75 = 0.0118 f x f x ( ) ( ) = 0.0018 l m
Since the product is positive, ii the root does not lie in the left range; therefore, it lies in the right range and, x l = x m. The search range has been reduced to 0.75 < x < 1.0 Repeat the procedure using MATLAB tillthe the range has been narrowed to less than 0.001. >> f = @(x) sin(sqrt(x)) x; >> f(xl)*f(xm) f(xm) ans = 8.2502e 004 >> xl = 0.75; >> xr = 1.00; >> xm = (xl + xr)/2; >> f = @(x) sin(sqrt(x)) x; >> f(xl)*f(xm) f(xm) ans = 8.2502e 004 >> xr = xm; >> xm = (xl + xr)/2; >> f(xl)*f(xm) ans = 3.3294e 004 >> xr = xm; >> xm = (xl + xr)/2; >> f(xl)*f(xm) ans = 9.4581e 005 >> xroot = (xl+xm)/2 xroot = 0.7656 Test the solution, >> % test >> f(xroot) ans = 0.0019
Programmingthebisection method The bisection method is seen to be an effective strategy to determine the root of an equation, but the hand calculations to compute the answer are time consuming. Let s see how to create an m file that will automatically do our work, and how call the m file to solve the problem.
function [xr, ea, iter] = divide_and_conquer(f, xl, xu, tol) ea = 100; iter = 0; fxl = f(xl); fxu = f(xu); while ea > tol iter = iter + 1; xr = (xl+xu)/2; % bisection method %xr = xu (fxu*(xl xu)) ( / (fxl fxu); % false position method if f(xr)*f(xu)< 0 % conditional statement xl = xr; fxl = f(xl); else xu = xr; fxu = f(xu); end if iter > 100 % safety valve in case no root exists at all ea = 1; else ea = abs((xu xl)/xr); end end
Launch the routine from MATLAB >> f = @(x) sin(sqrt(x)) x; >> xl = 0.5; >> xu = 1.0; >> tol = 0.0000001; >> [xr, ea, iter] = divide_and_conquer(f, xl, xu, tol) xr = 0.7686 ea = 7.7545e 008 iter = 23 >> % test >> f(xr) ans = 3.4000e 008
False position method Another bracketing method is known as the false position method in which the bisection step, xr = (xl+xu)/2; % bisection method, is replaced by an interpolation, xr = xu (fxu*(xl xu)) / (fxl fxu); % false position method xl xm xu
Modify the code using the false position method and solve the root finding problem. %xr = (xl+xu)/2; % bisection method xr = xu (fxu*(xl xu)) / (fxl fxu); % false position method >> f = @(x) sin(sqrt(x)) x; >> xl = 0.5; >> xu = 1.0; 10; >> tol = 0.0000001; >> [xr, ea, iter] = divide_and_conquer(f, xl, xu, tol) xr = 0.7686 ea = 2.8888e 016 iter = 15 Note that only 15 iterations were needed vs. 23.
Open Methods Newton Raphson (Newton s) Method Derivation using Taylor s series [ n ( ) ] ( ) ( ) ( ) n df xi df xi xi+ 1 xi f ( xi+ 1) = f ( xi) + xi+ 1 xi +... + +... [ n dx ] dx n! Truncate the series, df ( x ) ( ) ( ) + i ( ) f x + 1 f x x + 1 x dx i i i i
Recall that we are looking for x i+1 such that f(x i+1 ) = 0, so 0 ( xi ) ( ) df x f ( x ) + i x + 1 x dx i i i Rearrange to get an expression for x i+1, ( i) ( ) i 1 i ; + i i ( ) f x df x x x where f ( x ) = f x dx i Which is known as Newton s method.
Example Problem Find the root of ( ) sin ( ) f x = x x cos( x ) First derivative of f: f ( x ) = 1 2 x Newton s method becomes: x i+ 1 ( x ) i ( x ) i sin xi = xi cos 2 xi 1
Iteratively ti update dt x i using MATLAB >> f = @(x) sin(sqrt(x)) x; >> fprime = @(x)cos(sqrt(x))./(2*sqrt(x)) () (q ())/( q ()) 1; x = 0.5000; >> x = x f(x)/fprime(x) x = 0.8236 >> x = x f(x)/fprime(x) x = 0.7697 >> x = x f(x)/fprime(x) x = 0.7686 Note that finding the root to 4 places only took 3 iterations.
Secant tmthd Method Another open method to be used when f (x) is difficult or impossible to determine. Derivation Approximate f (x) using a finite difference, ( ) ( ) df f x i f x f ( x) = dx x x i i 1 i 1 Substitute into Newton s method, x i+ 1 f ( xi)( xi xi 1) xi f ( x ) f ( x ) f xi f xi 1
Example Problem Using the secant method, determine the root of, With MATLAB ( ) sin ( ) f x = x x >> f = @(x) sin(sqrt(x)) x; >> x0 =.5; >> x1 = 1.0; >> x2 = x1 f(x1)*(x1 x0)/(f(x1) f(x0)) >> x0 = x1; >> x1 = x2; >> x2 = x1 f(x1)*(x1 x0)/(f(x1) f(x0)) x2 = 0.7667 x2 = 0.7428 >> x0 = x1; >> x1 = x2; >> x2 = x1 f(x1)*(x1 x0)/(f(x1) f(x0)) x2 = 0.7687
Another Example Problem (from Cengal and Boles, example 16.4) Determine x: x 5 16.461 = 1 2 13.5 x 2 ( 3 x)( 2.5 x 2) 1 2 f ( x) x 5 = 16.461 1 2 x x 13.5 x 2 ( 3 x )( 2.5 x 2 ) 1 2 Now, find x such that f(x) = 0.
Using MATLAB Plot x vs. f(x) >> x = linspace(0,2.9); >> f = @(x)16.461 (x./((3 x).*(2.5 x/2).^.5))./(5./(13.5 x/2)).^.5; >> fx = f(x); >> grid on >> plot(x,fx) >> hold on >> x = linspace(3.1,4); >> fx = f(x); >> plot(x,fx) >> grid on
Secant Method >> x0 = 1.5; >> x1 = 2.0; 20; >> x2 = x1 f(x1)*(x1 x0)/(f(x1) f(x0)) x2 = 7.0474 >> x0 = x1; >> x1 = x2; >> x2 = x1 f(x1)*(x1 x0)/(f(x1) f(x0)) x2 = Note that for this case, the secant is clearly diverging, and will never reach the correct solution Motto of the story: 12.3827 13.5411i Not every numerical method will work with every problem.
MATLAB function for finding roots, fzero FromMATLAB help facility fzero Find zero of a function of one variable Syntax x = fzero(fun,x0) MATLAB example >> f = @(x) 16.461 (x/((3 x)*(2.5 x/2)^.5))/(5/(13.5 x/2))^.5; >> x0 = 1; >> x = fzero(f,x0) x = 2.7541
MATLAB function for finding roots of polynomials, roots Consider an n th degree polynomial n n 1 1 cx 1 + cx 2 +... + cx n + c n + 1 = 0 TheMATLAB function, roots, will factor the polynomial (find the zeros) = [ ] c c c c... c n c n+ r 1 2 3 1 ( ) = roots c
Note that r is a row vector of length n, such that, Example ( x r )( x r )( x r ) ( x r )( x r )... = 0 1 2 3 n 1 4 3 2 x x x x 23 + 185 601 + 630 = 0 >> p = [1 23 185 601 630] p = 1 23 185 601 630 >> r = roots(p) r = 9.0000 7.0000 5.0000 2.0000 n
Also note >> r = [2 5 7 9]; >> p = poly(r) p = 1 23 185 601 630 Which h relates the factored version to the polynomial. l ( )( )( )( ) 4 3 2 x 2 x 5 x 7 x 9 = x 23x + 185x 601x+ 630 = 0