Professional Documents
Culture Documents
Laboratory Module
EXPERIMENT 3
ROOTS OF EQUATIONS
1.0 OBJECTIVE
To find roots of equations using MATLAB software.
2.0. EQUIPMENT
Computers and MATLAB program in the Mechanical Design.
3.1.
Roots of Equations 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.(3.1) are known as the root of Equation (3.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:
11
Laboratory Module
and
(3.2) (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 methods. 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.2. 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*| / |xn| < tol
3.3.
3.3.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]
22
Laboratory Module
Example 3.1 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 3.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 polynomials coefficients. Example 3.2 Determine all the roots of polynomial:
f ( x ) = x 5 3.5 x 4 + 2.75 x 3 + 2.15 x 2 3.875 x +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 3.4. Graphical Methods A simple method for obtaining an estimate of the equation f(x)=0 is to make a plot of the function and observe where it crosses the x axis. This point, which represents the x value for which f(x)=0, provides a rough approximation of the root. Example 3.3 Use the graphical approach 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.
v=
gc d gm tanh( t) cd m
Solution
33
Laboratory Module
gc d gm tanh tv = 0 m cd 2. Write the following MATLAB session sets up a plot of f(m) versus mass: f (v ) =
-1
-2
-3
-4
-5 50
100
150
200
The function crosses the m axis between 140 and 150 kg. 3.5 Bracketing Method
3.5.1 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. a +b 2
x mid =
(3.4)
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.4), and the procedure is repeated until a specified convergence criterion is satisfied. The reduction of
44
Laboratory Module
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 ( x mid ) or
bi a i <
(3.5)
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 = 1. 2. Set iteration number i = i + 1. a (i ) + b (i ) 3. Find xmid = . 2 4. If xmid satisfies the convergence criterion
f ( x mid ) or bi a i <
take the desired root as xroot = xmid and stop the procedure. Otherwise, go to step 5/6. 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.
55
Laboratory Module
Yes
f(a(i))*f(b(i)) > 0
xmid =
a (i ) + b (i ) , f(xmid) 2
Yes
sign f(xmid) = = sign f(a(i)) No sign f(xmid) ~ = sign f(a(i)) b(i+1) = xmid a(i+1) = a(i)
b(i+1), a(i+1)
66
abs (a(i+1)-b(i+1)) < No
Laboratory Module
Yes
STOP
Example 3.4 Find the root of equation f ( x) = e x x using the bisection method with x1 = 0, x2 = 1, and =10-3. Procedure-MATLAB Programming 1. Start a new MatLab file by clicking on File, New and M-file that opens an empty file in the Editor/Debugger window.
for k=1:n xrold=xr; xr = (a + b)/2; %computing the midpoint fr = feval(f,xr); % f(x) at midpoint h = abs((xr-xrold)/xr)*100; if h< esp break end fprintf('%3d %12.8f %12.8f %12.8f %12.3e %12.8f\n',k,a,xr,b,fr,h);
77
Laboratory Module
3.5.2
False-Position Method
In numerical analysis, the false position method or regula falsi method is a root-finding algorithm that combines features from the bisection method and the secant method. Like the bisection method, the false position method starts two points a0 and b0 such that f(a0) and f(b0) are of opposite signs, which implies by the intermediate value theorem that the function f has a root in the interval [ a0, b0]. The method proceeds by producing a sequence of shrinking intervals [ak, bk] that all contain a root of f. At iteration number i, the number
ci =
ai f ( bi ) bi f ( ai ) , f ( bi ) f ( ai )
i = 0, 1, 2,
is computed. As explained below, ci is the root of the secant line through (ai, f(ai)) and (bi, f(bi)). If f(ai) and f(ci) have the same sign, then we set ai+1 = ci and bi+1 = bi, otherwise we set ai+1 = ai and bi+1 = ci. This process is repeated until the root is approximated sufficiently well. Example 3.5 Find the root of equation f ( x) = e x x using the false-position method with x1 = 0, x2 = 1, and =10-3. Procedure-MATLAB Programming 1. Start a new MatLab file by clicking on File, New and M-file that opens an empty file in the Editor/Debugger window.
Output:
fa = feval(f,a); fb = feval(f,b);
88
Laboratory Module
if fa*fb > 0 disp ('There are not roots in interval') return end fprintf(' k a xmid b f(xmid)\n');
fprintf('%3d %12.8f %12.8f %12.8f %12.3e\n',k,a,xr,b,fr,err); if sign(fr) == sign(fa) a = xr; fa = fr; else b = xr; fb = fr; end end 3. Click on Save As to save it as False.m.
4. Define equation using inline function in MatLab Command Window 5. To see how it works, type False (f,0,1,20,0.001) in MatLab Command Window.
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 <
99
Laboratory Module
i=0 i=i+1
No
g(xi)
xi+1 = g(xi)
No
Example 3.6 Find the root of equation f ( x) = e x x using the Fixed Point Iteration method with x0 = 1 and =10-3 Procedure-MATLAB Programming
1 10
Laboratory Module
1. Start a new MatLab file by clicking on File, New and M-file that opens an empty file in the Editor/Debugger window.
2. Write the program given below in M-file function xm =FixedPoint(x0,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,x,g,diff); x = g; if diff < esp break end end 3. Click on Save As to save it as FixedPoint.m.
4. Define equation using inline function in MatLab Command Window 5. To see how it works, type FixedPoint (1,20, 0.001) in MatLab Command Window.
3.6.2 Newton-Raphson Method By neglecting the higher order terms, the Taylors series expansion of the function f(x) about an arbitrary point x1 is approximated as
f ( x) = f ( x1 ) + ( x x1 ) f ' ( x1 )
(3.6)
In order to find the root of f(x) = 0, we set f(x) equal to zeros in Eq. (3.6) to obtain
f ( x1 ) + ( x x1 ) f ' ( x1 ) = 0
(3.7)
Since the higher order derivative terms were neglected in the approximation of f(x) in Eq. (3.6), the solution of Eq. (3.7) yields the next approximation to the root (instead of the exact root) as
x = x 2 = x1
f ( x1 ) f ' ( x1 )
(3.8)
1 11
Laboratory Module
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. (3.8) to obtain x3. This iterative procedure can be generalized as
xi +1 = xi
f ( xi ) : i = 1,2,.... f ' ( xi )
(3.9)
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.
f(x)
f(xi)
[ x f ( x )]
i, i
f ( xi +1 ) <
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
1 12
Laboratory Module
xi +1 = x i -
f(xi ) f'(x i )
f ( xi +1 ) <
4. Also check if the number of iterations has exceeded the maximum number of iterations Flowchart of Newton-Raphson Method
Start
i=1 i=i+1
f(xi), f(xi)
xi +1 = xi
f ( xi ) f ' ( xi )
No
STOP
Yes abs (xi+1-xi) <
Example 3.7 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. Procedure-MATLAB Programming
1 13
Laboratory Module
1. Start a new MatLab file by clicking on File, New and M-file that opens an empty file in the Editor/Debugger window.
2. Write the program given below in M-file function xm =Newton(x0,n,esp) % Input: % % % Output: xr=x0; fprintf(' k f(x) x0 n x = initial guess = number of iterations; default: n =8 = estimate of the root % Initial Guess dfdx x(k+1)\n');
for k=1:n xold = xr; f r = feval(f,xr); dfr = feval(f,xr); xr = xr-fr/dfr; fprintf('%3d %12.3e %12.3e %18.15f\n',k-1,f,dfdx,xr); if abs(xr-xold)< esp break end end 3. Click on Save As to save it as Newton.m. 4. Define values of x0,n, esp in Command Window. 5. To see how it works, type Newton (x0,n, esp); in MatLab Command Window.
3.6.3 Secant Method The secant method is similar to the Newtons 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.
1 14
Laboratory Module
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.
stop the process by taking x i + 1 as the root. Otherwise, update the iteration number as i = i+1 and go to step 2.
1 15
Laboratory Module
Set xi-1 = a, xi = b
i=1 i>n No
xi +1 = xi f ( xi )( xi xi 1 ) , f(xi+1) f ( xi ) f ( xi 1 )
i=i+1
xi 1 = xi xi = xi +1
Yes
STOP
No
Example 3.8 Find the root of equation f ( x) = e x x using the secant method with x1 = 0, x2 = 1, and =10-3. Procedure-MATLAB Programming
1 16
Laboratory Module
1. Start a new MatLab file by clicking on File, New and M-file that opens an empty file in the Editor/Debugger window.
2. Write the program given below in M-file function xm =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 = feval(f,a); % Initial values of f(a) and f(b) fb = feval(f,b); fprintf(' k a b xmid f(xmid)\n');
for k=1:n xr = b - fb*((b-a)/(fb-fa)); % Computes the new value of x fr = feval(f,xr); % f(x) at new x value fprintf('%3d %12.8f %12.8f %12.8f %12.3e\n',k,a,b,xr,fr); a = b; % replace the old values fa = fb; b = xr; fb = fr; if abs(b-a)< esp break end end 3. Click on Save As to save it as Secant.m. 4. Define values of xleft,xright,n in Command Window. 5. To see how it works, type Secant (xleft,xright,n); in MatLab Command Window.
4.0
LAB ASSIGNMENT
Water is flowing in a trapezoidal channel at a rate of Q = 20 m3/s. The critical depth y for such a channel must satisfy the equation
1 17
Laboratory Module
0 =1
Q2 B 3 gAc
where g = 9.81 m/s2 , Ac = the cross-sectional area (m2), and B = the width of the channel at the surface (m). For this case, the width and the cross-sectional area can be related to depth y by B=3+y and
Ac = 3 y + y2 2
Solve for the critical depth using (a) Graphical method (b) Bisection method (c) Newton-Rephson method (Hint: y is somewhere between 0.5 and 2.5 m.) Develop an M-file function so that it ensures that the relative error in the results is within 1%. Data Analysis 1) Fill up the Table 1, 2 ,3. 2) Plot f(y) against y in Figure 1 from data in Table 1. 3) Plot a graph for both (Bisection and Newton-Raphson method) results in Figure 2 (i.e. plot approximate percent relative error against n (iteration)).
5.0
1 18
Laboratory Module
Folder Name : ..
Mathematical Model
1).
Graphical Method
Table 1 f(y)
1 19
Laboratory Module
2).
Table 2 k (iteration) 1 . . . . . . n
fmid
Relative error(%)
3)
Newton-Raphson Method
Table 3 k (iteration) 1 . . . . . . n x
f(xi)
f(xi)
Relative error(%)
2 20
Laboratory Module
6.0
DISCUSSION
7.0
CONCLUSION
2 21