Professional Documents
Culture Documents
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.
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
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
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.
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.
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