Chapra - Applied Numerical Methods MATLAB Engineers Scientists 3rd-Halaman-182-191
Chapra - Applied Numerical Methods MATLAB Engineers Scientists 3rd-Halaman-182-191
function [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
FIGURE 6.7
An M-file to implement the Newton-Raphson method.
We should mention that although this derivative is not difficult to evaluate in principle, it
involves a bit of concentration and effort to arrive at the final result.
The two formulas can now be used in conjunction with the function newtraph to
evaluate the root:
>> y = @m sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;
>> dy = @m 1/2*sqrt(9.81/(m*0.25))*tanh((9.81*0.25/m) ...
^(1/2)*4)-9.81/(2*m)*sech(sqrt(9.81*0.25/m)*4)^2;
>> newtraph(y,dy,140,0.00001)
ans =
142.7376
inconvenient to evaluate. For these cases, the derivative can be approximated by a back-
ward finite divided difference:
f (xi−1 ) − f (xi )
f (xi ) ∼
=
xi−1 − xi
This approximation can be substituted into Eq. (6.6) to yield the following iterative
equation:
f (xi )(xi−1 − xi )
xi+1 = xi − (6.8)
f (xi−1 ) − f (xi )
Equation (6.8) is the formula for the secant method. Notice that the approach requires two
initial estimates of x. However, because f (x) is not required to change signs between the
estimates, it is not classified as a bracketing method.
Rather than using two arbitrary values to estimate the derivative, an alternative ap-
proach involves a fractional perturbation of the independent variable to estimate f (x),
δxi f (xi )
xi+1 = xi − (6.9)
f (xi + δxi ) − f (xi )
We call this the modified secant method. As in the following example, it provides a nice
means to attain the efficiency of Newton-Raphson without having to compute derivatives.
10−6 (50)(−4.57938708)
x1 = 50 −
−4.579381118 − (−4.57938708)
= 88.39931(|εt | = 38.1%; |εa | = 43.4%)
cha01102_ch06_151-181.qxd 12/17/10 8:05 AM Page 163
Second iteration:
x1 = 88.39931 f (x1 ) = −1.69220771
x1 + δx1 = 88.39940 f (x1 + δx1 ) = −1.692203516
10−6 (88.39931)(−1.69220771)
x2 = 88.39931 −
−1.692203516 − (−1.69220771)
= 124.08970(|εt | = 13.1%; |εa | = 28.76%)
The calculation can be continued to yield
i xi |εt|, % |εa|, %
0 50.0000 64.971
1 88.3993 38.069 43.438
2 124.0897 13.064 28.762
3 140.5417 1.538 11.706
4 142.7072 0.021 1.517
5 142.7376 4.1 × 10−6 0.021
6 142.7376 3.4 × 10−12 4.1 × 10−6
The choice of a proper value for δ is not automatic. If δ is too small, the method can be
swamped by round-off error caused by subtractive cancellation in the denominator of
Eq. (6.9). If it is too big, the technique can become inefficient and even divergent. How-
ever, if chosen correctly, it provides a nice alternative for cases where evaluating the
derivative is difficult and developing two initial guesses is inconvenient.
Further, in its most general sense, a univariate function is merely an entity that returns
a single value in return for values sent to it. Perceived in this sense, functions are not
always simple formulas like the one-line equations solved in the preceding examples in this
chapter. For example, a function might consist of many lines of code that could take a sig-
nificant amount of execution time to evaluate. In some cases, the function might even rep-
resent an independent computer program. For such cases, the secant and modified secant
methods are valuable.
FIGURE 6.8
Comparison of (a) the secant method and (b) inverse quadratic interpolation. Note that the
approach in (b) is called “inverse” because the quadratic function is written in y rather than in x.
f (x) f (x)
x x
(a) (b)
cha01102_ch06_151-181.qxd 12/17/10 8:05 AM Page 165
y
6
x f(y)
4
y f(x)
2
Root
0
2 4 x
FIGURE 6.9
Two parabolas fit to three points. The parabola written as a function of x, y f(x), has complex
roots and hence does not intersect the x axis. In contrast, if the variables are reversed, and the
parabola developed as x f(y), the function does intersect the x axis.
As we will learn in Sec. 18.2, this form is called a Lagrange polynomial. The root, xi1, cor-
responds to y 0, which when substituted into Eq. (6.10) yields
yi−1 yi yi−2 yi
xi+1 = xi−2 + xi−1
(yi−2 − yi−1 )(yi−2 − yi ) (yi−1 − yi−2 )(yi−1 − yi )
yi−2 yi−1
+ xi (6.11)
(yi − yi−2 )(yi − yi−1 )
As shown in Fig. 6.9, such a “sideways” parabola always intersects the x axis.
Solution. By reversing the x’s and y’s, Eq. (6.10) can be used to generate a quadratic in x
as
(x − 2)(x − 4) (x − 1)(x − 4) (x − 1)(x − 2)
f (x) = 2+ 1+ 5
(1 − 2)(1 − 4) (2 − 1)(2 − 4) (4 − 1)(4 − 2)
or collecting terms
f (x) = x 2 − 4x + 5
166 ROOTS: OPEN METHODS
This equation was used to generate the parabola, y f(x), in Fig. 6.9. The quadratic for-
mula can be used to determine that the roots for this case are complex,
4 ± (−4)2 − 4(1)(5)
x= =2±i
2
Equation (6.10) can be used to generate the quadratic in y as
or collecting terms:
g(y) = 0.5x 2 − 2.5x + 4
Finally, Eq. (6.11) can be used to determine the root as
Before proceeding to Brent’s algorithm, we need to mention one more case where
inverse quadratic interpolation does not work. If the three y values are not distinct (i.e.,
yi–2 yi–1 or yi–1 yi), an inverse quadratic function does not exist. So this is where the
secant method comes into play. If we arrive at a situation where the y values are not distinct,
we can always revert to the less efficient secant method to generate a root using two of
the points. If yi2 yi1, we use the secant method with xi–1 and xi. If yi–1 yi, we use xi–2
and xi–1.
function b = fzerosimp(xl,xu)
a = xl; b = xu; fa = f(a); fb = f(b);
c = a; fc = fa; d = b - c; e = d;
while (1)
if fb == 0, break, end
if sign(fa) == sign(fb) %If needed, rearrange points
a = c; fa = fc; d = b - c; e = d;
end
if abs(fa) < abs(fb)
c = b; b = a; a = c;
fc = fb; fb = fa; fa = fc;
end
m = 0.5*(a - b); %Termination test and possible exit
tol = 2 * eps * max(abs(b), 1);
if abs(m) <= tol | fb == 0.
break
end
%Choose open methods or bisection
if abs(e) >= tol & abs(fc) > abs(fb)
s = fb/fc;
if a == c %Secant method
p = 2*m*s;
q = 1 - s;
else %Inverse quadratic interpolation
q = fc/fa; r = fb/fa;
p = s * (2*m*q * (q - r) - (b - c)*(r - 1));
q = (q - 1)*(r - 1)*(s - 1);
end
if p > 0, q = -q; else p = -p; end;
if 2*p < 3*m*q - abs(tol*q) & p < abs(0.5*e*q)
e = d; d = p/q;
else
d = m; e = m;
end
else %Bisection
d = m; e = m;
end
c = b; fc = fb;
if abs(d) > tol, b=b+d; else b=b-sign(b-a)*tol; end
fb = f(b);
end
FIGURE 6.10
Function for Brent’s root-finding algorithm based on a MATLAB M-file developed by Cleve Moler
(2005).
168 ROOTS: OPEN METHODS
criteria are met, the loop is terminated. Otherwise, a decision structure chooses among the
three methods and checks whether the outcome is acceptable. A final section then evaluates
f at the new point and the loop is repeated. Once the stopping criteria are met, the loop
terminates and the final root estimate is returned.
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])
If we want to find the positive root, use a guess that is near it:
>> x = fzero(@(x) x^2-9,4)
x =
3
x =
-3
If we wanted to ensure that we found the positive root, we could enter two guesses as in
>> x = fzero(@(x) x^2-9,[0 4])
x =
3
Also, if a sign change does not occur between the two guesses, an error message is displayed
>> x = fzero(@(x) x^2-9,[-4 4])
??? Error using ==> fzero
The function values at the interval endpoints must ...
differ in sign.
cha01102_ch06_151-181.qxd 12/17/10 8:05 AM Page 169
The fzero function works as follows. If a single initial guess is passed, it first performs
a search to identify a sign change. This search differs from the incremental search described
in Section 5.3.1, in that the search starts at the single initial guess and then takes increasingly
bigger steps in both the positive and negative directions until a sign change is detected.
Thereafter, the fast methods (secant and inverse quadratic interpolation) are used un-
less an unacceptable result occurs (e.g., the root estimate falls outside the bracket). If an
unacceptable result happens, bisection is implemented until an acceptable root is obtained
with one of the fast methods. As might be expected, bisection typically dominates at first
but as the root is approached, the technique shifts to the faster methods.
A more complete representation of the fzero syntax can be written as
[x,fx] = fzero(function,x0,options,p1,p2,...)
where [x,fx] = a vector containing the root x and the function evaluated at the root fx,
options is a data structure created by the optimset function, and p1, p2... are any
parameters that the function requires. Note that if you desire to pass in parameters but not
use the options, pass an empty vector [] in its place.
The optimset function has the syntax
options = optimset('par 1 ',val 1 ,'par 2 ',val 2 ,...)
where the parameter pari has the value vali. A complete listing of all the possible param-
eters can be obtained by merely entering optimset at the command prompt. The parame-
ters that are commonly used with the fzero function are
display: When set to 'iter' displays a detailed record of all the iterations.
tolx: A positive scalar that sets a termination tolerance on x.
Thus, after 25 iterations of searching, fzero finds a sign change. It then uses interpo-
lation and bisection until it gets close enough to the root so that interpolation takes over and
rapidly converges on the root.
Suppose that we would like to use a less stringent tolerance. We can use the optimset
function to set a low maximum tolerance and a less accurate estimate of the root results:
>> options = optimset ('tolx', 1e-3);
>> [x,fx] = fzero(@(x) x^10-1,0.5,options)
x =
1.0009
fx =
0.0090
6.6 POLYNOMIALS
Polynomials are a special type of nonlinear algebraic equation of the general form
where n is the order of the polynomial, and the a’s are constant coefficients. In many (but
not all) cases, the coefficients will be real. For such cases, the roots can be real and/or com-
plex. In general, an nth order polynomial will have n roots.
Polynomials have many applications in engineering and science. For example,
they are used extensively in curve fitting. However, one of their most interesting and
powerful applications is in characterizing dynamic systems—and, in particular, linear
systems. Examples include reactors, mechanical devices, structures, and electrical
circuits.