Professional Documents
Culture Documents
BY
NEOLA MARYANNE VARELLA (191103036)
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>
float f(float x)
{
int main()
{
cout.precision(4);
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;
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++;
return 0;
}
Output