Ch1-1

advertisement
1 Chapter 1
Solutions of Equations in One Variable
1.1 Graphical Methods
A simple method for obtaining a root of the equation f(x) = 0 is to plot the function and
observe where it crosses the x axis. There are many available software that will facilitate
making a plot of a function. I will use Matlab exclusively for the course notes, however you
can use another software such as Excel or Matcad for your work.
Example 1.1
Solve f(x) =
600
(1  e0.15x)  50 = 0 using the graphical method.
x
Solution
The function f(x) =
600
(1  e0.15x)  50 can be plotted in Figure 1.1 using the following
x
Matlab statements.
% Graphical method to solve 600*(1-exp(-0.15x))/x - 50
%
x=4:.1:20;
fx=600*(1-exp(-.15*x))./x-50;
plot(x,fx,[0 20],[0 0])
xlabel('x');ylabel('f(x)')
grid;zoom on
20
15
10
5
f(x)
0
-5
-10
-15
-20
-25
0
2
4
6
8
10
x
12
14
16
18
20
Figure 1.1 The graphical method for roots finding.
1-1
The Matlab Zoom on statement allows the function to be zoomed in at the cursor with left
mouse click ( right mouse click will zoom out). Each time you click, the axes limits will be
changed by a factor of 2 (in or out). You can zoom in as many times as necessary for the
desired accuracy. Figure 1.2 shows the approximate root x to be 8.79.
0.06
0.04
0.02
f(x)
0
-0.02
-0.04
-0.06
-0.08
8.75
8.76
8.77
8.78
8.79
x
8.8
8.81
8.82
Figure 1.2 The graphical method for roots finding with Matlab Zoom on.
The plot of a function between x1 and x2 is important for understanding its behavior within
this interval. More than one roots can occur within the interval when f(x1) and f(x2) are on
opposite sides of the x axis. The roots can also occur within the interval when f(x1) and f(x2)
are on the same sides of the x axis. Since the functions that are tangent to the x axis satisfy
the requirement f(x) = 0 at this point, the tangent point is called a multiple root.
1.2 The Bisection Method
The bisection method or interval halving can be used to determine the solution to f(x) = 0 on
an interval [x1 = a, x2 = b] if f(x) is real and continuous on the interval and f(x1) and f(x2)
have opposite signs. We assume for simplicity that the root in this interval is unique. The
location of the root is then calculated as lying at the midpoint of the subinterval within which
the functions have opposite signs. The process is repeated to any specified accuracy. The
procedure can be summarized in the following steps
Let f(x1) f(x2) < 0 on an interval [x1 = a, x2 = b]
1
Step 1
Let xx = (x1 + x2); f1 = f(x1); f2 = f(x2)
2
Step 2
Evaluate fx = f(xx)
If fx f1 > 0 then
x1 = xx; f1 = fx
else
x2 = xx; f2 = fx
1-2
If abs(x2  x1) > an error tolerance, go back to Step 1
Step 3
15
10
f(x)
5
x5
x1 = a
x2=b
x3
0
x4
-5
-10
-15
4
6
8
10
x
12
14
16
Figure 1.3 The first three iterations x3, x4, and x5 of the bisection method.
Figure 1.3 shows first three iterations x3, x4, and x5 of the bisection method.
x3 =
1
1
(x1 + x2) = (6 + 14) = 10
2
2
f(x1) f(x3) < 0  x4 =
1
1
(x1 + x3) = (6 + 10) = 8
2
2
f(x3) f(x4) < 0  x5 =
1
1
(x3 + x4) = (10 + 8) = 9
2
2
Since f(x1) and f(x2) bracket the root and x3 =
1
1
(x1 + x2) = (a + b), the error after the first
2
2
1
(b  a). Let x be the actual root of f(x) and xn be the
2
approximate root after n iterations, then
iteration is less than or equal to
| xn  x| 
ba
2n
1-3
The above formula can be used to determine the number of iterations n required to find the
root within a specified error. For example, if we want to find the root of f(x) with | xn  x| 
10-3 within the interval (b  a) = 1 then the number of iterations n is determined from the
following steps
| xn  x| 
ba
2n
ba
1
1
= n  2n  3 = 103
n
2
10
2
3
ln( 10 )
n
= 9.97 = 10
ln 2
10-3 
We need 10 iterations to reduce the error to 10-3. A Matlab program for the bisection method
is listed in Table 1.1 where the function f(x) is an input to the program. The statement eval(f)
is used to evaluate the function at a given value of x.
Table 1.1 __________________________________
% Bisection Method
f=input('f(x)=','s');
tol=input('error tolerance =1e-5, new tolerance=');
if length(tol)==0,tol=1e-5;end
x1=input(' First guess=');
x=x1;
f1=eval(f);
x2=input(' Second guess=');
x=x2;
f2=eval(f);
if f1*f2<0
for i=1:101
x=(x1+x2)/2;
fx=eval(f);
if fx*f1>0
x1=x;f1=fx;
else
x2=x;f2=fx;
end
if abs(x2-x1)<tol, break,end
end
if i>100,disp('exeed 100 iterations'),end
fprintf('number of iterations = %g\n',i)
fprintf('root = %g\n',x)
else
disp('Two guessess do not bracket the root')
end
1-4
Download