# 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;

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.

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.

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

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

SOLUTION

iteration 1

iteration 2

iteration 3

1. Luxor

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

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

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

• Jennifer

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

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. nyd

halo
i new in matlab
i want to know how to get the answer for fn f(x)=x^3-6x^2+11×-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. Dita

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 =

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

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. Pat

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. janset

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. han

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 ‘;’]);

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. gup

how would you do this with modified secant method?

10. JEROSE`

May i know how to find the roots for
f=x^3+3×-1=.0
using MATLAB

thanks

• Uğur Dinçer

Hi Jerose,
You can use this;

`f=[1 0 3 -1];`
`roots(f)`

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.

12. piojabet

Hello,

First of all: Thank you very much for your post! it has helped me A LOT!. However, I would like to know if you can help me by showing me how I can get to display the approximate root for each iteration. For instance, if it takes 9 iterations to get to the root I want to not only be able to see the root and the number of iterations, but also I want to see the approximate root for iteration 1, 2, 3, and so on.

Hope that you can help me!

Thanks!

• Uğur Dinçer

You can use this script I did edit;

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
approximate=x(i)
if abs((x(i)-x(i-1))/x(i))*100 root=x(i)
iteration=iteration
break
end
end

13. Nahom

I been trying to use secant method code for today days, but I couldn’t figure it out. Please hep me.
1)find all positive root(s) of f(x)=0.5*(3*x^2-1)
if I know how to do this, I can deal with the others that I have.
Thank you,

14. sergio herrera

Hi I was wondering if you could help me solve this problem using secant method: y=8e^(-kt)*cos(wt)
k=.5
‘find the time taken to reach a displacement level of y=5m and at w=.5 and w=6?’

15. Derman

Write an M-function with the following specifications:
function H = imcircle(R, M, N)
%IMCIRCLE Generates a circle inside a rectangle.
% H = IMCIRCLE(R, M, N) generates a circle of radius R centered
% on a rectangle of height M and width N. H is a binary image with
% 1s on the circle and 0s elsewhere. R must be an integer >= 1.
Bu soruda yardımcı olabilirmisiniz ?Şöyle de bir koşul var ”matlabın hiçbir fonksiyonu kullanmadan kendi fonksiyonumuzla yazmamız gerekiyor” yardımlarınızı bekliyorum