e3-roots of equations

Upload: ahmad-zubair-hj-yahaya

Post on 14-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 e3-Roots of Equations

    1/21

    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 numericalcalculation, giving a table of numbers and/or graphical representation (figures). The stepsfrom a given situation (in engineering, economics, etc) to the final answer are usually asfollows.

    1. Modeling. We set up a mathematical model of the problem, such as anintegral, a system of equations, or a differential equation.

    2. Choice of mathematical methods, perhaps together with a preliminary errorestimation, 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 maydecide to use a computing environment, such as MAPLE, MATLAB, andMATHCAD.

    4. Doing the computation5. 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 involvingengineering applications.

    3.1. Roots of EquationsMany engineering analyses require the determination of the value(s) of the

    variablexthat satisfies a nonlinear equation:

    0)( =xf (3.1)

    The values ofxthat satisfy Eq.(3.1) are known as the root of Equation (3.1); the numberof roots may be finite or infinite and may be real or complex depending on the nature ofthe equation and the physical problem. The function f(x) may or may not be available inexplicit 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

  • 7/27/2019 e3-Roots of Equations

    2/21

    ENT 258 Numerical Analysis Laboratory Module

    0120804 =+ xx .(polynomial equation) (3.2)

    and

    .0tanhtan = xx (transcendental equation). (3.3)

    The roots of Eq. (3.1) are also known as the zeros of the function f(x). Thus, a function issaid to have zeros, while an equation is said to have roots. A transcendental function isone whose value cannot be determined for any specified value of the argument by a finitenumber of additions, subtraction, multiplications, or divisions. Exponential, logarithmic,trigonometric, and hyperbolic functions are examples of transcendental functions. Anyequation containing transcendental function is called a transcendental equation.

    There are two major classes of methods available to find the approximate values ofthe roots in nonlinear equation. They are

    Bracketing methods. As the name implies, these are based on two initial guessesthat 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 openmethods do not always work (i.e., they can diverge), but when they do they usuallyconverge quicker. In both cases, initial guesses are required.

    3.2. Stopping CriteriaSuppose {xn} is a sequence, converging to a limitx*. The limitx*has the property f(x*)=0.Let tol be a positive number

    3.3. MATLAB Built-in Function

    3.3.1. fzero is designed to find the real root of a single equation. A simplerepresentation 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

    Absolute error (inx)

    |xn x*| < tol

    Absolute error in f|f(xn)| < tol

    Relative error (inx)|xn x*| /|xn| < tol

  • 7/27/2019 e3-Roots of Equations

    3/21

    ENT 258 Numerical Analysis Laboratory Module

    where x0 and x1 are guesses that bracket a sign change.

    Example 3.1

    To find the root of equation 9)(2

    =xxf

    >> 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 thepolynomials coefficients.

    Example 3.2Determine all the roots of polynomial:

    25.1875.315.275.25.3)(2345

    +++= xxxxxxf

    >> a = [ 1 -3.5 2.75 2.125 -3.875 1.25]>> x = roots (a)

    x =

    2.0000-1.00001.0000 + 0.5000i1.0000 - 0.5000i0.5000

    3.4. Graphical MethodsA 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 thexaxis. This point, which represents the x

    value for which f(x)=0, provides a rough approximation of the root.

    Example 3.3Use the graphical approach to determine the mass of the bungee jumper with a dragcoefficient of 0.25 kg/m to have a velocity of 36 m/s after 4 s of free fall. Note: Theacceleration of gravity is 9.81 m/s2.

    )tanh( tm

    gc

    c

    gmv d

    d

    =

    Solution

    33

  • 7/27/2019 e3-Roots of Equations

    4/21

    ENT 258 Numerical Analysis Laboratory Module

    1. Changes the equation to be f(v) = 0. So, the equation to be

    vtm

    gc

    c

    gmvf d

    d

    = tanh)( = 0

    2. Write the following MATLAB session sets up a plot off(m) versus mass:

    50 100 150 200-5

    -4

    -3

    -2

    -1

    0

    1

    The function crosses the m axis between 140 and 150 kg.

    3.5 Bracketing Method

    3.5.1 Bisection MethodIn 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 ofxuntil two successive functionvalues are found with opposite signs.

    Let a =xk and b =xk+1 be the values ofxat which the function values ofxat which thefunction values f(a) and f(b) have apposite signs. This implies that the function has a rootbetween a =xk and b =xk+1.

    2

    baxmid

    += (3.4)

    The interval (xk, xk+1), in which the root is expected to lie, is called the interval ofuncertainty. The midpoint of the current interval of uncertainty (a, b) is computed as andthe function value f(xmid) is determined. If f(xmid) = 0, xmid will be a root of f(x) = 0. Iff(xmid) 0, then the sign of f(xmid) will coincide with that of f(a) or f(b) . If the signs of f(xmid) andf(a) coincide, then a is replaced byxmid . Otherwise (that is, if the signs of f(xmid) and f(b)coincide), b is replaced byxmid.

    Thus the interval of uncertainty is reduced to half of its original value. Again themidpoint of the current interval of uncertainty is computed using Eq.(3.4), and theprocedure is repeated until a specified convergence criterion is satisfied. The reduction of

    44

  • 7/27/2019 e3-Roots of Equations

    5/21

    ENT 258 Numerical Analysis Laboratory Module

    the interval of uncertainty (i.e., the progress of the iterative process) is shown in (). Thefollowing convergence criterion can be used to stop the iterative procedure:

    )(mid

    xf or

  • 7/27/2019 e3-Roots of Equations

    6/21

    ENT 258 Numerical Analysis Laboratory Module

    66

    No

    Seta(i) = a, b(i) = b

    iteration number =ntolerance =

    xmid =2

    )()( ii ba +, f(xmid)

    Yes f(a(i))*f(b(i)) > 0

    sign f(xmid

    ) ~ =sign

    f(a(i))

    sign f(xmid

    ) = =sign

    f(a(i))

    No

    Yes

    b(i+1) =xmida(i+1) = a(i)

    i> n

    i= 1

    i= i+ 1

    No

    Yes

    STOP

    abs (a(i+1)-b(i+1)) 0

    disp ('There are no roots in the interval')return

    end

    fprintf(' k a xmid b f(xmid) h\n');

    for k=1:n

    xrold=xr;xr = (a + b)/2; %computing the midpointfr = feval(f,xr); % f(x) at midpointh = abs((xr-xrold)/xr)*100;if h< esp

    breakend

    fprintf('%3d %12.8f %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

    77

    STOP Yes

  • 7/27/2019 e3-Roots of Equations

    8/21

    ENT 258 Numerical Analysis Laboratory Module

    b = xr;fb = fr;

    endend

    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 methodor regula falsi method is a root-findingalgorithmthat combines features from the bisection methodand the secant method.

    Like the bisection method, the false position method starts two points a0 and b0such that f(a0) and f(b0) are of opposite signs, which implies by the intermediate valuetheorem that the function f has a root in the interval [a0, b0]. The method proceeds byproducing a sequence of shrinking intervals [ak, bk] that all contain a root off.

    At iteration numberi, the number

    ( ) ( )

    ( ) ( )ii

    iiiii

    afbf

    afbbfac

    = , 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 = ciand bi+1 = bi, otherwise we

    set ai+1 = ai and bi+1 = ci. This process is repeated until the root is approximated sufficientlywell.

    Example 3.5

    Find the root of equation xexf 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 andM-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

    % esp= value of tolerance

    %

    % Output: x = estimate of the root

    fa = feval(f,a); % Initial values of f(a) and f(b)fb = feval(f,b);

    88

    http://en.wikipedia.org/wiki/Numerical_analysishttp://en.wikipedia.org/wiki/Root-finding_algorithmhttp://en.wikipedia.org/wiki/Root-finding_algorithmhttp://en.wikipedia.org/wiki/Root-finding_algorithmhttp://en.wikipedia.org/wiki/Bisection_methodhttp://en.wikipedia.org/wiki/Bisection_methodhttp://en.wikipedia.org/wiki/Secant_methodhttp://en.wikipedia.org/wiki/Secant_methodhttp://en.wikipedia.org/wiki/Intermediate_value_theoremhttp://en.wikipedia.org/wiki/Intermediate_value_theoremhttp://en.wikipedia.org/wiki/Numerical_analysishttp://en.wikipedia.org/wiki/Root-finding_algorithmhttp://en.wikipedia.org/wiki/Root-finding_algorithmhttp://en.wikipedia.org/wiki/Bisection_methodhttp://en.wikipedia.org/wiki/Secant_methodhttp://en.wikipedia.org/wiki/Intermediate_value_theoremhttp://en.wikipedia.org/wiki/Intermediate_value_theorem
  • 7/27/2019 e3-Roots of Equations

    9/21

    ENT 258 Numerical Analysis Laboratory Module

    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 midpointerr = abs((xr-xrold)/xr)*100;if err< espbreak

    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 Window5. 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 equationf(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

  • 7/27/2019 e3-Roots of Equations

    10/21

    ENT 258 Numerical Analysis Laboratory Module

    Flowchart of Fixed-Point Iteration Method

    Example 3.6

    Find the root of equation xexf x = )( using the Fixed Point Iteration method with x0 =

    1 and =10-3

    Procedure-MATLAB Programming

    101

    Setx

    i(initial guess)

    iteration number =ntolerance =

    g(xi)

    i> n

    i= 0

    i= i+ 1

    No

    Yes

    STOP

    abs (xi+1

    -xi)