matlab l2-4

16
5 Functions of One Variable “Function Summary” on page 5-2 “Representing Polynomials” on page 5-3 “Evaluating Polynomials” on page 5-4 “Roots of Polynomials” on page 5-5 “Roots of Scalar Functions” on page 5-6 “Derivatives” on page 5-12 “Convolution” on page 5-13 “Partial Fraction Expansions” on page 5-14 “Polynomial Curve Fitting” on page 5-15 “Characteristic Polynomials” on page 5-17

Upload: ahmed-a-hwaidi

Post on 09-Dec-2015

215 views

Category:

Documents


2 download

DESCRIPTION

matlab L2-4

TRANSCRIPT

Page 1: matlab L2-4

5

Functions of One Variable

• “Function Summary” on page 5-2

• “Representing Polynomials” on page 5-3

• “Evaluating Polynomials” on page 5-4

• “Roots of Polynomials” on page 5-5

• “Roots of Scalar Functions” on page 5-6

• “Derivatives” on page 5-12

• “Convolution” on page 5-13

• “Partial Fraction Expansions” on page 5-14

• “Polynomial Curve Fitting” on page 5-15

• “Characteristic Polynomials” on page 5-17

Page 2: matlab L2-4

5 Functions of One Variable

Function SummaryThe following table lists the MATLAB polynomial functions.

Function Description

conv Multiply polynomials

deconv Divide polynomials

fzero Find root of continuous function of one variable

poly Polynomial with specified roots

polyder Polynomial derivative

polyfit Polynomial curve fitting

polyval Polynomial evaluation

polyvalm Matrix polynomial evaluation

residue Partial-fraction expansion (residues)

roots Find polynomial roots

Symbolic Math Toolbox software contains additional specialized support forpolynomial operations.

5-2

ahmed hwaidi
Highlight
Page 3: matlab L2-4

Representing Polynomials

Representing PolynomialsMATLAB software represents polynomials as row vectors containingcoefficients ordered by descending powers. For example, consider the equation

p x x x( ) 3 2 5

This is the celebrated example Wallis used when he first represented Newton’smethod to the French Academy. To enter this polynomial into MATLAB, use

p = [1 0 -2 -5];

5-3

ahmed hwaidi
Highlight
Page 4: matlab L2-4

5 Functions of One Variable

Evaluating PolynomialsThe polyval function evaluates a polynomial at a specified value. To evaluatep at s = 5, use

polyval(p,5)

ans =110

It is also possible to evaluate a polynomial in a matrix sense. In this casep(x) = x3 – 2x – 5 becomes p(X ) = X3 – 2X – 5I, where X is a square matrix andI is the identity matrix. For example, create a square matrix X and evaluatethe polynomial p at X:

X = [2 4 5; -1 0 3; 7 1 5];Y = polyvalm(p,X)

Y =377 179 439111 81 136490 253 639

5-4

ahmed hwaidi
Highlight
Page 5: matlab L2-4

Roots of Polynomials

Roots of PolynomialsThe roots function calculates the roots of a polynomial:

r = roots(p)

r =2.0946

-1.0473 + 1.1359i-1.0473 - 1.1359i

By convention, the MATLAB software stores roots in column vectors. Thefunction poly returns to the polynomial coefficients:

p2 = poly(r)

p2 =1 8.8818e-16 -2 -5

poly and roots are inverse functions, up to ordering, scaling, and roundofferror.

5-5

ahmed hwaidi
Highlight
Page 6: matlab L2-4

5 Functions of One Variable

Roots of Scalar Functions

In this section...

“Solving a Nonlinear Equation in One Variable” on page 5-6

“Using a Starting Interval” on page 5-8

“Using a Starting Point” on page 5-9

Solving a Nonlinear Equation in One VariableThe fzero function attempts to find a root of one equation with one variable.You can call this function with either a one-element starting point or atwo-element vector that designates a starting interval. If you give fzero astarting point x0, fzero first searches for an interval around this point wherethe function changes sign. If the interval is found, fzero returns a value nearwhere the function changes sign. If no such interval is found, fzero returnsNaN. Alternatively, if you know two points where the function value differsin sign, you can specify this starting interval using a two-element vector;fzero is guaranteed to narrow down the interval and return a value near asign change.

The following sections contain two examples that illustrate how to find a zeroof a function using a starting interval and a starting point. The examples usethe function humps.m, which is provided with MATLAB. The following figureshows the graph of humps.

5-6

Page 7: matlab L2-4

Roots of Scalar Functions

Setting Options For fzeroYou can control several aspects of the fzero function by setting options. Youset options using optimset. Options include:

• Choosing the amount of display fzero generates — see “Setting Options”on page 8-12, “Using a Starting Interval” on page 5-8, and “Using a StartingPoint” on page 5-9.

• Choosing various tolerances that control how fzero determines it is at aroot — see “Setting Options” on page 8-12.

• Choosing a plot function for observing the progress of fzero towards a root— see “Plot Functions” on page 8-24.

• Using a custom-programmed output function for observing the progress offzero towards a root — see “Output Functions” on page 8-16.

5-7

Page 8: matlab L2-4

5 Functions of One Variable

Using a Starting IntervalThe graph of humps indicates that the function is negative at x = -1 andpositive at x = 1. You can confirm this by calculating humps at these twopoints.

humps(1)

ans =16

humps(-1)

ans =-5.1378

Consequently, you can use [-1 1] as a starting interval for fzero.

The iterative algorithm for fzero finds smaller and smaller subintervals of[-1 1]. For each subinterval, the sign of humps differs at the two endpoints.As the endpoints of the subintervals get closer and closer, they converge tozero for humps.

To show the progress of fzero at each iteration, set the Display option toiter using the optimset function.

options = optimset('Display','iter');

Then call fzero as follows:

a = fzero(@humps,[-1 1],options)

This returns the following iterative output:

a = fzero(@humps,[-1 1],options)

Func-count x f(x) Procedure2 -1 -5.13779 initial3 -0.513876 -4.02235 interpolation4 -0.513876 -4.02235 bisection5 -0.473635 -3.83767 interpolation6 -0.115287 0.414441 bisection

5-8

Page 9: matlab L2-4

Roots of Scalar Functions

7 -0.115287 0.414441 interpolation8 -0.132562 -0.0226907 interpolation9 -0.131666 -0.0011492 interpolation

10 -0.131618 1.88371e-007 interpolation11 -0.131618 -2.7935e-011 interpolation12 -0.131618 8.88178e-016 interpolation13 -0.131618 8.88178e-016 interpolation

Zero found in the interval [-1, 1]

a =

-0.1316

Each value x represents the best endpoint so far. The Procedure column tellsyou whether each step of the algorithm uses bisection or interpolation.

You can verify that the function value at a is close to zero by entering

humps(a)

ans =

8.8818e-016

Using a Starting PointSuppose you do not know two points at which the function values of humpsdiffer in sign. In that case, you can choose a scalar x0 as the starting pointfor fzero. fzero first searches for an interval around this point on whichthe function changes sign. If fzero finds such an interval, it proceeds withthe algorithm described in the previous section. If no such interval is found,fzero returns NaN.

For example, set the starting point to -0.2, the Display option to Iter, andcall fzero:

options = optimset('Display','iter');a = fzero(@humps,-0.2,options)

fzero returns the following output:

5-9

Page 10: matlab L2-4

5 Functions of One Variable

Search for an interval around -0.2 containing a sign change:

Func-count a f(a) b f(b) Procedure

1 -0.2 -1.35385 -0.2 -1.35385 initial interval

3 -0.194343 -1.26077 -0.205657 -1.44411 search

5 -0.192 -1.22137 -0.208 -1.4807 search

7 -0.188686 -1.16477 -0.211314 -1.53167 search

9 -0.184 -1.08293 -0.216 -1.60224 search

11 -0.177373 -0.963455 -0.222627 -1.69911 search

13 -0.168 -0.786636 -0.232 -1.83055 search

15 -0.154745 -0.51962 -0.245255 -2.00602 search

17 -0.136 -0.104165 -0.264 -2.23521 search

18 -0.10949 0.572246 -0.264 -2.23521 search

Search for a zero in the interval [-0.10949, -0.264]:

Func-count x f(x) Procedure

18 -0.10949 0.572246 initial

19 -0.140984 -0.219277 interpolation

20 -0.132259 -0.0154224 interpolation

21 -0.131617 3.40729e-005 interpolation

22 -0.131618 -6.79505e-008 interpolation

23 -0.131618 -2.98428e-013 interpolation

24 -0.131618 8.88178e-016 interpolation

25 -0.131618 8.88178e-016 interpolation

Zero found in the interval [-0.10949, -0.264]

a =

-0.1316

The endpoints of the current subinterval at each iteration are listed under theheadings a and b, while the corresponding values of humps at the endpointsare listed under f(a) and f(b), respectively.

Note The endpoints a and b are not listed in any specific order: a can begreater than b or less than b.

5-10

Page 11: matlab L2-4

Roots of Scalar Functions

For the first nine steps, the sign of humps is negative at both endpoints ofthe current subinterval, which is shown in the output. At the tenth step,the sign of humps is positive at the endpoint, -0.10949, but negative at theendpoint, -0.264. From this point on, the algorithm continues to narrowdown the interval [-0.10949 -0.264], as described in the previous section,until it reaches the value -0.1316.

5-11

Page 12: matlab L2-4

5 Functions of One Variable

DerivativesThe polyder function computes the derivative of any polynomial. To obtainthe derivative of the polynomial p = [1 0 -2 -5],

q = polyder(p)

q =3 0 -2

polyder also computes the derivative of the product or quotient of twopolynomials. For example, create two polynomials a and b:

a = [1 3 5];b = [2 4 6];

Calculate the derivative of the product a*b by calling polyder with a singleoutput argument:

c = polyder(a,b)

c =8 30 56 38

Calculate the derivative of the quotient a/b by calling polyder with twooutput arguments:

[q,d] = polyder(a,b)

q =-2 -8 -2

d =4 16 40 48 36

q/d is the result of the operation.

5-12

ahmed hwaidi
Highlight
Page 13: matlab L2-4

Convolution

ConvolutionPolynomial multiplication and division correspond to the operationsconvolution and deconvolution. The functions conv and deconv implementthese operations.

Consider the polynomials a(s) = s2 + 2s + 3 and b(s) = 4s2 + 5s + 6. To computetheir product,

a = [1 2 3]; b = [4 5 6];c = conv(a,b)

c =4 13 28 27 18

Use deconvolution to divide a(s) back out of the product:

[q,r] = deconv(c,a)

q =4 5 6

r =0 0 0 0 0

5-13

ahmed hwaidi
Highlight
Page 14: matlab L2-4

5 Functions of One Variable

Partial Fraction Expansionsresidue finds the partial fraction expansion of the ratio of two polynomials.This is particularly useful for applications that represent systems in transferfunction form. For polynomials b and a, if there are no multiple roots,

b sa s

rs p

rs p

rs p

kn

ns

( )( )

1

1

2

2

where r is a column vector of residues, p is a column vector of pole locations,and k is a row vector of direct terms. Consider the transfer function

4 8

6 82s

s s

b = [-4 8];a = [1 6 8];[r,p,k] = residue(b,a)

r =-12

8

p =-4-2

k =[]

Given three input arguments (r, p, and k), residue converts back topolynomial form:

[b2,a2] = residue(r,p,k)

b2 =-4 8

a2 =1 6 8

5-14

ahmed hwaidi
Highlight
Page 15: matlab L2-4

Characteristic Polynomials

Characteristic PolynomialsThe poly function also computes the coefficients of the characteristicpolynomial of a matrix:

A = [1.2 3 -0.9; 5 1.75 6; 9 0 1];poly(A)

ans =1.0000 -3.9500 -1.8500 -163.2750

The roots of this polynomial, computed with roots, are the characteristicroots, or eigenvalues, of the matrix A. (Use eig to compute the eigenvaluesof a matrix directly.)

5-17

ahmed hwaidi
Highlight
Page 16: matlab L2-4

5 Functions of One Variable

5-18