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

 

 

About The Author

7 Comments

  1. Luxor says:

    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…^_^

    • Ugur Dincer says:

      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. Jennifer says:

    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

    • Jennifer says:

      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. Ugur Dincer says:

    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. Dita says:

    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

    • Ugur Dincer says:

      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