Sunteți pe pagina 1din 6

Chapter 1

Solution of linear and nonlinear equations

1.1
1.1.1

Functions of one variable


Direct solution of equations

b A linear equation of the form ax + b = 0 can be readily solved for x to give x = a (provided a = 0). Likewise the 2 + bx + c = 0 has solutions of the form quadratic equation ax b b2 4ac x= . (1.1) 2a

A general equation of one variable can be written in the form f (x) = 0. Values of x which satisfy this equation are called the roots of the function f . Some more complicated equations can also be manipulated to yield a solution. Example 1.1. The SuaveRedlichKwong equation of state for the pressure p, temperature T , and molar volume V is RT a p= + , (1.2) V b V (V + b) where a and b are constants and is a function of T (see Constantinides and Mostou for details of a, b, and ). This can be solved for V by rearranging it so that the left hand side is a cubic function of V , V3 RT 2 bRT a V b2 + + p p p V+ ab = 0, p (1.3)

Standard formulas for the roots of a cubic can then be applied. As with quadratic equations it is important to identify the physically meaningful root. Example 1.2. The DarcyWeisbach equation gives the pressure drop, p, for turbulent uid of density , owing with average velocity v through a pipe of length L, hydraulic diameter D, and Darcy friction factor f : p = f L v2 . D 2
1

(1.4)

In the case of a rough pipe the friction factor is characterized implicitly by the Colebrook equation f 2 = A ln B +C f 2 ,
1

(1.5)

2 where A = ln(10) , B = 3.7D for a pipe of diameter D and characteristic roughness height , and C = 2.51 for a ow with Re Reynolds number Re. Equation 1.5 is often solved numerically but can also be expressed explicitly in terms of the Lambert W0 function: 1 B B 2 f = AW0 exp (1.6) AC AC C

In this case W0 will itself be evaluated numerically, being dened implicitly as a solution to the equation z = WeW , but more commonly encountered mathematical functions such as cos and exp also require numerical evaluation. 1

Chapter 1. Solution of linear and nonlinear equations

1.1.2

Finding roots numerically

There are a range of numerical techniques which can be used to nd the roots of a function. Some examples are: Bisection method The bisection method starts with an interval then divides it in half, and repeats the procedure on the half-interval which is sure to contain a root of the function in question. Once the interval is sufciently small the location of the root has effectively been found. If f is a continuous function and the sign of f (xa ) is opposite to the sign of f (xb ) then there must be a zero in the interval (xa , xb ). By testing the sign of f (xc ), where xc = 1 (xa + xb ), we can decide whether (xa , xc ) 2 or (xc , xb ) contains a root of f and repeat the procedure on the new smaller interval. In the case that f (xc ) = 0 we have found a root. Secant method The secant method starts with two points and approximates the function in question by the line passing through the function at those two points. The intercept of this line with the x-axis gives a new approximation for the position of the root. The new point Figure 1.1 the bisection method and the second of the old points are then used to form a new linear approximation to the function, and the process is repeated until a suitably close approximation to the root is found. The line passing through the function f at two points, xn and xn+1 , is given by the equation g(x) = f (xn+1 ) + (x xn+1 ) f (xn ) f (xn+1 ) , xn xn+1 (1.7)

so the new approximation to the root, xn+2 , obtained from these points is xn+2 = xn+1 f (xn+1 ) xn xn+1 . f (xn ) f (xn+1 ) (1.8)

The procedure can then be repeated with xn+1 and xn+2 . False position The false position or linear interpolation method is similar to the secant method, except that it follows the same rules as the bisection method for the selection of initial points and the choice of which of the two current points to replace by the new approximation for the next step, ensuring that the two current points bracket a root. NewtonRaphson The NewtonRaphson method (or just Newtons method) is an extremely important method, partly because it can be readily generalized to systems of equations. It is somewhat similar to the secant method, but in this case the linear approximation is the local tangent of the function at a point single. The tangent at the point xn is obtained from the value of the function and its derivative, f (xn ) and f (xn ) as follows: g(x) = f (xn ) + f (xn ) (x xn ) . (1.9)
Figure 1.2 the secant method

This is a two-term truncated Taylor series approximation to f about the point xn . The position of the root estimated from this approximation is f (xn ) . (1.10) xn+1 == xn + xn = xn f (xn )

Chapter 1. Solution of linear and nonlinear equations The tangent at the point xn+1 is then determined and the process continued until a suitably close approximation to the root is found. Ideally the derivative, f , can be expressed analytically. When this is impossible a nite difference approximation can be used, for example f (x) f (x + h) f (x) , h (1.11)

for a suitably small value of h. Other methods There are a range of other methods which wont be discussed here, including the method of successive substitution and the Wegstein method which are suited to solving equations of the form x = g (x). Example 1.3. Use the NewtonRaphson method to nd the greater root of the function f (x) = x2 x 1 . (1.12)
Figure 1.3 the NewtonRaphson method

Of course we could simply use the quadratic formula to nd this root. To use the NewtonRaphson method we rst calculate the derivative: f (x) = 2x 1 . (1.13) Note that this indicates that the function has a minimum at x = 1 . For this function choosing an initial value which 2 1 is less than 1 will converge on the lesser root and a choice greater than 2 will converge on the greater root. (What 2 1 happens for a choice of 2 ?) The NewtonRaphson method requires the iteration xn+1 = xn x2 x 1 , 2x 1 (1.14)

so if we start with x0 = 1 we obtain the following sequence of values.


n 0 1 2 3 xn 1 2
5 3 34 21

f (xn ) 1 1
1 9 1 441

f (xn ) 1 3
7 3 47 21

xn 1 1 3
1 21 1 987

xn+1 2
5 3 34 21 1597 987

We see that the absolute value of both f (xn ) and xn decrease rapidly as we would hope when seeking the value where f (x) = 0.

1.1.3

Convergence of numerical methods

When the iterations of a numerical root-nding method are converging towards a solution it is still necessary to decide when the current value is sufciently accurate. This depends on what level of accuracy the task at had requires (or indeed allows). When using a digital computer there is always a limit to the precision with which calculated values can be stored. Approaching this level of accuracy can cause problems with an otherwise robust algorithm, and a much looser accuracy will almost always be sufcient. As the root is not known the accuracy of the current approximation is also unknown. In the case of bracketing methods an upper bound on the error is known, as the root must lie within a certain interval which decreases with each iteration. For methods such as NewtonRaphson it is usual to estimate the error from the magnitude of the change in x. We require that |xn+1 xn | = |xn | < , (1.15)

Chapter 1. Solution of linear and nonlinear equations

Figure 1.4 An example of the NewtonRaphson method failing to converge

for some small tolerance , and stop the procedure when this condition is satised. Sometimes a check on the magnitude of the function | f (xn )| is also appropriate. In some situations a root-nding method may not converge, so the stopping condition is never satised. To guard against an algorithm which does not stop it is generally a good idea to specify the maximum number of iterations which should be attempted. If this maximum is reached without satisfying the stopping condition then it may be necessary to supply a different initial choice or even use a different method. Example 1.4. Write a Matlab function which applies the secant method and use it to approximate the root of f (x) = x2 x 1. The following is the contents of a Matlab m-le which denes the function Secant(). The le should be named Secant.m. Note that the text which follows the % symbol on any line is a comment which is ignored by Matlab and could be removed without effect. function [x, fx, i] = Secant(f, x0, x1, xtol, imax) %SECANT Applies the secant method to find a root of f. % Input arguments: % f - the function for which a root is wanted, either a string % containing the function name (defined elsewhere) or a function % handle, for example @(x)(x^2 - x - 1). % x0 - the first initial guess at the root % x1 - the second initial guess at the root % xtol - the stopping condition; the current value for the root is % - accepted if abs(xa - xb) < xtol % imax - the maximum number of iterations to apply before terminating. % % Output arguments: % x - the approximate location of the root % fx - the value of the function at the root (should be ~0) % n - the number of iterations applied % Initialize the points xa and xb, and the values of f at these points. xa = x0; xb = x1; fa = feval(f, xa);

Chapter 1. Solution of linear and nonlinear equations fb = feval(f, xb); % Note: feval is a built in Matlab function for evaluating a function % supplied as a string containing the function name or a function % handle. % Set completed to false; if the stopping condition is satisfied we % will set theis to true. completed = false; % Iterate the secant method at most imax times. for i = 1:imax % Calculate the change in x required. dx = -fb*(xa - xb)/(fa - fb); % Update the values of xa, xb, fa, and fb. xa = xb; xb = xa + dx; fa = fb; fb = feval(f, xb); % Check if the stopping condition is satisfied. if ( abs(dx) < xtol ) completed = true; break; % The keyword break causes the algorithm to leave the % for-loop even if i has not reached imax yet. end end % (end of the for-loop) % Check if the for-loop stopped because the stopping condition was % satisfied. if (~completed) warning( [ Maximum iterations (imax = %d) ... reached without finding a root. ], imax); % The function warning is a built in Matlab function which prints % a warning message to the screen. It replaces %d with the % value of imax. end % Assign the output arguments (i already has a value). x = xb; fx = fb; end % (end of the function)

The function Secant() can be used in the Matlab command window so long as the le Secant.m is in the current folder or is in a folder in the list of paths dened via FileSet Path... If so the following can be typed into the Matlab command window to use the function to complete the current exercise. % Define an anonymous function handle called testFun. testFun = @(x)(x.^2 - x - 1); % Call the function Secant. [xRoot, fValue, iterations] = Secant(testFun, 1.0, 2.0, 1E-8, 10);

Chapter 1. Solution of linear and nonlinear equations % Display the value of xRoot in the command window. disp(xRoot);

Following the function call the variables xRoot, fValue, and iterations will be available in the Matlab workspace where they can be examined without using disp(). The function handle, dened using the symbol @, allows functions to be essentially stored as a variable and passed to other functions. An alternative method is to create another m-le named testFun.m, containing the following. function y = testFun(x) y = x.^2 - x - 1; end Now Secant() can be called from the Matlab console using the name of the function in a string (that is, an list of characters) which is written within single quotes. [xRoot, fValue, iterations] = Secant(testFun, 1.0, 2.0, 1E-8, 10);

S-ar putea să vă placă și