secant method

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Experiment:-3

 Theory –
 Regula Falsi Method (or) Method of False Position:-

Like Bisection method, the False-Position method also requires


two initial guesses, a and b, which bracket the root. Let the root
of the equation f(x) = 0 lie in the interval [a, b], and the function
f(x) be a continuous function. Then, the next approximation is the
point of intersection of the chord joining these two points {(a, f(a)),
(b, f(b))} and the x-axis. This point of intersection gives a "false
position" of the reason for the root, and it is the reason for the
name, method of false position or in Latin, regular falsi. The
equation of the chord joining two points (a, f(a)) and (b, f(b)) is as
follows
y−f ( b ) = [ f ( b ) −f (a)
b−a ]
( x−b )

Let x0 be the point of intersection of this chord and x-axis. At point


x= x0 , this equation is reduced to the following equation
−f ( b )=
[ f ( b )−f ( a )
b –a ]( x0 – b)

af ( b ) −bf ( a )
x 0=
f ( b ) −f ( a )

So, x0 is the approximation to the root. We will continue with the


new interval which brackets the root. If f(a)*f(x0) < 0 ,then the root
will lie in the interval (a, x0) so the next approximation is given by
x 1=
{af ( x 0 )−x 0 f ( a ) }
{ f ( x 0 )−f ( a ) }

{ bf ( x 0 ) −x 0 f ( b ) }
else x 1=
f ( x 0 )−f (b)

We will continue this process to generate a sequence x 1, x2, x3...,


till the nth approximation xn is correct up to the desired accuracy.

 Secant Method:-

In this method, we proceed like the Regula Falsi method, i.e., we


approximate our curve f(x) with the chord joining two points on the
curve. The only difference is that we don't check the intermediate
value property for the next two approximation points.
Let x0 and x1 be the two initial approximations, then the next
approximation, as in Regula Falsi method, is given by
{ x 0 f ( x 1) −x 1 f ( x 0 ) }
x 2=
f ( x 1 )−f (x 0 )
Now ,we will use the approximations x1and x2 to compute the next
approximation x3.
{ x 1 f ( x 2) −x 2 f ( x1 ) }x 3=
f ( x 2) −f ( x 1 )

Similarly, the approximation x is given by

x4=
{ x2 f ( x 3 )−x 3 f ( x 2 ) }
f ( x 3 )−f ( x 2)

Note that the next approximation is computed with latest available


approximations. We do not check the intermediate value property
in Secant method. So, the approximations need not to bracket the
root.

In general, let xn-1 and xn be two approximations at any step. The


next approximation is given by the equation of the chord joining
these two points {xn-1, f (xn-1)} and {xn,f(xn)}

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>

/* Defining equation to be solved.


Change this equation to solve another problem. */
#define f(x) x*x*x - 5*x + 1

using namespace std;

int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;

/* Setting precision and writing floating point values in fixed-point notation. */


cout<< setprecision(6)<< fixed;

/* 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;

/* Implementing Secant Method */


cout<< endl<<"**************"<< endl;
cout<<"Secant Method"<< endl;
cout<<"**************"<< endl;
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
cout<<"Mathematical Error.";
exit(0);
}

x2 = x1 - (x1 - x0) * f1/(f1-f0);


f2 = f(x2);

cout<<"Iteration-"<< step<<":\t x2 = "<< setw(10)<< x2<<" and f(x2) = "<<


setw(10)<< f(x2)<< endl;

x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;

step = step + 1;

if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
}while(fabs(f2)>e);

cout<< endl<<"Root is: "<< x2;

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

Root is: -2.330059

You might also like