Secant Method
BY
NEOLA MARYANNE VARELLA (191103036)
MANTHAN NAIK (191103031)
GAURANG TARI (191103017)
ENE A
ALGORITHM
Step 1 : Start
Ex. Using secant method find the root of the
function in [-1,0]
F(x)= cosx+2sinx+x2
Step 2 : Get values of x1,
x2 and e; where x1, x2
are the initial values
and e is the stopping
criteria
Ex.
x1 = -1 , x2 = 0 , e = 0.001
Step 3: Compute f(x1)
and f(x2)
Ex.
F(x)= cosx+2sinx+x2 [-1,0]
x1 = -1 f(x1)= -0.14263
x2 = 0 f(x2)= 1
▣
▣
▣
▣
▣
Step 6 : Display required
root as x3
Ex.
Required root is x3 = -0.6592
Graph of F(x)= cosx+2sinx+x2
Step 7 : STOP
Yes
No
Yes
No
No
Source Code for Secant Method
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
using namespace std;
float f(float x)
{
float fn = cos(x) + (2 * sin(x)) + pow(x, 2);
return fn;
}
int main()
{
cout.precision(4);
float x1, x2, x3, e;
int iter = 1;
cout<<"Enter the initial value \n x1 = ";
cin>>x2;
cout<<"\n x2 = ";
cin>>x3;
cout<<"\n Enter the degree of accuracy\n";
cin>>e;
cout<<"\n\nSr.No."<<setw(12)<<"x1"<<setw(12)<<"x2"<<setw(12)<<"x3"<<en
dl;
cout<<"__________________________________________\n";
do
{
if(f(x3) == f(x2))
{
cout<<"Mathematical Error\n";
return 0;
x1 = x2;
x2 = x3;
x3 = ((x1 * f(x2)) - (x2 * f(x1))) / (f(x2) - f(x1));
cout<<iter<<setw(16)<<x1<<setw(12)<<x2<<setw(12)<<x3<<endl;
if (f(x3) == 0)
{
cout<<"\nThe root of the equation is "<<x3;
return 0;
}
iter++;
}while(abs(x3 - x2) > e);
cout<<"\nThe root of the equation is "<<x3<<endl<<endl;
return 0;
}
Output