15

Secant Method and Matlab Code

Determination of any roots of any function by Newton Raphson method is known. One condition function must be continous and derivable.

According to the Newton Raphson method, root of a function can be calculated by the following method;

sekantmethod1

 

This equation gets closer to the root as a result of consecutive iterrations. However, the potential hardness is that taking derivatives becomes very though and time consuming for some functions.

This is the main idea of secant method. When we define calculations of derivatives, the formula to determine root becomes like the following form.

sekantmethod2

Last obtained equation is known as secant method. Secant method is an open method. It can be observed that how it is tending to the root.

Secant_method.svg

Red line indicates the function we search for. Roots of blue lines are the ones determined after each iterrations.

 

MatLab Script Code for Secant Method

a=input('enter function:','s');
f=inline(a)

x(1)=input('enter first guess: ');
x(2)=input('enter second guess: ');
n=input('enter tolerance: ');

iteration=0;
for i=3:1000

   x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) - f(x(i-2))));
    iteration=iteration+1;

    if abs((x(i)-x(i-1))/x(i))*100<n
        root=x(i)
        iteration=iteration
        break

    end
end

 Output

sekantmethod7

 

 

Visual Basic Code for Secant Method

Public Class Form1

Private Function f(ByVal X As Double) As Double

        Dim a As Double
        Dim b As Double
        Dim c As Double

        a = Val(TextBox1.Text)
        b = Val(TextBox2.Text)
        c = Val(TextBox3.Text)

        Return a * X ^ 2 - b * X + c

    End Function

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer
        Dim u As Double
        Dim d As Double
        Dim k As Double
        Dim tol As Double

        u = Val(TextBox4.Text)
        d = Val(TextBox5.Text)

        tol = Val(TextBox6.Text)
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 100

        For i = 1 To 10000

            k = u - (f(u) * ((u - d) / (f(u) - f(d))))
            u = k
            ProgressBar1.Value = 100

            If Math.Abs((k - u) / k) * 100 < tol Then
                Exit For
            End If

        Next
        MsgBox(k, MsgBoxStyle.OkCancel)

    End Sub

End Class

SECANT METHOD EXAMPLE MANUAL SOLUTION

Using secant method, determine the roots of the function of

sekantmethod8

 

 

sekantmethod3

 

SOLUTION

iteration 1

sekantmethod4

 

iteration 2

sekantmethod5

 

iteration 3

sekantmethod6

 

 

editor

15 Comments

  1. Hi, may i know how to find the roots for;

    Secant Method:

    f(x)=x^3 – 2x^2 – 13x – 10

    using the MATLAB ?

    thank you…^_^

    • Roots of your function are -2,-1,5.
      You can use this script code.
      We suppose your first guess is 4 and second guess is 5. In this situation Secant Method is going to find 5(root).

      f=inline(‘x^3-2*x^2-13*x-10′)
      x(1)=input(‘first guess: ‘);
      x(2)=input(‘second guess: ‘);
      n=input(‘tolerance: ‘);
      iteration=0;
      for i=3:1000
      x(i) = x(i-1)-(f(x(i-1)))*((x(i-1)-x(i-2))/(f(x(i-1))-f(x(i-2))));
      iteration=iteration+1;
      if abs((x(i)-x(i-1))/x(i))*100 root=x(i)
      iteration=iteration
      break
      end
      end

  2. Hi :)

    May i know how to find the roots for
    f=(3*(p^2)*pi*u*d)+(.05*p*pi*(d^2)*(vo^2))+(.75*p*pi*(vo^2)*(d^2)*((1+(u^-.5)*(p^.5)*(vo^.5)*(d^.5))^-1))-(m*g);

    using MATLAB

    thanks :D

    • i forgot to add that
      g=9.81;
      u=1.8e-5;
      p=1.29; %which are all constants

      and i am trying to find vo as the root.

      How do i write an m-file function using secant method, where there are two(2) inputs which are ‘m’ and ‘d’

  3. Hi Jennifer
    Also we sent e-mail to your adress.

    m=input(‘enter m:’);
    d=input(‘enter d:’);
    g=9.81;
    u=1.8e-5;
    p=1.29;

    f=@(x) ((3*(1.29^2)*1.29i*1.8e-5*d)+(.05*1.29*1.29i*(d^2)*(x^2))+(.75*1.29*1.29i*(x^2)*(d^2)*((1+(1.8e-5^-.5)*(1.29^.5)*(x^.5)*(d^.5))^-1))-(m*9.81))
    x(1)=input(‘enter first guess: ‘);
    x(2)=input(‘enter second guess: ‘);
    n=input(‘enter tolerance: ‘);

    for i=3:1000

    x(i) = x(i-1) – (f(x(i-1)))*((x(i-1) – x(i-2))/(f(x(i-1)) – f(x(i-2))));
    if abs((x(i)-x(i-1))/x(i))*100 root=x(i)

    break

    end
    end

  4. halo
    i new in matlab
    i want to know how to get the answer for fn f(x)=x^3-6x^2+11x-6.1 for x1=2.5, xU=3.5, ea<10^-3 by using secant method in matlab . i dont know how to start it in matlab. hope u can help me sir.

  5. hello!
    i wrote this code, but matlab doesn’t accept it.
    It shows me
    ??? Error using ==> mupadmex
    Error in MuPAD command: Index exceeds matrix
    dimensions.

    Error in ==> sym.sym>sym.subsref at 1381
    B =
    mupadmex(‘symobj::subsref’,A.s,inds{:});

    What do i have to do??

    my code:
    >> f = inline(‘ tan(0.58*x+0.1)-x^2′,’x’);
    >> x1 = input(‘first guess: -2′);
    first guess: -2
    >> x2 = input(‘second guess: 0′);
    second guess: 0
    >> n = input(‘tolerance: 0.0001′);
    tolerance: 0.0001
    >> iteration = 0;
    >> for i = 3:1000
    x(i) = x(i-1)-(f(x(i-1)))*((x(i-1)-x(i-2))/(f(x(i-1))-f(x(i-2))));
    iteration = iteration+1;
    if ((abs((x(i)-x(i-1))/x(i))*100 ) < n)
    root = x(i)
    iteration = iteration
    break
    end
    end

    • f=inline(‘tan(0.58*x+0.1)-x^2′)
      x(1)=-2;
      x(2)=0;
      n=0.00001;
      iteration=0;
      for i=3:1000
      x(i) = x(i-1) – (f(x(i-1)))*((x(i-1) – x(i-2))/(f(x(i-1)) – f(x(i-2))));
      iteration=iteration+1;
      if abs((x(i)-x(i-1))/x(i))*100 root=x(i)
      iteration=iteration
      break
      end
      end

  6. Can you help me to find how solve these exercises using secant iteration method(matlab script code for secant method)?

    Find all zeros(five decimals correctly)
    1. f(x)= 0.5(5x^3-3x)
    2. f(x)=x cos(x)+sin(x)
    3. f(x)= 35x^4-30x^2+3/
    8

  7. Can anybody help me please? Determine the highest real root of f(x) = x3 – 6×2 + 11x – 6.1
    a) Graphically,
    b) Using the Newton-Raphson method (three iterations, xi = 3.5)
    c) Using the Secant method (three iterations, xi-1 = 2.5 and xi = 3.5).

  8. i wrote this code, but matlab doesn’t accept it.
    It shows me
    ??? Subscript indices must either be real positive integers or logicals.

    Error in ==> newton at 3
    f(C)=4.04+(3.94)*log(R*(sqrt(C)));

    Error in ==> run at 57
    evalin(‘caller’, [s ';']);

    please help me

    this is my code

    C=0.0001;
    R = 10^3:1000:10^5;
    f(C)=4.04+(3.94)*log(R*(sqrt(C)));
    df(C)=(1.97/C)+ (1/(2*C^1.5));
    C0 = 0;
    N = 10;
    tol 10^(-14);
    Iterations
    for i = ({1 to N});
    if df(C0)==0;
    end
    C=C0-(f(C0)/df(C0));
    if (abs(C-C0)<tol)||(abs(f(C))<tol)
    root=C;
    break
    end
    end
    if i==N;
    end
    C0 = C;
    output
    root;

    plot(Cx)

  9. how may get the root of the function using secant method (MATLAB), for three iterations with initial guess of X as (0 and 1). y(x)=cosX-Xexp^X
    here is the question.
    You are to write a short computer programme with MATLAB programming
    language (without using existing Library functions) that will generally obtain
    the bracket (xO1, xO2) containing the real root of this function within the
    range (0, 4) at the interval of +1.0 and real root of this function using
    SECANT and REGULA-FALSI methods. In developing the program, use the
    bracket obtained as the initial guesses, maximum number of iteration equal
    20 and error tolerance is to be less than 0.100E-03. Print in an output file:
    the number of iterations, the initial guesses, and the real root and the value
    of this function f(x) for this root.

Leave a Reply

Your email address will not be published. Required fields are marked *