Professional Documents
Culture Documents
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”
1. Introduction
2. Basic formula
2.1. Original secant method
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.
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
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
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
8
Graph plotting of each iteration of f(x)=e-x- x
Figure 2
9
Conclusion
10