Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Numerical Analysis Lab Roots of Equations LAB 3 ROOTS OF EQUATIONS 1.0 OBJECTIVE To find roots of equations using Matlab software. 2.0 INTRODUCTION Numerical methods are used to solve problems on computers or calculators by numerical calculation, giving a table of numbers and/or graphical representation (figures). The steps from a given situation (in engineering, economics, etc) to the final answer are usually as follows. 1. Modeling. We set up a mathematical model of the problem, such as an integral, a system of equations, or a differential equation. 2. Choice of mathematical methods, perhaps together with a preliminary error estimation, a choice of step sizes, etc. 3. Programming. From an algorithm we write a program, say, in FORTRAN, C, or C++, and/or select suitable routines from a software system. Or we may decide to use a computing environment, such as MAPLE, MATLAB, and MATHCAD. 4. Doing the computation 5. Interpretation of results in physical or other terms, including decisions to rerun if further results are needed. In this lab we will use MATLAB software to solve the problems that involving engineering applications. 1 Numerical Analysis Lab Roots of Equations Figure 3.0: Symbols used in flowchart 2 Numerical Analysis Lab Roots of Equations 3.0 SUMMARY OF THEORY Many engineering analyses require the determination of the value(s) of the variable x that satisfies a nonlinear equation: f ( x) 0 (3.1) The values of x that satisfy Eq.(1) are known as the root of Equation (1); the number of roots may be finite or infinite and may be real or complex depending on the nature of the equation and the physical problem. The function f(x) may or may not be available in explicit form. If it is available in explicit form, it may be in the form of a general nonlinear equation, or a polynomial or a transcendental equation: x 4 80 x 120 0 .(polynomial equation (3.2) and tan x tanh x 0. (transcendental equation). (3.3) The roots of Eq. (3.1) are also known as the zeros of the function f(x). Thus, a function is said to have zeros, while an equation is said to have roots. A transcendental function is one whose value cannot be determined for any specified value of the argument by a finite number of additions, subtraction, multiplications, or divisions. Exponential, logarithmic, trigonometric, and hyperbolic functions are examples of transcendental functions. Any equation containing transcendental function is called a transcendental equation. There are two major classes of methods available to find the approximate values of the roots in nonlinear equation. They are Bracketing method. As the name implies, these are based on two initial guesses that ‘’bracket’’ the root-that is, are on either side of the root. Open methods. These methods can involve one or more initial guesses, but there is no need for them to bracket the root. For well-posed problems, the bracketing methods always work but converge slowly (i.e., they typically take more iterations to home in on the answer). In contrast, the open methods do not always work (i.e., they can diverge), but when they do they usually converge quicker. In both cases, initial guesses are required. 3 Numerical Analysis Lab Roots of Equations Stopping Criteria Suppose {xn} is a sequence, converging to a limit x*. The limit x* has the property f(x*)=0. Let tol be a positive number Absolute error (in x) |xn – x*| < tol Absolute error in f |f(xn)| < tol Relative error (in x) |xn – x*| / |x*| < tol 4 Numerical Analysis Lab Roots of Equations MATLAB Function 1) fzero – is designed to find the real root of a single equation. A simple representation of its syntax is fzero(function,x0) where function is the name of the function being evaluated, and x0 is the initial guess. Note that two guesses that bracket the root can be passed as a vector: fzero(function,[x0,x1] where x0 and x1 are guesses that bracket a sign change. Example To find the root of equation f ( x) x 2 9 >> x = fzero(inline(‘x^2-9’),-4) x = -3 >> x = fzero(inline(‘x^2-9’),[0 4]) x=3 2) roots – is designed to find the real and complex roots of a polynomial equations. The roots function has the syntax, x = roots (c) where x is a column vector containing the roots and c is a row vector containing the polynomial’s coefficients. Example Determine all the roots of polynomial: f ( x) x 5 3.5x 4 2.75x 3 2.15x 2 3.875x 1.25 >> a = [ 1 -3.5 2.75 2.125 -3.875 1.25] >> x = roots (a) x= 2.0000 -1.0000 1.0000 + 0.5000i 1.0000 - 0.5000i 0.5000 5 Numerical Analysis Lab Roots of Equations 3) poly – is designed to find the polynomial’s coefficients. It syntax is c = poly (r) where r is a column vector containing the roots and c is a row vector containing the polynomial’s coefficients. Example >> a=poly(x) a= 1.0000 -3.5000 2.7500 2.1250 -3.8750 1.2500 4) polyval – is designed to evaluate of a polynomial of n degree at x. It syntax is y = polyval ( p, x) where p is a column vector containing the polynomial’s coefficients, x is/are column vector containing the element(s) of x, and y is/are a column vector containing the answer(s) from evaluation. Example >> p = [ 1 -3.5 2.75 2.125 -3.875 1.25] p= 1.0000 -3.5000 2.7500 2.1250 -3.8750 >> y=polyval(p,[3 4]) y= 42.5000 323.7500 6 1.2500 Numerical Analysis Lab Roots of Equations 1.0 Bisection Method In order to find the roots of the equations f(x) = 0 using the bisection method, the function f(x) is first evaluated at equally spaced intervals of x until two successive function values are found with opposite signs. Let a = xk and b = xk+1 be the values of x at which the function values of x at which the function values f(a) and f(b) have apposite signs. This implies that the function has a root between a = xk and b = xk+1. x mid ab 2 (1.0) The interval (xk, xk+1), in which the root is expected to lie, is called the interval of uncertainty. The midpoint of the current interval of uncertainty (a, b) is computed as and the function value f(xmid) is determined. If f(xmid) = 0, xmid will be a root of f(x) = 0. If f(xmid) ≠ 0, then the sign of f(xmid) will coincide with that of f(a) or f(b) . If the signs of f(xmid) and f(a) coincide, then a is replaced by xmid . Otherwise (that is, if the signs of f(xmid) and f(b) coincide), b is replaced by xmid. Thus the interval of uncertainty is reduced to half of its original value. Again the midpoint of the current interval of uncertainty is computed using Eq.(3.0), and the procedure is repeated until a specified convergence criterion is satisfied. The reduction of the interval of uncertainty (i.e., the progress of the iterative process) is shown in (). The following convergence criterion can be used to stop the iterative procedure: f ( xmid ) or bi ai (2.1) Here ε is a specified small number. Assuming that the values of a and b, at which and have opposite signs, are known, the iterative procedure used to find the roots of = 0 can be summarized as follows: 1. Set a(1) = a, b(1) = b, and i = 0. 2. Set iteration number i = i + 1. a (i ) b (i ) 3. Find xmid = . 2 4. If xmid satisfies the convergence criterion f ( xmid ) ≤ε or bi ai 7 Numerical Analysis Lab Roots of Equations take the desired root as xroot = xmid and stop the procedure. Otherwise, go to step 5. 5. If f(xmid). f(a(i)) > 0, both f(xmid) and f(a(i)) will have the same sign, hence, set a(i+1) = xmid and b(i+1) = b(i), and go to step 2. 6. If f(xmid). f(a(i)) < 0, f(xmid) and f(a(i)) will have opposite signs, hence, set b(i+1) = xmid and a(i+1) = a(i), and go to step 2. 8 Numerical Analysis Lab Roots of Equations Flowchart of Bisection Method Start iteration number =n tolerance = ε Set a(i) = a, b(i) = b Yes f(a(i))*f(b(i)) > 0 No Yes i=0 i>n STOP i=i+1 No xmid = a (i ) b (i ) , f(xmid) 2 9 Numerical Analysis Lab a(i+1) = xmid b(i+1) = b(i) Yes Roots of Equations sign f(xmid) = = sign f(a(i)) No b(i+1) = xmid a(i+1) = a(i) sign f(xmid) ~ = sign f(a(i)) b(i+1), a(i+1) Yes STOP abs (a(i+1)-b(i+1)) < ε 10 No Numerical Analysis Lab Roots of Equations Example-Bisection Method Find the root of equation f ( x) e x x using the bisection method with x1 = 0, x2 = 1, and ε =10-3. MATLAB Programming % Using bisection to find the roots f(x)=exp(-x)-x. function xm =Bisection(xleft,xright,n,esp) % Synopsis: x = Bisection(xleft,xright) % x = Bisection(xleft,xright,n) % % Input: xleft,xright = left and right brackets of the root % n = (optional) number of iterations % esp= value of tolerance % % Output: x = estimate of the root a = xleft; b =xright; fa = exp(-a)-a; fb = exp(-b)-b; % Initial values of f(a) and f(b) if fa*fb > 0 disp ('There are not roots in interval') return end fprintf(' k f(xmid)\n'); a for k=1:n xm = (a + b)/2; fm = exp(-xm)-xm; fprintf('%3d xmid b %computing the midpoint % f(x) at midpoint %12.8f %12.8f %12.8f if sign(fm) == sign(fa) a = xm; fa = fm; else b = xm; fb = fm; end if abs(b-a)< esp break end end 11 %12.3e\n',k,a,xm,b,fm); Numerical Analysis Lab Roots of Equations 2.0 Fixed-Point Iteration In this method, the equation f(x) = 0 is rewritten in the form x = g(x), And an iterative procedure is adopted using the relation xi+1 = g(xi); i = 1, 2, 3, ……… where a new approximation to the root, xi+1, is found using the previous approximation, xi (x1 denotes the initial guess). The iterative process can be stopped whenever the convergence criterion xi 1 xi is satisfied, where ε is a small number on the order of 10-3 to 10-6. 12 Numerical Analysis Lab Roots of Equations Flowchart of Fixed-Point Iteration Method Start iteration number =n tolerance = ε Set xi(initial guess) Yes STOP i=0 i>n i=i+1 No g(xi) xi+1 = g(xi) STOP Yes abs (xi+1-xi) < ε 13 No Numerical Analysis Lab Roots of Equations Example-Fixed Point Iteration Method Find the root of equation f ( x) e x x using the Fixed Point Iteration method with x1 = 1 and ε =10-3 MATLAB Programming function xm =FixedPoint(x0,n) % Using fixed Point Iteration to find the roots of f(x) = exp(-x)-x % Synopsis: x =Newton(x0,left,right) % x =Newton(x0,left,right,n) % % Input: x0 = initial guess % n = (optional) number of iterations % % Output: x = estimate of the root x=x0; g=exp(-x); fprintf(' k x g(x) abs(x(k+1)-x(k))\n\n'); for k=1:n g = exp(-x); diff = abs(g - x); fprintf('%3d %12.6f %12.6f %18.5e\n',k-1,x,g,diff); x = g; if diff < esp break end end 14 Numerical Analysis Lab Roots of Equations 3.0 Newton-Raphson Method By neglecting the higher order terms, the Taylor’s series expansion of the function f(x) about an arbitrary point x1 is approximated as f ( x) f ( x1 ) ( x x1 ) f ' ( x1 ) (3.0) In order to find the root of f(x) = 0, we set f(x) equal to zeros in Eq. (1.0) to obtain f ( x1 ) ( x x1 ) f ' ( x1 ) 0 (3.1) Since the higher order derivative terms were neglected in the approximation of f(x) in Eq. (1.0), the solution of Eq. (1.1) yields the next approximation to the root (instead of the exact root) as x x2 x1 f ( x1 ) f ' ( x1 ) (3.2) where x2 denotes an improved approximation to the root. To further improve the root, we use x2 in place of x1 on the right-hand side of Eq. (1.2) to obtain x3. This iterative procedure can be generalized as xi 1 xi f ( xi ) : i 1,2,.... f ' ( xi ) (3.3) The procedure is shown graphically in Fig.1. Assuming a real root for the equation f(x) = 0. If xi is the initial guess for the root of f (xi) = 0, the point of intersection of the tangent to the curve at xi with the x axis gives the next approximation to the root, xi+1. The convergence of the procedure to the exact root can also be seen in Fig. 1. 15 Numerical Analysis Lab Roots of Equations f(x) x f x f(xi) i, i f(xi+1) xi+2 xi+1 xi X Figure 1. Geometrical illustration of the Newton-Raphson method The iterative process can be stopped whenever the convergence criterion xi 1 xi or f ( xi 1 ) is satisfied, where ε is a small number on the order of 10-3 to 10-6. Algorithm The steps to apply Newton-Raphson method to find the root of an equation f(x) = 0 are 1. Evaluate f ' ( x) symbolically 2. Use an initial guess of the root, xi, to estimate the new value of the root xi+1 as xi 1 = xi 3. f(xi ) f'(x i ) Find the absolute relative approximate , a as xi 1 xi or f ( xi 1 ) 4. Also check if the number of iterations has exceeded the maximum number of iterations 16 Numerical Analysis Lab Roots of Equations Flowchart of Newton-Raphson Method Start iteration number =n tolerance = ε Set xi(initial guess) Yes STOP i=0 i>n i=i+1 No f(xi), f’(xi) xi 1 xi f ( xi ) f ' ( xi ) Yes STOP No abs (xi+1-xi) < ε 17 Numerical Analysis Lab Roots of Equations Example-Newton-Raphson Method Find the root of equation f ( x) e x x using the Newton-Raphson method with starting point x1 = 0.0 and the convergence criterion ε =10-3. MATLAB Program function xm =Newton(x0,n,esp) % Using Newton method to find the roots of f(x)= exp(-x)-x % % Synopsis: x =Newton(x0) % x =Newton(x0,n) % % Input: x0 = initial guess % n = (optional) number of iterations; default: n =8 % % Output: x = estimate of the root xr=x0; fprintf(' k % Initial Guess f(x) dfdx x(k+1)\n'); for k=1:n xold = xr; f = exp(-xr)-xr; dfdx = -exp(-xr)-1; xr = xr-f/dfdx; fprintf('%3d %12.3e %12.3e %18.15f\n',k-1,f,dfdx,xr); if abs(xr-xold)< esp break end end 4.0 Secant Method 18 Numerical Analysis Lab Roots of Equations The secant method is similar to the Newton’s method, but is different in that the derivative f’ is approximated by using two consecutive iterative values of f. The derivative f ' ( xi ) is approximated as f ' ( xi ) f ( xi ) f ( xi 1 ) xi xi 1 The general expression for the iterative process can then be written as xi 1 xi f ( xi ) f ( xi )[ xi xi 1 ] xi ; i 2,3,4,... ' f ( xi ) f ( xi 1 ) f ( xi ) Note that the Secant Method requires two initial guesses x1 and xi-1 to start iterative process. The following iterative process can be used to implement the secant method 1. Start with two initial approximations x1 and x2 for the root of f ( x) 0 and a small number ε to test the convergence of the process. Set i= 2. 2. Find the new approximation, xi 1 , as xi 1 xi f ( xi )( xi xi 1 ) f ( xi ) f ( xi 1 ) 3. Verify the convergence of the process. If f ( xi 1 ) , or xi 1 xi stop the process by taking xi 1 as the root. Otherwise, update the iteration number as i = i+1 and go to step 2. Flowchart of Secant Method 19 Numerical Analysis Lab Roots of Equations Start iteration number =n tolerance = ε Set xi-1 = a, xi = b f(xi-1), f(xi) No Yes i>n STOP i=0 i=i+1 No xi 1 xi f ( xi )( xi xi 1 ) , f(xi+1) f ( xi ) f ( xi 1 ) xi 1 xi xi xi 1 Yes STOP abs (xi+1-xi) < ε Example-Secant Method 20 No Numerical Analysis Lab Roots of Equations Find the root of equation f ( x) e x x using the bisection method with x1 = 0, x2 = 1, and ε =10-3. MATLAB Programming function xm =Secant(xleft,xright,n) % Using secant method to find the roots of f(x)=exp(-x)-x. % % Synopsis: x =Secant(xleft,xright) % x =Secant(xleft,xright,n) % % Input: xleft,xright = left and right brackets of the root % n = (optional) number of iterations % esp= value of tolerance % % Output: x = estimate of the root a = xleft; b =xright; % Copy original bracket to local variables fa = exp(-a)-a; % Initial values of f(a) and f(b) fb = exp(-b)-b; fprintf(' k a b xmid f(xmid)\n'); for k=1:n xm = b - fb*((b-a)/(fb-fa)); % Computes the new value of x fm = exp(-xm)-xm; % f(x) at new x value fprintf('%3d %12.8f %12.8f %12.8f %12.3e\n',k,a,b,xm,fm); a = b; % replace the old values fa = fb; b = xm; fb = fm; if abs(b-a)< esp break end end Exercises 21 Numerical Analysis Lab Roots of Equations 1. Determine all the roots with MATLAB of f ( x) x 3 6 x 2 11x 6.1 2. Use bisection to determine the mass of the bungee jumper with a drag coefficient of 0.25 kg/m to have a velocity of 36 m/s after 4 s of free fall. Note: The acceleration of gravity is 9.81 m/s2. Start with initial guesses of xlower = 50 kg and xupper = 200 kg and iteration until the approximate absolute error fall below 0.001. f (m) gcd gm tanh( t) v cd m 22