Lec 8 Secant

Embed Size (px)

Citation preview

  • 7/27/2019 Lec 8 Secant

    1/24

    Roots of Nonlinear Equations

    Topic: Secant Method

    Dr. Nasir M Mirza

    Numerical Methods

    Email: [email protected]

  • 7/27/2019 Lec 8 Secant

    2/24

    Secant Method

    )(xf

    )f(x-= xx

    i

    iii

    1

    f(x)

    f(xi)

    f(xi-1)

    xi+2 xi+1 xiX

    ii xfx ,

    1

    1 )()()(

    ii

    ii

    i

    xx

    xfxfxf

    )()(

    ))((

    1

    11

    ii

    iiiii

    xfxf

    xxxfxx

    Newton-Raphson Method

    Approximate the derivative

  • 7/27/2019 Lec 8 Secant

    3/24

    Secant Method

    )()(

    ))((

    1

    11

    ii

    iiiii

    xfxf

    xxxfxx

    Geometric Similar Trianglesf(x)

    f(xi)

    f(xi-1)

    xi+1 xi-1 xiX

    B

    C

    E D A

    11

    1

    1

    )()(

    ii

    i

    ii

    i

    xx

    xf

    xx

    xf

    DE

    DC

    AE

    AB

  • 7/27/2019 Lec 8 Secant

    4/24

    Algorithm for Secant MethodTo find a solution to f(x) = 0 given initial guess as x0 and x1.

    INPUT: initial guess x0 nd x1, max. number of iterations (N) andtolerance (TOL).

    OUTPUT: approximate solution

    Step 1: Set i = 1;

    y0 = f(x0); y1 = f(x1);Step 2: while i N do step 3 to 6

    Step 3: Set xi+1 = xiyi(xixi-1)/(yiyi-1)

    Step 4: If | xi+1xi | < TOL then

    OUTPUT ( xi+1 ); procedure completed successfully

    STOP

    Step 5: Set i = i+1

    Step 6: Set xi = xi+1 ; yi = f(xi);

    Step 7: OUTPUT: Method failed after N iterations

    STOP

  • 7/27/2019 Lec 8 Secant

    5/24

    Step 1

    010x

    1

    1

    x

    - xx=

    i

    iia

    Calculate the next estimate of the root from two initialguesses

    Find the absolute relative approximate error

    )()(

    ))((

    1

    11

    ii

    iiiii

    xfxf

    xxxfxx

  • 7/27/2019 Lec 8 Secant

    6/24

    Step 2

    Find if the absolute relative approximate error is greaterthan the pre-specified relative error tolerance.

    If so, go back to step 1, else stop the algorithm.

    Also check if the number of iterations has exceeded themaximum number of iterations.

  • 7/27/2019 Lec 8 Secant

    7/24

    Example

    You are working for DOWN THE TOILET COMPANY thatmakes floats for ABC commodes. The ball has aspecific gravity of 0.6 and has a radius of 5.5 cm. Youare asked to find the distance to which the ball will getsubmerged when floating in water.

  • 7/27/2019 Lec 8 Secant

    8/24

    Solution

    The equation that gives the depth x to which the ball issubmerged under water is given by

    Use the Secant method offinding roots of equations tofind the depth x to which theball is submerged under water.

    Conduct three iterations toestimate the root of the aboveequation.

    423 10x99331650 -.+x.-xxf

  • 7/27/2019 Lec 8 Secant

    9/24

    Graph of function f(x)

    -0.02 0.00 0.02 0.04 0.06 0.08 0.10 0.12

    -0.0004

    -0.0003

    -0.0002

    -0.0001

    0.0000

    0.0001

    0.0002

    0.0003

    0.0004

    0.0005

    f(x)

    x

    4

    23

    10x9933

    1650

    -.+

    x.-xxf

  • 7/27/2019 Lec 8 Secant

    10/24

    Iteration #1

    %61.22

    0.06461

    05.0,02.0

    1

    10

    100

    01

    01

    a

    x

    xfxf

    xxxfxx

    xx

  • 7/27/2019 Lec 8 Secant

    11/24

    Iteration #2

    %525.3

    06241.0

    x

    06461.0,05.0

    2

    01

    011

    12

    10

    a

    x

    xff

    xxxfxx

    xx

  • 7/27/2019 Lec 8 Secant

    12/24

  • 7/27/2019 Lec 8 Secant

    13/24

    Secant Method algorithm

    Do while |x2 - x1| >= tolerance value 1

    or |f(x3)|>= tolerance value 2

    Set x3 = x2 - f(x2)*(x2 - x1)/(f(x2)-f(x1))

    Set x1 = x2;

    Set x2= x

    3;

    END loop

  • 7/27/2019 Lec 8 Secant

    14/24

    Example 2:

    Let us solve thefollowing problem:

    x4 - x10 = 0

    Then f(x) = x4 - x10

    df/dx = 4x3 - 1

    Let us first draw a

    graph and see where are

    the roots;

    The roots are between (-2, -1), & (1, 3)

    -3 -2 -1 0 1 2 3

    -20

    0

    20

    40

    60

    80

    f(x)

    x

  • 7/27/2019 Lec 8 Secant

    15/24

    Matlab pogram for graph

    % --------------- graph of a function

    clear all

    fun = @(x) x^4. - x - 10.0 ;

    xmin = -3.0; xmax = 3.0; max_points = 100;

    del = (xmax - xmin)/max_points;x(1)= xmin ; y(1)=0.0;

    for j=2: 1 : max_points

    x(j) = x(j-1) + del ; end

    for i = 1: max_points

    f(i)= fun(x(i)) ; y(i)=0.0 ; endaxis( [xmin xmax -20. 80.0] )

    hold on

    plot(x, f, x, y, 'LineWidth',2)

  • 7/27/2019 Lec 8 Secant

    16/24

    Solution

    4375.30.10)50.1()50.1()(50.1

    0.80.8

    .)2(0.1)0.8(0.1

    0.80.10.)1()1()(,0.1

    0.80.10.)2()2()(,0.2

    4

    11

    10

    10001

    4

    00

    411

    xfx

    xfxf

    xxxfxx

    xfx

    xfxIteration 1:

    2812.4)(,8767.1

    )0.8(4375.3

    )0.1(5.1)4375.3(5.1

    4375.30.10)50.1()50.1()(,50.1

    0.80.10.)1()1()(,0.1

    22

    01

    01112

    4

    11

    4

    00

    xfx

    xfxf

    xxxfxx

    xfx

    xfx

    Iteration 2:

  • 7/27/2019 Lec 8 Secant

    17/24

    Solution

    5951.0)(6678.1

    )4375.3(2812.4

    )5.1(8767.1)2812.4(8767.1

    2812.40.10)8767.1()8767.1()(,8767.1

    4375.30.10)50.1()50.1()(50.1

    33

    12

    12223

    4

    22

    411

    xfx

    xfxf

    xxxfxx

    xfx

    xfxIteration 3:

    Iteration 4:

    0857.0)(6933.1

    )2812.4(5951.0

    )8767.1(6678.1)5951.0(6678.1

    5951.00.10)6678.1()6678.1()(,6678.1

    2812.40.10)8767.1()8767.1()(,8767.1

    44

    23

    23334

    4

    23

    4

    22

    xfx

    xfxf

    xxxfxx

    xfx

    xfx

  • 7/27/2019 Lec 8 Secant

    18/24

    Solution

    0022.0)(6976.1

    6976.1

    0857.00.10)6933.1()6933.1()(,6933.1

    5951.00.10)6678.1()6678.1()(,6678.1

    45

    34

    34445

    4

    44

    433

    xfx

    xfxf

    xxxfxx

    xfx

    xfxIteration 5:

    Iteration 6:

    0001.0)(6975.1

    6975.1

    0022.00.10)6976.1()6976.1()(,6976.1

    0857.00.10)6933.1()6933.1()(,6933.1

    46

    45

    45556

    4

    45

    4

    44

    xfx

    xfxf

    xxxfxx

    xfx

    xfx

    The root seems to be -1.6975 with deviation of 0.0001.

  • 7/27/2019 Lec 8 Secant

    19/24

    Computer Program in MATLAB

    % Use of secant method to find the roots of F(x)=0% Input: xleft,xright = left and right brackets of the root

    % n = (optional) number of iterations; default: n =6

    % Output: x = estimate of the root

    n=8; xleft = -2.0; xright = -1.0;

    a = xleft; b =xright; % Copy original bracket to local

    variablesfa = a^4 - a - 10.0; % Initial values

    fb = b^4 - b - 10.0;

    fprintf(' k a b xm f(xm)\n');

    for k=1:n

    xm = b - fb*((b-a)/(fb-fa)); % Minimize roundoff

    fm = xm^4 - xm - 10.0; % f(x) at xm

    fprintf('%3d %12.8f %12.8f %12.8f

    %12.3e\n',k,a,b,xm,fm);

    a = b; fa = fb; b = xm; fb = fm;

    end

  • 7/27/2019 Lec 8 Secant

    20/24

    Results

    >>

    k a b xm f(xm)

    1 -2.00000000 -1.00000000 -1.50000000 -3.438e+000

    2 -1.00000000 -1.50000000 -1.87671233 4.282e+000

    3 -1.50000000 -1.87671233 -1.66776026 -5.959e-001

    4 -1.87671233 -1.66776026 -1.69328962 -8.570e-002

    5 -1.66776026 -1.69328962 -1.69757795 2.181e-003

    6 -1.69328962 -1.69757795 -1.69747151 -7.683e-006

    7 -1.69757795 -1.69747151 -1.69747188 -6.851e-0108 -1.69747151 -1.69747188 -1.69747188 0.000e+000

    >>

    Result of the computer program: xleft = -2.0 , xright = -1.0and iterations n = 8.

    The root value is x = -1.69747188

  • 7/27/2019 Lec 8 Secant

    21/24

    Advantages

    Converges fast, if it converges; Requires two guesses that do not need to bracket the

    root.

    It is used to refine answers obtained by other

    methods such as bisection. Since this method requires a good first approximation

    therefore it gives rapid convergence.

  • 7/27/2019 Lec 8 Secant

    22/24

    Problems with the Secant Method

    This method can be

    catastrophic if thepoints are near apoint where the firstderivative is zero.

  • 7/27/2019 Lec 8 Secant

    23/24

    Drawbacks

    Division by zero

    10 5 0 5 10

    2

    1

    0

    1

    2

    f(x)

    prev. guess

    new guess

    2

    2

    0

    f x( )

    f x( )

    f x( )

    1010 x x guess1 x guess2

    0 xSinxf

  • 7/27/2019 Lec 8 Secant

    24/24

    Drawbacks (continued)

    Root Jumping

    10 5 0 5 102

    1

    0

    1

    2

    f(x)x'1, (first guess)x0, (previous guess)Secant linex1, (new guess)

    2

    2

    0

    f x( )

    f x( )

    f x( )

    secant x( )

    f x( )

    1010 x x 0 x 1' x x 1

    0 Sinxxf