secant method
secant method
secant method
Theory –
Regula Falsi Method (or) Method of False Position:-
af ( b ) −bf ( a )
x 0=
f ( b ) −f ( a )
{ bf ( x 0 ) −x 0 f ( b ) }
else x 1=
f ( x 0 )−f (b)
Secant Method:-
x4=
{ x2 f ( x 3 )−x 3 f ( x 2 ) }
f ( x 3 )−f ( x 2)
x n+1=
{ x n−1 f ( x n )−x n f ( x n−1 ) }
f ( x n ) −f (x n−1 )
or
[
x n+1=x n −
{ x n−x n−1 }
f ( x n )−f ( x n−1) ] f ( x n )n = 1, 2, 3, ...
Note: In both the methods (Regula Falsi and Secant), we
approximate our curves with the chord joining two points. The
difference is that, in Regula Falsi method, we replace one of the
two old approximations, so that the root is always between these
two new approximations. But in the case of Secant method, the
oldest point is always replaced with a new approximation. It is not
necessary for Secant method that next two guesses will bracket
the root.
Input:-
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
/* Inputs */
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;
x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;
step = step + 1;
if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
}while(fabs(f2)>e);
return 0;
}
Output:-
Enter first guess: -3
Enter second guess: 5
Enter tolerable error: 0.0003
Enter maximum iteration: 6
**************
Secant Method
**************
Iteration-1: x2 = -2.214286 and f(x2) = 1.214649
Iteration-2: x2 = -2.302103 and f(x2) = 0.310115
Iteration-3: x2 = -2.332210 and f(x2) = -0.024315
Iteration-4: x2 = -2.330021 and f(x2) = 0.000424
Iteration-5: x2 = -2.330059 and f(x2) = 0.000002