lec 9 other methods
DESCRIPTION
Lecture 9 On Numerical MethodsTRANSCRIPT
Roots of Nonlinear Equations
Topic: Other Methods
Dr. Nasir M Mirza
Numerical Numerical MethodsMethods
Email: [email protected]
Lecture’s 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 24roots_ p1=roots(p1) % Find the roots of p1(x)
roots_p1 =4.00003.00002.00001.0000
We observe that MATLAB displays the polynomial coefficients as a row vector, and the roots as a column vector.
Example 1.2Find the roots of the polynomial: p2(x)= x5 – 7x4 + 16x2 + 25x +52Solution: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.50142.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.3It 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 polynomialr3 = 1 2 3 4poly(r3) % Find the polynomial coefficients1 -10 35 -50 24These 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 4−5j ]r4 = Columns 1 through 4-1.0000 -2.0000 -3.0000 -4.0000 + 5.0000iColumn 5-4.0000 - 5.0000ipoly(r4)1 14 100 340 499 246
Therefore, the polynomial is: x5 + 14x4 + 100x3 + 340x2 + 499x +246
Example 5The 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 6Let p1 = x5 – 3x4 + 5x2 + 7x + 9
p2 = 2x6 – 8x4 + 4x2 + 10x + 12Compute 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 7Let p3 = x7 – 3x5 – 5x3 + 7x + 9 p4 = 2x6 – 8x5 + 4x2 + 10x + 12Compute 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.5000r = 0 4 -3 0 3 2 3Therefore, the quotient and remainder are
q(x)=0.5 r(x) = 4x5 – 3x4 + 3x2 +2x+3
Example 8Let 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 Matlabfunction [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; returnend 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 f (x)0.0 5.0000.2 4.6080.4 3.4640.6 1.6160.8 -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 1y = x.^3 - 10.0*x.^2 + 5.0;
x1 = 0.7200 x2 = 0.7400 >>
Output is here.
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 Matlabfunction 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; endif nargin < 4; filter = 0; endf1 = feval(func,x1);if f1 == 0.0; root = x1; return; endf2 = feval(func,x2); if f2 == 0.0; root = x2; return; endif f1*f2 > 0;error(’Root is not bracketed in (x1,x2)’)end
bisect function in Matlab - continuedn = ceil(log(abs(x2 - x1)/tol)/log(2.0));for i = 1:nx3 = 0.5*(x1 + x2); f3 = feval(func,x3);if(filter == 1) & (abs(f3) > abs(f1))...& (abs(f3) > abs(f2))root = NaN; returnendif f3 == 0.0root = x3; returnendif f2*f3 < 0.0x1 = x3; f1 = f3;elsex2 = x3; f2 = f3;endendroot=(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 bisectiona = 0.0; b = 20.0; dx = 0.01;nroots = 0;while 1
[x1,x2] = rootsearch(@fex4,a,b,dx);if isnan(x1)
breakelse
a = x2;x = bisect(@fex4,x1,x2,1);if ˜isnan(x)
nroots = nroots + 1;root(nroots) = x;
endend
endroot
function y = fex4(x)% Function used in Example y = x - tan(x);
>> root =0 4.4934 7.7253 10.9041 14.0662 17.2208
Newton Raphson method programfunction [root,numIter] = newton_simple(func,dfunc,x,tol)% Simple version of Newton-Raphson method used in
Example. if nargin < 5; tol = 1.0e6*eps; endfor i = 1:30dx = -feval(func,x)/feval(dfunc,x);x = x + dx;if abs(dx) < tolroot = x; numIter = i; returnendendroot = NaN
function y = fex4(x)% first Function used in Example y = xˆ4 - 6.4*xˆ3 + 6.45*xˆ2 + 20.538*x - 31.752;
function y = dfex4(x)% second Function used in Example y = 4.0*xˆ3 - 19.2*xˆ2 + 12.9*x + 20.538;
Newton Raphson method program
>> [root, numIter] = newton_simple(@fex4, @dfex4, 2.0)root = 2.1000numIter = 27
Here are the results:
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.