e3-roots of equations

advertisement
ENT 258 Numerical Analysis
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.0 INTRODUCTION & THEORY
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.
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
ENT 258 Numerical Analysis
Laboratory Module
4
x

80
x
120

0
.(polynomial equation)
(3.2)
tan
xtanh
x0
.(transcendental equation).
(3.3)
and
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.
MATLAB Built-in Function
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:
22
ENT 258 Numerical Analysis
Laboratory Module
fzero(function,[x0,x1]
where x0 and x1 are guesses that bracket a sign change.
Example 3.1
2
To find the root of equation f(x)x 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
polynomial’s coefficients.
Example 3.2
Determine all the roots of polynomial:
5
4
3
2
f
(
x
)

x

3
.
5
x

2
.
75
x

2
.
15
x

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

gm gc
d
tanh(
t)
c
m
d
33
ENT 258 Numerical Analysis
Laboratory Module
Solution
1. Changes the equation to be f(v) = 0. So, the equation to be
gc

gm
 d

f
(
v
)
 tanh
t

v
m
 =0
c
d


2. Write the following MATLAB session sets up a plot of f(m) versus mass:
1
0
-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.
xmid
ab
2
(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.
44
ENT 258 Numerical Analysis
Laboratory Module
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
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 
(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 ( xmid ) ≤ε or bi ai 
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
ENT 258 Numerical Analysis
Laboratory Module
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=1
i>n
STOP
i=i+1
No
xmid =
a(i) b(i)
2
, f(xmid)
Yes
a(i+1) = xmid
b(i+1) = b(i)
sign f(xmid) = = sign
f(a(i))
No
sign f(xmid) ~ = sign
f(a(i))
b(i+1) = xmid
a(i+1) = a(i)
66
ENT 258 Numerical Analysis
Laboratory Module
b(i+1), a(i+1)
STOP
Yes
No
abs (a(i+1)-b(i+1)) <
ε
Example 3.4
x
Find the root of equation f(x)e 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.
2. Write the program given below in M-file
function xm =Bisection(f,xleft,xright,n,esp)
%
Input:
xleft,xright
=left and right brackets of the root
%
n
=number of iterations
%
esp
=value of tolerance
%
%
Output:
x = estimate of the root
a = xleft;
b =xright;
fa = feval(f,a);
% Initial values of f(a) and f(b)
fb = feval(f,b);
xr=a;
if fa*fb > 0
disp ('There are no roots in the interval')
return
end
fprintf('
\n');
k
a
xmid
b
f(xmid)
h
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
77
ENT 258 Numerical Analysis
fprintf('%3d
%12.8f
Laboratory Module
%12.8f
%12.8f
%12.3e %12.8f\n',k,a,xr,b,fr,h);
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 Bisection.m.
4. Define equation using inline function in MatLab Command Window
5. To see how it works, type Bisection (f,0,1,20,0.001) in MatLab Command Window.
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
a
b

b
a
if
i
if
i
c
,
i
f
b

f
a
i
i
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
x
Find the root of equation f(x)e 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.
2. Write the program given below in M-file
function xm =False(xleft,xright,n,esp)
%
%
Input:
xleft,xright = left and right brackets of the root
n = (optional) number of iterations
88
ENT 258 Numerical Analysis
%
%
%
Laboratory Module
esp= value of tolerance
Output:
x = estimate of the root
fa = feval(f,a);
fb = feval(f,b);
% Initial values of f(a) and f(b)
if fa*fb > 0
disp ('There are not roots in interval')
return
end
fprintf(' k
a
xmid
b
f(xmid)\n');
for k=1:n
xr = ((a*fb)-(b*fa))/(fb-fa); %computing the midpoint
fr = feval(f,xr); % f(x) at midpoint
err = abs((xr-xrold)/xr)*100;
if err< esp
break
end
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.
3.6 Open Method
3.6.1
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, ………
99
ENT 258 Numerical Analysis
Laboratory Module
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.
Flowchart of Fixed-Point Iteration Method
Start
iteration number =n
tolerance = ε
Set
xi(initial guess)
i=0
Yes
i>n
STOP
i=i+1
No
g(xi)
xi+1 = g(xi)
Yes
STOP
No
abs (xi+1-xi) <
ε
110
0
ENT 258 Numerical Analysis
Laboratory Module
Example 3.6
x
Find the root of equation f(x)e x using the Fixed Point Iteration method with x0 =
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.
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 Taylor’s series expansion of the function f(x)
about an arbitrary point x1 is approximated as
f
(
x
)

f
(
x
)

(
x

x
)
f'(
x
)
1
1
1
(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(
x
)

(
x

x
)
f'(
x
)

0
1
1
1
(3.7)
111
1
ENT 258 Numerical Analysis
Laboratory Module
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
f(x
)
1
x
x
x
2
1 '
f (x
)
1
(3.8)
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
f(
x
i)
x

x
:
i
1
,
2
,....
i

1
i
'
f(
x
i)
(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)
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.
112
2
ENT 258 Numerical Analysis
Laboratory Module
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
f(x
)
x
x i
i
1 =
i f'(x
i)
3. Find the absolute relative approximate , as
xi1 xi  or f (xi1) 
4. Also check if the number of iterations has exceeded the maximum number of iterations
Flowchart of Newton-Raphson Method
Start
iteration number =n
tolerance = ε
Set
xi(initial guess)
Yes
i=1
i>n
STOP
i=i+1
No
f(xi), f’(xi)
f(x
i)
x
x
i
1
i
f'(x
i)
No
Yes
STOP
abs (xi+1-xi) < ε
113
3
ENT 258 Numerical Analysis
Laboratory Module
Example 3.7
x
Find the root of equation f(x)e x using the Newton-Raphson method with starting
point x1 = 0.0 and the convergence criterion ε =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.
2. Write the program given below in M-file
function xm =Newton(x0,n,esp)
% Input:
%
%
% Output:
x0
n
= initial guess
= number of iterations; default: n =8
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 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 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(
x
)

f(
x
)
i

1
f'(
x
 i
i)
x
x
i
i

1
The general expression for the iterative process can then be written as
114
4
ENT 258 Numerical Analysis
Laboratory Module
f
(
x
)
f
(
x
)[
x

x
]
i
i
i
i

1
x

x


x

;
i

2
,
3
,
4
,...
i

1
i
i
'
(
x
)

f
(
x
)
f
(
x
) f
i
i

1
i
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
f(
x
x
x
)
i)(
i
i

1
x

x
i

1
i
f(
x

f(
x
)
i)
i

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.
115
5
ENT 258 Numerical Analysis
Laboratory Module
Flowchart of Secant Method
Start
iteration number =n
tolerance = ε
Set
xi-1 = a, xi = b
f(xi-1), f(xi)
No
Yes
i=1
i>n
STOP
i=i+1
No
f(
x
x
x
)
i)(
i
i

1
x

x
i

1
i
f(
x

f(
x
), f(xi+1)
i)
i

1
xi 1  xi
xi  xi 1
Yes
STOP
abs (xi+1-xi) < ε
No
116
6
ENT 258 Numerical Analysis
Laboratory Module
Example 3.8
x
Find the root of equation f(x)e x using the secant 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.
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.
117
7
ENT 258 Numerical Analysis
4.0
Laboratory Module
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
2
Q
01 3 B
gA
c
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
y2
A
y
c 3
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)).
118
8
ENT 258 Numerical Analysis
5.0
Laboratory Module
DATA & RESULTS
Lab Name
: …………………..
Pc Number : …………………..
Folder Name : …………………..
Mathematical Model
1).
Graphical Method
Table 1
f(y)
y
119
9
ENT 258 Numerical Analysis
Laboratory Module
Figure 1: f(y) against y
2).
Bisection Method
Table 2
k (iteration)
xlower
Xupper
xmid
fmid
f’(xi)
f(xi)
Relative
error(%)
Relative
error(%)
1
.
.
.
.
.
.
n
3)
Newton-Raphson Method
Table 3
k (iteration)
x
1
.
.
.
.
.
.
n
220
0
ENT 258 Numerical Analysis
Laboratory Module
Figure 2: Relative error(%) against n
6.0
DISCUSSION
7.0
CONCLUSION
221
1
Download