You are on page 1of 11

TP- 03

SECANT AND MODIFY SECANT METHOD

PRUM SOPHEARITH
E20170706

LECTURED BY:
CHHORN SOPHEAKTRA

ENGINEERING’S DEGREE
DEPARTMENT OF ELECTRICAL AND ENERGY ENGINEERING
INSTITUTE OF TECHNOLOGY OF CAMBODIA
PHNOM PENH
2019-2020
TP3

“Secant Method”

Instructed by “Chhorn Sopheaktra”

1. Introduction

A potential problem in implementing the Newton-Raphson method is the evaluation of


the derivative. Although this is not inconvenient for polynomials and many other functions, there
are certain functions whose derivatives may be extremely difficult or inconvenient to evaluate.
For these cases, the secant method is less complicated and more convenient.

2. Basic formula
2.1. Original secant method

As recall in tutorial 4, the Newton Raphson method is given by:

However, the derivative f’(x ) can be written as:

1
The secant method replaces the values in strict sequence, with the new value Xn replacing
Xn-1 and Xn replacing Xn-1 . As a result, the two values can sometimes lie on the same side of the
root. For certain cases, this can lead to divergence.

2.2 Modify secant method

Rather than using two arbitrary values to estimate the derivative, an alternative approach
involves a fractional perturbation of the independent variable to estimate

2
Where 𝛿 is a small perturbation fraction.

3. Algorithms
3.1. Original secant method
 Selected the initial value for x0 ,x1
 Define function of f(x)
 Find

 Checked the stopping condition based on error value/f(x) value and iteration value
 If the process is not meeting the stopping condition, repeat the process by
updating value of X0= X1, X1 = Xn

3.2. Modify secant method


 Selected the initial value for x0 and 𝛿
 Define function of f(x)
 Find

 Checked the stopping condition based on error value/f(x) value and iteration value

3
 If the process is not meeting the stopping condition, repeat the process by
updating value of X1 = Xn

4. Practices
Problem 1. Using matlab to find the root of equation f(x)=2sin(x)+cos(x)+x2,
corrected up to 5 decimal places by using secant method. The initial value is 𝑥0 =
0 and 𝑥1 = −0.1. Display result as a table and plot the graph of error of each
iteration.
 Implementation code

clear all;
clc;
fx=input('Insert a function:');
x0=input('Insert first initial value:');
x1=input('Insert second initial value:');
max_error=input('Insert maximum value:');
max_itr=input('Insert maximum iterations:');
itr=0;
while(1)
itr=itr+1;
x=x1-(fx(x1)*(x0-x1))/(fx(x0)-fx(x1));
error=abs(fx(x));

x_record(itr)=x;
error_record(itr)=error;
itr_record(itr)=itr;
if(error<max_error || itr>max_itr)
break;
end
x0=x1;
x1=x;
end
display(x);
display(itr);
display(error);

4
fprintf('%22s %11s %13s\n','Iteration','Result','Error');
fprintf('%22s %11s %12s
\n','---------','----------','----------');
fprintf('%20.4f | %12.5f|%12.5f|\n',
[itr_record;x_record;error_record]);
figure(1);
bar(itr_record,error_record,0.1);
title('Error Vs Iteration');
xlabel('iteration');
ylabel('abs(fx)');
grid minor;

 Input

Insert a function:@(x) 2*sin(x)+cos(x)+x^2;


Insert first initial value: 0;
Insert second initial value: -0.1;
Insert maximum value:0.000001;
Insert maximum iterations:100;

 Output and result

Iteration Result Error


--------- ---------- ----------
1.0000 | -0.51371| 0.15200|
2.0000 | -0.60996| 0.04605|
3.0000 | -0.65180| 0.00660|
4.0000 | -0.65880| 0.00041|
5.0000 | -0.65926| 0.00000|
6.0000 | -0.65927| 0.00000|

5
 Graph plotting of each iteration of f(x)=2sin(x)+cos(x)+x ,

Figure 1

6
Problem 2. Find the root of equation x f(x)=e -x -x, corrected up to 6 decimal places
by using modify secant method. The initial value is xi=1 and 𝛿 = 0.001. Display
result as a table and plot the graph of error of each iteration.

 Implementation code

clear all;
clc;
close all;
fx=input('Insert a function:');
xi=input('Insert first initial value:');
delta =0.001;
max_error=input('Insert maximum value:');
max_itr=input('Insert maximum iterations:');
itr=0;
while(1)
itr=itr+1;
x=xi-(delta*xi*fx(xi)/(fx(xi+delta*xi)-fx(xi)));
error=abs(fx(x));

x_record(itr)=x;
error_record(itr)=error;
itr_record(itr)=itr;
if(error<max_error || itr>max_itr)
break;
end
xi=x;
end
display(x);
display(itr);
display(error);
fprintf('%22s %11s
%13s\n','Iteration','Result','Error'); %display header
fprintf('%22s %11s %12s \n','---------','----------','-
---------');
fprintf('%20.4f | %12.6f|%12.6f|\n',
[itr_record;x_record;error_record]);
figure(1);
bar(itr_record,error_record,0.1);
title('Error Vs Iteration');
xlabel('iteration');
ylabel('abs(fx)');
7
grid minor;

 Input

Insert a function:@(x) exp(-x)-x;


Insert first initial value:1;
Insert maximum value:0.000001;
Insert maximum iterations:100;

 Output and result

Iteration Result Error


--------- ---------- ----------
1.000000 | 0.537821| 0.046199|
2.000000 | 0.566989| 0.000241|
3.000000 | 0.567143| 0.000000|

8
 Graph plotting of each iteration of f(x)=e-x- x

Figure 2

9
 Conclusion

In numerical analysis, the secant method is a root-finding algorithm that uses a succession


of roots of secant lines to better approximate a root of a function f. The secant method can be
thought of as a finite-difference approximation of Newton's method. At the same time, modify
secant method is also a preferable way to find roots because it only requires one initial points
which makes it convenient for users to easily find the result ;however, secant method needs two
initial points to complete the task.

10

You might also like