Elementary mathematical and computational tools for electrical and computer engineers using Matlab - Chapter 5 potx

22 327 0
Elementary mathematical and computational tools for electrical and computer engineers using Matlab - Chapter 5 potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

0-8493-????-?/00/$0.00+$.50 © 2000 by CRC Press LLC © 2001 by CRC Press LLC 5 Root Solving and Optimization Methods In this chapter, we first learn some elementary numerical techniques and the use of the fsolve and fzero commands from the MATLAB library to obtain the real roots (or zeros) of an arbitrary function. Then, we discuss the use of the MATLAB command roots for finding all roots of a polynomial. Following this, we consider the Golden Section method and the fmin and fmins MATLAB commands for optimizing (finding the minimum or maxi- mum value of a function) over an interval. Our discussions pertain exclu- sively to problems with one and two variables (input) and do not include the important problem of optimization with constraints. 5.1 Finding the Real Roots of a Function This section explores the different categories of techniques for finding the real roots (zeros) of an arbitrary function. We outline the required steps for com- puting the zeros using the graphical commands, the numerical techniques known as the Direct Iterative and the Newton-Raphson methods, and the built-in fsolve and fzero functions of MATLAB. 5.1.1 Graphical Method In the graphical method, we find the zeros of a single variable function by implementing the following steps: 1. Plot the particular function over a suitable domain. 2. Identify the neighborhoods where the curve crosses the x-axis (there may be more than one point); and at each such point, the following steps should be independently implemented. 3. Zoom in on the neighborhood of each intersection point by repeated application of the MATLAB axis or zoom commands. © 2001 by CRC Press LLC 4. Use the crosshair of the ginput command to read the coordinates of the intersection. In problems where we desire to find the zeros of a function that depends on two input variables, we follow (conceptually) the same steps above, but use 3-D graphics. In-Class Exercises Pb. 5.1 Find graphically the two points in the x-y plane where the two sur- faces, given below, intersect: (Hint: Use the techniques of surface and contour renderings, detailed in Chapter 1, to plot the zero height contours for both surfaces; then read off the intersections of the resulting curves.) Pb. 5.2 Verify your graphical answer to Pb. 5.1 with that you would obtain analytically. 5.1.2 Numerical Methods This chapter subsection briefly discusses two techniques for finding the zeros of a function in one variable, namely the Direct Iterative and the Newton- Raphson techniques. We do not concern ourselves too much, at this point, with an optimization of the routine execution time, nor with the inherent lim- its of each of the methods, except in the most general way. Furthermore, to avoid the inherent limits of these techniques in some pathological cases, we assume that we plot each function under consideration, verify that it crosses the x-axis, and satisfy ourselves in an empirical way that there does not seem to be any pathology around the intersection point before we embark on the application of the following algorithms. These statements will be made more rigorous to you in future courses in numerical analysis. 5.1.2.1 The Direct Iterative Method This is a particularly useful technique when the equation f(x) = 0 can be cast in the form: x = F(x) (5.1) zxy zxy 1 22 2 725 42 4 =− + + =− − © 2001 by CRC Press LLC F(x) is then called an iteration function, and it can be used for the generation of the sequence: x k+1 = F(x k ) (5.2) To guarantee that this method gives accurate results in a specific case, the function should be continuous and it should satisfy the contraction condition: (5.3) where 0 ≤ s < 1; that is, the changes in the value of the function are smaller than the changes in the value of the arguments. To prove that under these conditions, the iterative function possesses a fixed point (i.e., that ultimately the difference between two successive iterations can be arbitrarily small) that can be immediately obtained from the above contraction condition [Eq. (5.3)]. PROOF Let the x guess be the first term in the iteration, then: (5.4) but since (5.5) then (5.6) Similarly, (5.7) translates into (5.8) The argument can be extended to the (m + 1)-iteration, where we can assert that: (5.9) Fx Fx sx x nmnm () ( )−≤− Fx Fx sx x guess guess () ( ) 11 −≤− Fx x Fx x guess () ()== 112 and xx sxx guess21 1 −≤ − Fx Fx sx x() () 2121 −≤− xx sxx sxx guess32 21 2 1 −≤ −≤ − xxsxx mm m guess+ −≤ − 11 © 2001 by CRC Press LLC but, because s is a non-negative number smaller than 1, the right-hand-side of the inequality in Eq. (5.9) can be made, for large enough value of m, arbitrarily small, and the above iterative procedure does indeed converge to a fixed point. Example 5.1 Find the zero of the function: y = x – sin(x) – 1 (5.10) Solution: At the zero, the iterative form can be written as: x(k) = sin(x(k – 1)) + 1 (5.11) The contraction property, required for the application of this method, is valid in this case because the difference between two sines is always smaller than the difference between their arguments. The fixed point can then be obtained by the following MATLAB program: x(1)=1; %value of the initial guess for k=2:20 x(k)=sin(x(k-1))+1; end If we display the successive values of x, we obtain: x Ans 1.0000 1.8415 1.9636 1.9238 1.9383 1.9332 1.9350 1.9344 1.9346 1.9345 1.9346 1.9346 1.9346 1.9346 1.9346 1.9346 1.9346 1.9346 1.9346 1.9346 As can be noticed from the above printout, about 11 iterations were required to get the value of the fixed point accurate to one part per 10,000. NOTE A more efficient technique to find the answer within a proscribed error tolerance is to write the program with the while command, where we can specify the tolerance level desired. 5.1.2.2 The Newton-Raphson Method This method requires a knowledge of both the function and its derivative. The method makes use of the geometrical interpretation of the derivative being the tangent at a particular point, and that the tangent is the limit of the chord between two close points on the curve. It is based on the fact that if f(x 1 ) and f(x 2 ) have opposite signs and the function f is continuous on the interval © 2001 by CRC Press LLC [x 1 , x 2 ], we know from the Intermediate Value theorem of calculus that there is at least one value x c between x 1 and x 2 , such that f(x c ) = 0. A sufficient con- dition for this method to work is that f ′(x) and f ″ (x) have constant sign on an open interval that contains the solution f(x) = 0; in that case, any starting point that is close enough to the solution will give successive Newton’s approximations that converge to the solution. Let x guess and x have the same meaning as in the iterative method; therefore, f(x) = 0, and the definition of the derivative results in the equation: (5.12) This relation can now be the basis of an iterative function given by: (5.13) The fixed point can be obtained, in general, for the same initial guess and tol- erance, in a smaller number of iterations in the Newton-Raphson method than in the Direct Iteration method. In-Class Exercise Pb. 5.3 Write a routine to find the zero of the function y = x – sin(x) – 1 using the Newton-Raphson algorithm. Pb. 5.4 Compare the answers from the present algorithm with that of the Direct Iterative method, at each iteration step, in the search for the zeros of the function y = x – sin(x) – 1, and comment on which of the two methods appears to be more effective and converges faster. Example 5.2 Apply the Newton-Raphson method to find the voltage-current relation in a diode circuit with an ac voltage source. Solution: The diode is a nonlinear semiconductor electronics device with a voltage current curve that is described, for voltage values larger than the reverse breakdown potential (a negative quantity), by: (5.14) where I s is the reverse saturation current (which is typically on the order of 10 –6 mA), and kT is the average thermal energy of an electron divided by its xx fx fx guess guess guess =− ′ () () xk xk fxk fxk () ( ) (( )) (( )) =−− − ′ − 1 1 1 iIe s vkT =− ′ () / 1 © 2001 by CRC Press LLC charge at the diode operating temperature (equal to 1/40 V at room temper- ature). An important application of this device is to use it as a rectifier (a device that passes the current in one direction only). (Can you think of a prac- tical application for this device?) The problem we want to solve is to find the current through the circuit (shown in Figure 5.1) as a function of time if we are given a sinusoidal time- dependent source potential. The other equation, in addition to Eq. (5.14) that we need in order to set the problem, is Ohm’s law across R. This law, as previously noted, states that the current passing through a resistor is equal to the potential difference across the resistor, divided by the value of the resistance: (5.15) Eliminating the current from Eqs. (5.14) and (5.15), we obtain a nonlinear equation in the potential across the diode. Solving this problem is then reduced to finding the roots of the function f defined as: (5.16) where the potential across the diode is the unknown. In the Newton-Raphson method, we also need for our iteration the deriva- tive of this function: (5.17) For a particular value of V s , we need to determine v and, from this value of the potential across the diode, we can determine the current in the cir- cuit. However, because we are interested in obtaining the current through FIGURE 5.1 The diode semi-rectifier circuit. D R V S I υ i Vv R s = − fv I v kT Vv R s s ( ) [exp( / ) ]= ′ −− −       1 ′ = ′       ′ +fv kT IvkT R s ( ) exp( / ) / 1 1 © 2001 by CRC Press LLC the diode for a source potential that is a function of time, we need to repeat the Newton-Raphson iteration for each of the different source voltage val- ues at the different times. The sequence of the computation would proceed as follows: 1. Generate the time array. 2. Generate the source potential for the different elements in the time array. 3. For each time array entry, find the potential across the diode using the Newton-Raphson method. 4. Obtain the current array from the potential array. 5. Plot the source potential and the current arrays as a function of the time array. Assuming that the source potential is given by: V s = V 0 sin(2πft) (5.18) and that f = 60 Hz, V 0 = 5 V, kT = 0.025 V, R = 500 Ω, and the saturation current I s is 10 –6 mA; the following script M-file finds the current in this circuit: Is=10^(-9); R=500; kT=1/40; f=60; V0=5; t=linspace(0,2/f,600); L=length(t); K=200; Vs=(V0*sin(2*pi*t*f))'*ones(1,K); v=zeros(L,K); i=zeros(L,K); for k=1:K-1 v(:,k+1)=v(:,k)-(Is*(exp((1/kT)*v(:,k))-1) (1/R)*(Vs(:,k)-v(:,k)))./ ((1/kT)*Is*exp((1/kT)*v(:,k))+1/R); i(:,k+1)=(Vs(:,k+1)-v(:,k+1))/R; end plot(t,1000*i(:,K),'b',t,Vs(:,K),'g') © 2001 by CRC Press LLC The current (expressed in mA) and the voltage (in V) of the source will appear in your graph window when you execute this program. Homework Problem Pb. 5.5 The apparent simplicity of the Newton-Raphson method is very misleading, suffice it to say that some of the original work on fractals started with examples from this model. a. State, to the best of your ability, the conditions that the function, its derivative, and/or the original guess should satisfy so that this iterate converges to the correct limit. Supplement your arguments with geometric sketches that illustrate each of the pathologies. b. Show that the Newton-Raphson method iterates cannot find the zero of the function: c. Illustrate, with a simple sketch, the reason that this method does not work in part (b). 5.1.3 MATLAB fsolve and fzero Built-in Functions Next, we investigate the use of the MATLAB command fsolve for finding the zeros of any function. We start with a function of one variable. The recommended sequence of steps for finding the zeros of a function is as follows: 1. Edit a function M-file for the function under consideration. 2. Plot the curve of the function over the appropriate domain, and estimate the values of the zeros. 3. Using each of the estimates found in (2) above as an initial “guess,” use the command fsolve to accurately find each of the roots. The syntax is as follows: xroot=fsolve('funname',xguess) NOTE Actually, the MATLAB command fzero is quite suitable for finding the zero of a function of one variable. However, we used fsolve in the text above because it can only be used for the two-variables problem. yx=−3 © 2001 by CRC Press LLC In the following application, we use the command fzero to find the zeros of a Bessel function, and learn in the process some important facts about this often-used special function of applied mathematics. Application Bessel functions are solutions to Bessel’s differential equations of order n, given by: (5.19) There are special types of Bessel functions referred to as “of the first, second, and third kinds.” Bessel functions of integer order appear, inter alia, in the expression of the radiation field in cylindrically shaped resonant cavities, and in light diffraction from a circular hole. Bessel functions of half-integer indices (see Pb. 2.26) appear in problems of spherical cavities and scattering of elec- tromagnetic waves. Airy functions, a member of the Bessel functions family, appear in a number of important problems of optics and quantum mechanics. The recursion formula that relates the Bessel function of any kind of a cer- tain order with those of the same kind of adjacent orders is 2nZ n (x) = xZ n–1 (x) + xZ n+1 (x) (5.20) where Z n (x) is the generic designation for all kinds of Bessel functions. In this application, we concern ourselves only with the Bessel function of the first kind, usually denoted by J n (x). Its MATLAB call command is besselj(n,x). In the present problem, we are interested in the root struc- ture of the Bessel function of the first kind and of zero order. In the program that follows, we call the Bessel function from the MATLAB library; however, we could have generated it ourselves using the techniques of Section 4.7 because we know the ODE that it satisfies, and its value and that of its derivative at x = 0, namely: The problem that we want to solve is to find the zeros of J 0 (x) and compare to these exact values those obtained from the approximate expression: (5.21) To implement this task, edit and execute the following script M-file: for k=1:10 p(k)=4*k-1; x dy dx x dy dx xny 2 2 2 0++−=() Jx Jx 00 01 00() ()== ′ ==and xk kk k k0 3355 4 41 1 24 1 31 641 3779 15 4 1 , () () () () ≈−+ − − − + − +… π ππ π © 2001 by CRC Press LLC x0(k)=fzero('besselj(0,x)',(pi/4)*p(k)); x0approx(k)=(pi/4)*p(k)+(1/(2*pi))*(p(k)^(-1)) (31/6)*(1/pi^3)*(p(k)^(-3))+ (3779/15)*(1/pi^5)*(p(k)^(-5)); end kk=1:10; subplot(2,1,1); plot(kk,x0,'o') title('Zeros of Zero Order BesselJ Function') subplot(2,1,2); semilogy(kk,x0-x0approx,'o') title('Error in Approximate Values of the Zeros') As you can easily observe by examining Figure 5.2, the approximate series is suitable for calculating all (except the smallest) zeros of the function J 0 (x) cor- rectly to at least five digits. FIGURE 5.2 The first ten zeros of the Bessel function J 0 (x). Top panel: The values of the successive zeros (roots) of J 0 (x). Bottom panel: Deviation in the values of these zeros between their exact expressions and their approximate values as given in Eq. (5.21). [...]... a(2) = c(1) = a(1) + r(b(1) − a(1)) (5. 26) b(2) = b(1) (5. 27) Now, replacing the values of a(2) and b(2) from Eqs (5. 26) and (5. 27) into Eq (5. 25) , we are led to a second-degree equation in r: r2 – 3r + 1 = 0 The desired root is the value of the Golden ratio: © 2001 by CRC Press LLC (5. 28) r= 3− 5 2 (5. 29) and hence, the name of the method The following function M-file implements the above algorithm:... file for the negative of this function (call it n-funname) and entering the following commands in the command window: xmax=fmin('n-funname',xi,xf) fmax =-1 *feval('n-funname',xmax) Homework Problems Pb 5. 22 We have two posts of height 6 m and 8 m, and separated by a distance of 21 m A line is to run from the top of one post to the ground between the posts and then to the top of the other post (Figure 5. 3)... in the interval 3 < x < 3 .5, accurate to 10–4, we would enter in the command window, after having saved the above function M-file, the following command: © 2001 by CRC Press LLC [xmin,ymin]=goldensection('cos',3,3 .5, 10^ (-4 )) 5. 3.3 MATLAB fmin and fmins Built-in Function Following methodically the same steps using fzero to find the zeros of any function, we can use the fmin command to find the minimum of... farray=funname(array) x=array(1); y=array(2); farray(1)=7-sqrt( 25+ x.^2+y.^2); farray(2)= 4-2 *x-4*y; 3 Use the approximate value found in step 1 as the value for the guess array; for example: xyguess=[4 -1 ]; 4 Finally, use the fsolve command to accurately find the root The syntax is: © 2001 by CRC Press LLC xyroots=fsolve('funname',xyguess) xyroots = 4.7081 -1 . 354 1 5 To find the second root, use the second value... tolerance) r=(3-sqrt (5) )/2; c=a+r*(b-a); fc=feval(funname,c); d=a+(1-r)*(b-a); fd=feval(funname,d); while d-c>tolerance if fc>=fd dnew=c+(1-r)*(b-c); a=c; c=d; fc=fd; d=dnew; fd=feval(funname,dnew); else cnew=a+r*(d-a); b=d; d=c; fd=fc; c=cnew; fc=feval(funname,cnew); end end xmin=(c+d)/2; ymin=feval(funname,xmin); For example, if we wanted to find the position of the minimum of the cosine function and its... complex numbers and with the term complex conjugate (the latter term should pique your curiosity), be a little patient Help is on the way; Chapter 6 covers the topic of complex numbers] Inversely, knowing the roots, we can reassemble the polynomial The command is poly poly(r) In-Class Exercise Pb 5. 14 Find the roots of the polynomial p = [1 pute their sum and product 3 2 1 0 3], and com- Pb 5. 15 Consider... the fsolve command: xyguess= [-4 2]; xyroots=fsolve('funname',xyguess) xyroots = -3 .9081 2. 954 1 This method can be extended to any number of variables and nonlinear equations, but the estimate of the roots becomes much more difficult and we will not go into further details here In-Class Exercises Find the values of x and y that simultaneously satisfy each pair of the following equations: Pb 5. 10 z1 = 0... roots of higher-order polynomials can© 2001 by CRC Press LLC not be found analytically MATLAB has a built-in command that finds all the roots (real and complex) for any polynomial equation As previously noted, the MATLAB command for finding the polynomial roots is roots: r=roots(p) In interpreting the results from this command, recall the Fundamental Theorem of Algebra, which states the root properties of... the power of the MATLAB fsolve function really shines in evaluating the roots of multivariable functions Example 5. 3 Find the intersection in the x-y plane of the parabaloid and the plane given in Pb 5. 1 Solution: We follow these steps: 1 Use the contour command to estimate the coordinates of the points of intersection of the surfaces in the x-y plane 2 Construct the function M-file for two functions... point is to accurately pinpoint the position and value of this minimum We detail the derivation of an elementary technique for this search: the Golden Section method More accurate and efficient techniques for this task have been developed These are incorporated in the built-in command fmin; the mode of use is discussed in Section 5. 3.3 © 2001 by CRC Press LLC 5. 3.2.1 Golden Section Method Assume that, . we desire that: (5. 25) (5. 26) (5. 27) Now, replacing the values of a(2) and b(2) from Eqs. (5. 26) and (5. 27) into Eq. (5. 25) , we are led to a second-degree equation in r: r 2 – 3r + 1 = 0 (5. 28) The. com- mand is poly. poly(r) In-Class Exercise Pb. 5. 14 Find the roots of the polynomial p = [132103], and com- pute their sum and product. Pb. 5. 15 Consider the two polynomials: p 1 = [132103]andp 2 . for com- puting the zeros using the graphical commands, the numerical techniques known as the Direct Iterative and the Newton-Raphson methods, and the built-in fsolve and fzero functions of MATLAB. 5. 1.1

Ngày đăng: 13/08/2014, 02:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan