You are on page 1of 22

Numerical Methods

Roots of Nonlinear Equations

Topic: Other Methods

Dr. Nasir M Mirza


Email: nasirmm@yahoo.com

Lectures Goals
Let us introduce methods for Solving Equations of one Variable by MATLAB

Example 1.
Find the roots of the polynomial f(x) = x5 -10x4 + 35x2 - 50x + 24 Solution: The roots are found with the following two statements. We have denoted the polynomial as p1, and the roots as roots_ p1.
p1=[1 10 35 50 24] % Specify the coefficients of p1(x) p1 = 1 -10 35 -50 24 roots_ p1=roots(p1) % Find the roots of p1(x)

roots_p1 = 4.0000 3.0000 2.0000 1.0000 We observe that MATLAB displays the polynomial coefficients as a row vector, and the roots as a column vector.

Example 1.2
Find the roots of the polynomial: p2(x)= x5 7x4 + 16x2 + 25x +52 Solution: There is no cube term; therefore, we must enter zero as its coefficient. The roots are found with the statements below where we have defined the polynomial as p2, and the roots of this polynomial as p2=[1 7 0 16 25 52]; roots(p2) % the result is following: 6.5014 2.7428 -1.5711 -0.3366 + 1.3202i -0.3366 - 1.3202i The result indicates that this polynomial has three real roots, and two complex roots. Of course, complex roots always occur in complex conjugate pairs.

Polynomial Construction from Known Roots


We can compute the coefficients of a polynomial from a given set of roots with the poly(r) function, where r is a row vector containing the roots. Example 1.3 It is known that the roots of a polynomial are 1, 2, 3, 4. Compute the coefficients of this polynomial. Solution: We first define a row vector, say, with the given roots as elements of this vector; then, we find the coefficients with the poly(r) function as shown below. r3=[1 2 3 4] % Specify the roots of the polynomial r3 = 1 2 3 4 poly(r3) % Find the polynomial coefficients 1 -10 35 -50 24 These are coefficients of the polynomial of Example 1.

Example 4.
It is known that the roots of a polynomial are -1, -2, -3, 4 + 5i, 4 5i . Find the coefficients of this polynomial. Solution: We form a row vector, say , with the given roots, and we find the polynomial coefficients with the poly(r) function as shown below. r4=[ 1 2 3 4+5j 45j ] r4 = Columns 1 through 4 -1.0000 -2.0000 -3.0000 -4.0000 + 5.0000i Column 5 -4.0000 - 5.0000i poly(r4) 1 14 100 340 499 246 Therefore, the polynomial is: x5 + 14x4 + 100x3 + 340x2 + 499x +246

Example 5
The polyval(p,x) function evaluates a polynomial at some specified value of the independent variable. Evaluate the polynomial: x6 3x5 + 5x3 4x2 + 3x + 2 at x = -3. Solution: p5=[1 3 0 5 4 3 2]; % These are the coefficients % The semicolon (;) after the right bracket suppresses the display of % the row vector that contains the coefficients.

polyval(p5, 3) % Evaluate p5 at x=3. % No semicolon is used here % because we want the answer to be displayed
1280

Other polynomial functions


Other MATLAB functions used with polynomials are the following: conv(a,b) multiplies two polynomials a and b [q,r]=deconv(c,d) divides polynomial c by polynomial d and displays the quotient q and remainder r. polyder(p) produces the coefficients of the derivative of a polynomial p.

Example 6
Let p1 = x5 3x4 + 5x2 + 7x + 9 p2 = 2x6 8x4 + 4x2 + 10x + 12 Compute the product with the conv(a,b) function. Solution: p1 = [1 3 0 5 7 9]; p2 = [2 0 8 0 4 10 12]; conv(p1, p2) 2 -6 -8 34 18 -24 -74 -88 78 166 174 108 Therefore, Product = 2x11 6x10 8x9 + 34x8 + 18x7 24x6 74x5 88x4 + 78x3 + 166x2 + 174x + 108

Example 7
Let p3 = x7 3x5 5x3 + 7x + 9 p4 = 2x6 8x5 + 4x2 + 10x + 12 Compute the quotient using the deconv(p,q) function. Solution: p3=[1 0 3 0 5 7 9]; p4=[2 8 0 0 4 10 12]; [q, r]=deconv(p3, p4) q= 0.5000 r = 0 4 -3 0 3 2 3 Therefore, the quotient and remainder are q(x)=0.5 r(x) = 4x5 3x4 + 3x2 +2x+3

Example 8
Let p5 = 2x6 8x4 + 4x2 +10x + 12 Compute the derivative using the polyder(p) function. Solution: p5=[2 0 8 0 4 10 12]; der_p5=polyder(p5) der_p5 = 12 0 -32 0 8 10

Therefore, derivative = 12x5 32x3 + 4x2 + 8x + 10

Rootsearch function
The function rootsearch looks for a zero of the function f (x) in the interval (a, b); The search starts at a and proceeds in steps dx toward b. Once a zero is detected, rootsearch returns its bounds (x1, x2) to the calling program. If a root was not detected, x1 = x2 = NaN is returned (in MATLAB NaN stands for not a number). After the first root (the root closest to a) has been bracketed, rootsearch can be called again with a replaced by x2 in order to find the next root. This can be repeated as long as rootsearch detects a root.

Rootsearch function in Matlab


function [x1, x2] = rootsearch(func, a, b, dx) % Incremental search for a root of f(x). % USAGE: [x1, x2] = rootsearch(func, a, b, dx) % INPUT: func = handle of function that returns f(x). % a, b = limits of search. % dx = search increment. % OUTPUT: x1,x2 = bounds on the smallest root in (a,b); % set to NaN if no root was detected x1 = a; f1 = feval(func,x1); x2 = a + dx; f2 = feval(func,x2); while f1*f2 > 0.0 if x1 >= b x1 = NaN; x2 = NaN; return end x1 = x2; f1 = f2; x2 = x1 + dx; f2 = feval(func,x2); end

Example 1
Use incremental search with x = 0.2 to bracket the smallest positive zero of f (x) = x3 10x2 + 5. Solution: We evaluate f (x) at intervals x = 0.2, staring at x = 0, until the function changes its sign (value of the function is of no interest to us; only its sign is relevant). This procedure yields the following results:
x 0.0 0.2 0.4 0.6 0.8 f (x ) 5.000 4.608 3.464 1.616 -0.888

From the sign change of the function we conclude that the smallest positive zero lies between x = 0.6 and x = 0.8. and

% Example 1 (root finding with bisection) a = 0.0; b = 0.80; dx = 0.02; [x1, x2] = rootsearch(@fex1,a,b,dx); function y = fex1(x) % Function used in Example 1 y = x.^3 - 10.0*x.^2 + 5.0;
Output is here.

x1 = 0.7200 x2 = 0.7400 >>

bisect function: for bisection method


This uses the bisection to find the root of f (x) = 0 that is known to lie in the interval (x1, x2). The number of bisections n required to reduce the interval to tol. The input argument filter controls the filtering of suspected singularities. By setting filter = 1, we force the routine to check whether the magnitude of f (x) decreases with each interval halving. If it does not, the root may not be a root at all, but a singularity, in which case root = NaN is returned. Since this feature is not always desirable, the default value is filter = 0.

bisect function in Matlab


function root = bisect(func,x1,x2,filter,tol) % Finds a bracketed zero of f(x) by bisection. % USAGE: root = bisect(func,x1,x2,filter,tol) % INPUT: % func = handle of function that returns f(x). % x1,x2 = limits on interval containing the root. % filter = singularity filter: 0 = off (default),1 = on. % tol = error tolerance (default is 1.0e4*eps). % OUTPUT: % root = zero of f(x), or NaN if singularity suspected. if nargin < 5; tol = 1.0e4*eps; end if nargin < 4; filter = 0; end f1 = feval(func,x1); if f1 == 0.0; root = x1; return; end f2 = feval(func,x2); if f2 == 0.0; root = x2; return; end if f1*f2 > 0; error(Root is not bracketed in (x1,x2)) end

bisect function in Matlab - continued


n = ceil(log(abs(x2 - x1)/tol)/log(2.0)); for i = 1:n x3 = 0.5*(x1 + x2); f3 = feval(func,x3); if(filter == 1) & (abs(f3) > abs(f1))... & (abs(f3) > abs(f2)) root = NaN; return end if f3 == 0.0 root = x3; return end if f2*f3 < 0.0 x1 = x3; f1 = f3; else x2 = x3; f2 = f3; end end root=(x1 + x2)/2;

EXAMPLE 2
Find all the zeroes of f(x) = x tan(x) in the interval (0, 20) by the method of bisection. Utilize the functions rootsearch and bisect. Solution: Note that tan x is singular and changes sign at x = /2, 3 /2, . . . . To prevent bisect function from mistaking these point for roots, we set filter = 1. The closeness of roots to the singularities is another potential problem that can be removed by using small x in rootsearch function. Choosing x = 0.01, we arrive at the following program:

Example 2
% all root finding with bisection a = 0.0; b = 20.0; dx = 0.01; nroots = 0; while 1 [x1,x2] = rootsearch(@fex4,a,b,dx); if isnan(x1) function y = fex4(x) break % Function used in Example else y = x - tan(x); a = x2; x = bisect(@fex4,x1,x2,1); if isnan(x) nroots = nroots + 1; root(nroots) = x; end end end >> root = root 0 4.4934 7.7253 10.9041 14.0662 17.2208

Newton Raphson method program


function [root,numIter] = newton_simple(func,dfunc,x,tol) % Simple version of Newton-Raphson method used in Example. if nargin < 5; tol = 1.0e6*eps; end for i = 1:30 dx = -feval(func,x)/feval(dfunc,x); x = x + dx; if abs(dx) < tol root = x; numIter = i; return end function y = fex4(x) end % first Function used in Example root = NaN

y = x4 - 6.4*x3 + 6.45*x2 + 20.538*x - 31.752;


function y = dfex4(x) % second Function used in Example y = 4.0*x3 - 19.2*x2 + 12.9*x + 20.538;

Newton Raphson method program


Here are the results:
>> [root, numIter] = newton_simple(@fex4, @dfex4, 2.0) root = 2.1000 numIter = 27 It can be shown that near a multiple root the convergence of the Newton Raphson method is linear, rather than quadratic, which explains the large number of iterations. Convergence to a multiple root can be speeded up by replacing the Newton Raphson formula as : x(i+1) = x(i) m f(x(i))/f(x(i)) Here m is multiplicity (2 in this case). After the change the answer is obtained after 5 iterations.

You might also like